PHP Mail Function

The mail() function allows you to send emails, as long as the server has a fully-functional email system already set up.

This function accepts five different parameters, only four of which we will discuss here, and only three of which are required.

Parameter Importance Description
to Required Recipient(s) of Email
subject Required Subject of Email
message Required Message Body of Email
headers Optional From, Reply-To, CC, BCC, Date, Etc.

Listed in order of appearance, the parameters are: mail(to,subject,message,headers)

One of the most simple examples of the mail() function is:

mail("[email protected]","Brilliant Subject","Boring message body.");

The addition of headers allows you to specify who the email is from and who should be copied and/or blind copied on an email

$to = "[email protected]";
$subject = "Why do dogs bury bones in the ground?";
$message = "Because they can't bury them in trees!";
$headers = 'From: <[email protected]>' . "\r\n";
$headers .= 'Reply-To: [email protected]' . "\r\n";
$headers .= 'Cc: [email protected]' . "\r\n";
$headers .= 'Bcc: [email protected]' . "\r\n";

HTML emails can be sent by specifying the appropriate content-type in the header.

$to = "[email protected]";
$subject = "This is an HTML email!";
$message = "
<h2>Cool, right?</h2>
$headers = 'From: <[email protected]>' . "\r\n";
$headers .= "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";


This site uses Akismet to reduce spam. Learn how your comment data is processed.