English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this tutorial, you will learn how to use the PHP mail() function to send simple text or HTML emails directly from the script.
Sending email messages is very common in web applications, such as sending welcome emails when users create accounts on your website, sending newsletters to your registered users, or obtaining user feedback or comments through the website's contact form, etc.
You can use the built-in PHP mail() function to send email messages in plain text format or formatted HTML dynamically created from a PHP application and sent to one or more recipients. The basic syntax of the function can be given as follows:
mail(to, subject, message, headers, parameters)
The following table summarizes the parameters of this function.
Parameter | Description |
---|---|
Required - The following parameters are required | |
to | Recipient's email address. |
subject | The subject of the email to be sent. This parameter, the subject line, must not contain any newline characters (\n). |
message | Define the message to be sent. Each line should be separated by a newline character LF (\n). The number of lines cannot exceed70 characters. |
Optional - The following parameters are optional | |
headers | Is usually used to add additional headers, such as 'From', 'Cc', 'Bcc'. Additional headers should be separated by carriage return and line feed CRLF (\r\n). |
parameters | Used to pass other parameters. |
The simplest way to send an email using PHP is to send a plain text email. In the following example, we first declare the variables-Recipient's email address, subject line, and email body-Then pass these variables to the mail() function to send an email.
<?php $to = '[email protected]'; $subject = 'Proposal'; 'message' = 'Hi, Jane, will you marry me?'; $from = '[email protected]'; //Send Email if(mail($to, $subject, $message)){ echo 'Your email has been sent successfully.'; } else { echo 'Unable to send email. Please try again.'; } ?>
When sending text messages using PHP, all content is treated as plain text. We will improve the output and convert the email into an HTML-formatted email.
The process will be the same when sending text messages using PHP. However, this time, we need to provide other headers as well as HTML-formatted messages.
<?php $to = '[email protected]'; $subject = 'Proposal'; $from = '[email protected]'; //To send HTML emails, you must set the Content-type header $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; //Create Email Subject $headers .= 'From: $from."\r\n". Reply-To: $from."\r\n" . 'X-Mailer: PHP/' . phpversion(); //Write a Simple HTML Email $message = '<html>'; $message .= '<h1 style="color:#f40;">Hi Jane!</h1"> $message .= '<p style="color:#080;font-size:18px;">Will you marry me?</p>'; $message .= '</html>'; //Send Email if(mail($to, $subject, $message, $headers)){ echo 'Your email has been sent successfully.'; } else { echo 'Unable to send email. Please try again.'; } ?>
Note:The PHP mail() function is part of the PHP core, but you need to set up a mail server on your computer to make it work properly.
In the next two chapters (PHP Form HandlingandPHP Form ValidationIn this section, you will learn how to implement an interactive contact form on your website to use this PHP email sending feature to receive users' comments and feedback via email.