PHP provides inbuilt function known as mail() function which allow you to send mail directly from your program. This function returns TRUE on success and FALSE when failed. Mail function accept 5 parameters like to, subject, message, headers and  extra parameters.

Syntax

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

Name Required Description
to Yes Mail Address of the receiver or receivers 
subject Yes Subject of the mail content.
message Yes Mail message. This can either be plain text or HTML formatted mail.
headers No Additional headers can be From, Cc, and Bcc etc.
parameters No Additional parameter to the send mail program.

PHP Simple Mail Function

The following example will send simple text mail.
<?php
$to = "receiver@domain.com";
$subject = "This is a test mail";
$message = "Hi! This is a simple text mail to test";
$from = "sender@a2zwebhelp.com";
$headers = "From:" . $from;
$result = mail($to,$subject,$message,$headers);
if($result==true){
   echo "Mail Sent successfully.";
}
?> 

PHP Send Mail With CC and BCC

<?php
$to = "receiver@domain.com";
$cc = "CCto@domain.com";
$bcc = "BCCto@domain.com";
$subject = "This is a test mail with CC and BCC";
$message = "Hi! This is a simple text mail to test with CC and BCC";
$from = "sender@a2zwebhelp.com";
$headers = "From:" . $from . "\r\n";
$headers .= "CC:" . $cc . "\r\n";
$headers .= "BCC:" . $bcc. "\r\n";
$result = mail($to,$subject,$message,$headers);
if($result==true){
   echo "Mail Sent successfully.";
}
?> 

PHP Send HTML Mail

<?php
$to = "receiver@domain.com";
$cc = "CCto@domain.com";
$bcc = "BCCto@domain.com";
$subject = "This is a test mail with CC and BCC";
$from = "sender@a2zwebhelp.com";
$message = "<table border="0" width="100%" cellspacing="0" cellpadding="0" 
style="font-family: Verdana; font-size: 8pt">
<tr>
<td colspan="2" align="center" height="40"><b>Simple HTML Mail</b></td>
</tr>
<tr>
<td height="25" width="10%">&nbsp;</td>
<td height="25">Mail content goes here. </td>
</tr>
<tr>
<td height="25" width="10%">&nbsp;</td>
<td height="25">Mail second line goes here.</td>
</tr>
</table>";
// Set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

$headers .= "From:" . $from . "\r\n";
$headers .= "CC:" . $cc . "\r\n";
$headers .= "BCC:" . $bcc. "\r\n";
$result = mail($to,$subject,$message,$headers);
if($result==true){
   echo "Mail Sent successfully.";
}
?> 

Send mail with attachment

Click here to know how to send mail with attachment.
Top