How to send email in PHP using mail() function on windows

mail() function is a useful function of PHP to send email using PHP script. You will require to set up some configurations to use this function. How the mail() function can be used to send email in PHP is shown in this tutorial.

Setup Configuration:

Two files will be required to modify to set up the configuration. One is php.ini file and another is sendmail.ini file. Follow the steps shown below to setup the configuration.

Steps: 1. Open the php.ini file and modify the file with the following configurations

SMTP=smtp.gmail.com
smtp_port=587
sendmail_from =sender@gmail.com
sendmail_path = "C:\xampp\sendmail\sendmail.exe -t"
mail.add_x_header=On

If the openssl extension is not enable by default in the file then enable it. extension=php_openssl.dll

2. Open the sendmail.ini file from the location, C:\xampp\sendmail and modify the file with the following configurations.


smtp_server=smtp.gmail.com
smtp_port=587
smtp_ssl=tls
error_logfile=error.log
debug_logfile=debug.log
auth_username='username@gmail.com'
auth_password='password'

3. Restart the Apache server.
4. Login to the Gmail account that is used in the configuration of sendmail.ini file. Enable the “Less secure app access” option of this account for sending email using the Gmail account.
Syntax:

bool mail (string $to, string $subject, string $message [, mixed $additional_headers [, string $additional_parameters ]])

This function can take four arguments. The first argument takes the receiver’s email address. The second argument takes the subject of the email. The third argument takes the email body. The last argument is optional and it contains additional information of the email as a string or an array.

Sending email using mail() function:

Different uses of mail() function are shown in this section by using multiple examples.

Example-1: Send a simple text email

How you can send a simple text email using mail() function is shown in this tutorial. Create a PHP file with the following script and run the script from any local or online server.


<?php

//Email address of the receiver
$to = "ahelperbd@gmail.com";
//Email subject
$subject = "testing mail() function";
//Email message
$message = "Sending email using mail() function";
//Header information
$headers = "From: Admin <admin@fahmidasclassroom.com>\r\n";
//Send email
if(mail($to, $subject, $message, $headers))
   echo "Email sent successfully.";
else
   echo "Unable to send the email.";
?>

Example-2: Send an HTML formatted email

How you can send a simple HTML formatted email using mail() function is shown in this tutorial. Create a PHP file with the following script and run the script from any local or online server.


<?php

//Email address of the receiver
$to = "ahelperbd@gmail.com";
//Email subject
$subject = 'Your order is submitted successfully.';
//Set the email body
$message = '
<p style="font-size:16px;">You have ordered the following products:</p>
<ol>
<li> A4 Mouse </li>
<li> Samsung Printer </li>
<li> HP Scanner </li>
</ol>
<p>Thank you for your order.</p>
';
//Newline characters
$nl = "\r\n";
//Header information
$headers = 'MIME-Version: 1.0'.$nl;
$headers .= 'Content-type: text/html; charset=iso-8859-1'.$nl;
$headers .= 'To: Fahmida Yesmin <fahmida18bd@gmail.com>'.$nl;
$headers .= 'From: Admin <admin@fahmidasclassroom.com>'.$nl;
//Send email
if(mail($to, $subject, $message, $headers))
   echo "Email sent successfully.";
else
   echo "Unable to send the email.";
?>

Example-3: Send email with attachment

How you can send a simple text email with the text file attachment using mail() function is shown in this tutorial. Create a PHP file with the following script and run the script from any local or online server.


<?php
//Email address of the receiver
$to = "ahelperbd@gmail.com";
//Email subject
$subject = "A text file is attached with the email";
//Read the content of the file
$filename = 'studentList.txt';
$content = file_get_contents($filename);
$content = chunk_split(base64_encode($content));
//Generate random hash number
$separator = md5(microtime(true));

//Message configuration
$message = "--" . $separator . "\r\n";
$message .= "Content-Type: text/plain; charset=\"iso-8859-1\"" . "\r\n";
$message .= "Content-Transfer-Encoding: 16bit" . "\r\n";
$message .= "A text file is attached with the email." . "\r\n";
$message .= "--" . $separator . "\r\n";
$message .= "Content-Type: application/octet-stream; name=\"" . $filename . "\"" . "\r\n";
$message .= "Content-Transfer-Encoding: base64" . "\r\n";
$message .= "Content-Disposition: attachment" . "\r\n";
$message .= $content . "\r\n";
$message .= "--" . $separator . "--";

//Header information
$headers = 'MIME-Version: 1.0'."\r\n";
$headers .= "From: Admin <admin@fahmidasclassroom.com>"."\r\n";
$headers .= "Reply-To: Admin <admin@fahmidasclassroom.com>"."\r\n";
$headers .= "Content-Type: multipart/mixed; boundary=\"" . $separator . "\"" ."\r\n";

//Send email

if(mail($to, $subject, $message, $headers))
   echo "Email sent successfully.";
else
   echo "Unable to send the email.";
?>

A video link of this tutorial is given below.