How to Send PHP Email with Proper Headers

All the new bees to PHP always stuck up on the fact that How to send email in PHP with proper email headers.

The fact is that if you send an email with just “To, Subject, Message, From” parameters in the mail function, then it is very likely that the email will end up as spam or in junk email folder.

The reason is that the mail servers these days have become very smart and have been trained filter junk emails or spam email.

So, today we will decide and take an oath that we will send email through PHP with proper email headers so that our email does not land up in spam/junk folder.

Now our PHP mail function is as below-

mail($to, $subject, $message, $from);

We will add some more headers in so that we can send an email with proper headers.

Our code is as below in the form of a function.

You can include this function in your common include file or functions file so that it can be called from anywhere and from any file.

function MAIL_FIRE($from_name, $from_email, $to_email,$to_bcc, $subject, $message)
{
$headers¬† = “MIME-Version: 1.0\n”;
$headers .= “Content-type: text/html; charset=iso-8859-1\n”;
$headers .= “X-Priority: 3\n”;
$headers .= “X-MSMail-Priority: Normal\n”;
$headers .= “X-Mailer: PHP/” . phpversion().”\n”;
$headers .= “From: \””.$from_name.”\” <“.$from_email.”>\n”;
if($to_bcc)
{
$headers .= “Bcc: $to_bcc\n”;
}
$headers .= “Reply-To: $from_email\n”;
return mail($to_email, $subject, $message, $headers);
}

Now as our email function is ready above I will provide you with a sample code to call the above mail function.

//Send Email
$to_email = “info@somehost.com”;
$subject = “Thanks; for this sample code”;
$message = “This is a message string”;

$from_email = “thanks@somehost.com”;
$from_name = “Gaurav Kumar”;
$to_bcc = “gkumar@oswebstudio.com”;

$mailto = MAIL_FIRE($from_name, $from_email, $to_email,$to_bcc, $subject, $message);

Thats it….. Simple.¬† Happy Mailing.

Both comments and pings are currently closed.

One Response to “How to Send PHP Email with Proper Headers”

  1. This is a really good read for me, must admit that you are one of the best bloggers I ever saw. Thank you for writing this informative article UncleCode.Com – Technology Blog | Send email using PHP with proper headers to avoid email going into Junk or bulk folder . Carry on the good work, I’ve added you to my blogroll.