PHP Send Emails

The PHP mail() Function

Sending email messages is a common task in web applications, such as sending welcome emails when a user creates an account, distributing newsletters to registered users, or receiving feedback through a contact form.

You can utilize the PHP built-in mail() function to dynamically create and send email messages to one or more recipients from your PHP application. Emails can be sent in either plain-text or formatted HTML. The basic syntax of this function is:

mail(to, subject, message, headers, parameters)

The following table outlines the parameters of the mail() function:

Parameter Description
Required — The following parameters are mandatory
to The recipient's email address.
subject The subject of the email to be sent. This parameter cannot contain newline characters (\n).
message The content of the email message. Each line should be separated by a line feed (\n). Lines should not exceed 70 characters.
Optional — The following parameters are optional
headers Additional headers like "From", "Cc", "Bcc". Multiple headers should be separated by carriage return and line feed (\r\n).
parameters Additional parameters to pass.

Sending Plain Text Emails

The simplest method to send an email using PHP is to send it as plain text. In the example below, we first define variables for the recipient's email address, subject line, and message body, and then use the mail() function to send the email.

Example

Download
<?php
$to = 'maryjane@email.com';
$subject = 'Marriage Proposal';
$message = 'Hi Jane, will you marry me?'; 
$from = 'peterparker@email.com';

// Sending email
if(mail($to, $subject, $message)){
echo 'Your mail has been sent successfully.';
} else{
echo 'Unable to send email. Please try again.';
}
?>

Sending HTML Formatted Emails

When you send a plain text message using PHP, the content is treated as simple text. To enhance the presentation, you can send an email formatted in HTML.

To send an HTML email, the process remains similar. However, you must include additional headers along with an HTML-formatted message.

Example

Download
<?php
$to = 'maryjane@email.com';
$subject = 'Marriage Proposal';
$from = 'peterparker@email.com';

// To send HTML mail, the Content-type header must be set
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

// Create email headers
$headers .= 'From: '.$from."\r\n".
'Reply-To: '.$from."\r\n" .
'X-Mailer: PHP/' . phpversion();

// Compose a simple HTML email message
$message = '<html><body>';
$message .= '<h1 style="color:#f40;">Hi Jane!</h1>';
$message .= '<p style="color:#080;font-size:18px;">Will you marry me?</p>';
$message .= '</body></html>';

// Sending email
if(mail($to, $subject, $message, $headers)){
echo 'Your mail has been sent successfully.';
} else{
echo 'Unable to send email. Please try again.';
}
?>

 

Note: Although the PHP mail() function is part of the PHP core, you need to set up a mail server on your machine for it to function properly.

In the following two chapters (PHP Form Handling and PHP Form Validation), you will discover how to create an interactive contact form on your website. This form will enable you to gather user comments and feedback, which can be sent via email using PHP's mail function.