MOCKSTACKS
EN
Questions And Answers

More Tutorials









PHP Sending Email


A typical email has three main components:
1. A recipient (represented as an email address)
2. A subject
3. A message body

Sending mail in PHP can be as simple as calling the built-in function mail(). mail() takes up to five parameters but the first three are all that is required to send an email (although the four parameters is commonly used as will be demonstrated below). The first three parameters are:
1. The recipient's email address (string)
2. The email's subject (string)
3. The body of the email (string) (e.g. the content of the email)

A minimal example would resemble the following code:

mail('recipient@example.com', 'Email Subject', 'This is the email message body');


The simple example above works well in limited circumstances such as hardcoding an email alert for an internal system. However, it is common to place the data passed as the parameters for mail() in variables to make the code cleaner and easier to manage (for example, dynamically building an email from a form submission).

Additionally, mail() accepts a fourth parameter which allows you to have additional mail headers sent with your email. These headers can allow you to set:

the From name and email address the user will see the Reply-To email address the user's response will be sent to additional non-standards headers like X-Mailer which can tell the recipient this email was sent via PHP

$to = 'recipient@example.com'; // Could also be $to = $_POST['recipient'];
$subject = 'Email Subject'; // Could also be $subject = $_POST['subject'];
$message = 'This is the email message body'; // Could also be $message = $_POST['message'];
$headers = implode("\r\n", [
 'From: John Conde <webmaster@example.com>',
 'Reply-To: webmaster@example.com',
 'X-Mailer: PHP/' . PHP_VERSION
]);


The optional fifth parameter can be used to pass additional flags as command line options to the program configured to be used when sending mail, as defined by the sendmail_path configuration setting. For example, this can be used to set the envelope sender address when using sendmail/postfix with the -f sendmail option.

$fifth = '-fno-reply@example.com';


Although using mail() can be pretty reliable, it is by no means guaranteed that an email will be sent when mail() is called. To see if there is a potential error when sending your email, you should capture the return value from mail(). TRUE will be returned if the mail was successfully accepted for delivery. Otherwise, you will receive FALSE.

$result = mail($to, $subject, $message, $headers, $fifth);


NOTE: Although mail() may return TRUE, it does not mean the email was sent or that the email will be received by the recipient. It only indicates the mail was successfully handed over to your system's mail system successfully.

If you wish to send an HTML email, there isn't a lot more work you need to do. You need to:
1. Add the MIME-Version header
2. Add the Content-Type header
3. Make sure your email content is HTML

$to = 'recipient@example.com';
$subject = 'Email Subject';
$message = '<html><body>This is the email message body</body></html>';
$headers = implode("\r\n", [
 'From: John Conde <webmaster@example.com>',
 'Reply-To: webmaster@example.com',
 'MIME-Version: 1.0',
 'Content-Type: text/html; charset=ISO-8859-1',
 'X-Mailer: PHP/' . PHP_VERSION
]);


Here's a full example of using PHP's mail() function

<?php
// Debugging tools. Only turn these on in your development environment.
error_reporting(-1);
ini_set('display_errors', 'On');
set_error_handler("var_dump");
// Special mail settings that can make mail less likely to be considered spam
// and offers logging in case of technical difficulties.
ini_set("mail.log", "/tmp/mail.log");
ini_set("mail.add_x_header", TRUE);
// The components of our email
$to = 'recipient@example.com';
$subject = 'Email Subject';
$message = 'This is the email message body';
$headers = implode("\r\n", [
 'From: webmaster@example.com',
 'Reply-To: webmaster@example.com',
 'X-Mailer: PHP/' . PHP_VERSION
]);

// Send the email
$result = mail($to, $subject, $message, $headers);
// Check the results and react accordingly
if ($result) {
 // Success! Redirect to a thank you page. Use the
 // POST/REDIRECT/GET pattern to prevent form resubmissions
 // when a user refreshes the page.
 header('Location: http://example.com/path/to/thank-you.php', true, 303);
 exit;
}
else {
 // Your mail was not sent. Check your logs to see if
 // the reason was reported there for you.
}
?>


Conclusion

In this page (written and validated by ) you learned about PHP Sending Email . What's Next? If you are interested in completing PHP tutorial, your next topic will be learning about: PHP Sending HTML Email Using mail().



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.