How Can I Send Email Using mail() Function in PHP?

PHP provides you with built in mail () function to send emails. The general syntax of this function is given below.

Keywords :- Send Email Using Mail() Function by PHP, How to Send Email with Mail() Function Using PHP

PHP Mail Function Syntax


In PHP, 4 parameters are passed inside the mail () function –

to – In this we put the email id you want to send the mail to.
subject – in this we write the subject of the email.
message – put any message you want to send in it.
headers – this is an optional field, in which you have to give from, cc, bcc etc.
parameters – This is also an optional field.

Example 1:- sendmail.php

$from = "[email protected]"
$to = "[email protected]";
$subject = "My Message : - Experts PHP"; 
$message = "<h1>Hello Friend, I am Experts PHP.</h1>"; 
$header = "From : ".$from."\r\n"; 
$header .= "MIME-Version : 1.0 \r\n"; 
$header .= "Content-type : text/html \r\n"; 
if(mail ($to,$subject,$message,$header)){
echo "Email sent successfully."; 
echo "Email cannot sent."; 


Example 2:- sendmail.php

$to = '[email protected]';
$subject = 'Marriage Proposal';
$from = '[email protected]';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: '.$from."\r\n".
'Reply-To: '.$from."\r\n" .
'X-Mailer: PHP/' . phpversion();
$message = '<html><body>';
$message .= '<h1 style="color:red;">Hi Jyoti ji!</h1>';
$message .= '<p style="color:green;font-size:16px;">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.';


Cc:- Message is sent to more than one email id from Cc (Carbon copy). The receiver who is the receiver sees all the email id of Cc.

Bcc:- Message is sent to more than one email id from Bcc (Blind carbon copy). But the receiver who is the receiver does not see the email id of Bcc.