Sending Emails using PHP

PHP provides a built-in function to send mail from the PHP script. In this tutorial, you are going to learn how to send emails to the user using PHP script.

Sending emails is very common for web applications nowadays, for example, sending emails to the user when crates a new account, sending emails newsletter to subscribed users, sending password resetting links to the user, or getting user feedback or comment through website’s contact form, and so on.

PHP mail() function

PHP provides a built-in function mail() function creating and sending email dynamically from PHP application either in plain text format or in HTML format. The basic syntax of the mail() function is as below:

PHP Mail() function syntax

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

Below are the details of the parameters.

Parameter Description
to Mandatory. Specifies the receiver/receivers of the email.
subject Mandatory. Specifies the subject of the email. This parameter cannot contain any newline character.
message Mandatory. Defines the message to be sent. Each line should be separated with an LF (\n). Lines should not exceed 70 characters
headers Optional. This specifies additional headers, like From, Cc, and Bcc. The additional headers should be separated with a carriage return plus a line feed-CRLF (\r\n)
Parameters Optional. This specifies an additional parameter.

Whenever the mail function is called, PHP will attempt to send the email and return true if successful and false if it is failed.

Sending Plain Text Emails

The simplest way to send email from PHP is sending email in plain text format. Now let’s take a simple example that sends an email in plain text format.

<?php
$to = 'teradatapoint@gmail.com';
$subject = 'PHP Email Testing';
$message = 'Hello! Welcome to the world of PHP. This email received from PHP script.'; 
 
// Sending email
if(mail($to, $subject, $message)){
       echo 'Mail has been sent successfully.';
} else{
       echo 'Unable to send email. Please try again.';
}

?>

Output

Mail has been sent successfully.

php email sending

In the above example, first, we have declared variables – $to, $subject, $message and later on we are passing those into mail() function.

Sending HTML formatted Emails

In general, PHP sends mail in plain text format. But if you want to improve the visual look of the email you need to embed it with HTML.

For sending an HTML-formatted email, the process will be the same. However, you need to provide additional headers as well as an HTML formatted message.

Now, let’s consider an example to send email in HTML format.

Example

<?php
$to = 'teradatapoint@gmail.com';
$subject = 'PHP Email Testing';
$from = 'tutorialsbook@gmail.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:#008000;">Hello Guest!</h1>';
$message .= '<p style="color:#B22222;font-size:20px;"> Welcome to the world of PHP. This email received from PHP script.</p>';
$message .= '</body></html>';
 
// Sending email
if(mail($to, $subject, $message, $headers)){
    echo 'Mail has been sent successfully.';
} else{
    echo 'Unable to send email. Please try again.';
}
?>

Output
PHP html formatted mail

Please get connected & share!

Advertisement