How to Send Email from a PHP Script using mail() function.



In this article, I will show you how to send  mail using PHP mail() function in PHP.


PHP mail() function:

The PHP mail() function is used to send emails in php.


Syntax :
mail(string to, string subject, string message, string headers,parameters);


to:  The receiver of the email
subject: The subject of the email.
message: the body of the email.
headers: Additional headers, like From, Cc, and Bcc.


Example:


<?php
$to = "rajesh@example.com";//The receiver of the email
$subject = "Test mail"; //The subject of the email.
$body = "Hello!"; //The body of the email.
$from = "test@example.com";
$headers = "From:" . $from; 
//mail() function
if(mail($to,$subject,$message,$headers)) 
{
   echo("Message successfully sent!");
}
else
{
  echo("Message delivery failed...");
}
?>