PHP Sending Emails
PHP allows you to send emails directly from a script.
PHP mail() Function
The PHP mail() function is used to send emails from a script.
Syntax
Parameter | Description |
---|---|
to | Required. Specifies the email recipient. |
subject | Required. Specifies the subject of the email. Note: This parameter cannot contain any newline characters. |
message | Required. Defines the message to be sent. Use LF (\n) to separate lines. Each line should be limited to 70 characters. |
headers | Optional. Specifies additional headers, such as From, Cc, and Bcc. Additional headers should be separated by CRLF (\r\n). |
parameters | Optional. Specifies extra parameters for the mail sending program. |
Note: The PHP mail function requires a mail system (such as sendmail, postfix, qmail, etc.) to be installed and running. The program used is defined by the configuration settings in the php.ini file. Please read more in our PHP Mail Reference Manual.
PHP Simple E-Mail
The simplest way to send an email using PHP is to send a text email.
In the following example, we first declare variables ($to, $subject, $message, $from, $headers), and then we use these variables in the mail() function to send an email:
<?php
$to = "[email protected]"; // Email recipient
$subject = "Parameter Mail"; // Email subject
$message = "Hello! This is the email content."; // Email body
$from = "[email protected]"; // Email sender
$headers = "From:" . $from; // Header settings
mail($to,$subject,$message,$headers);
echo "Email sent";
?>
PHP Mail Form
With PHP, you can create a feedback form on your site. The following example sends a text message to a specified email address:
<html>
<head>
<meta charset="utf-8">
<title>tutorialpro.org(tutorialpro.org)</title>
</head>
<body>
<?php
if (isset($_REQUEST['email'])) { // If email parameter is received, send the email
// Send email
$email = $_REQUEST['email'] ;
$subject = $_REQUEST['subject'] ;
$message = $_REQUEST['message'] ;
mail("[email protected]", $subject, $message, "From:" . $email);
echo "Email sent successfully";
} else { // If no email parameter, display the form
echo "<form method='post' action='mailform.php'>
Email: <input name='email' type='text'><br>
Subject: <input name='subject' type='text'><br>
Message:<br>
<textarea name='message' rows='15' cols='40'>
</textarea><br>
<input type='submit'>
</form>";
}
?>
</body>
</html>
- First, check if the email input box is filled
- If not filled (e.g., when the page is first visited), output the HTML form
- If filled (after the form is filled out), send the email from the form
- After submitting the form and clicking the submit button, the page reloads, the email input is reset, and a success message is displayed
Note: This simple email sending method is insecure. In the next chapter of this tutorial, you will read more about security risks in email scripts, and we will explain how to validate user input to make it more secure.
PHP Mail Reference Manual
For more information about the PHP mail() function, please visit our PHP Mail Reference Manual.