Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What do I need for a compliant email header

Tags:

php

email

header

I am trying to send an email from a site I am building, but it ends up in the yahoo spam folder. It is the email that sends credentials. What can I do to legitimize it?

$header = "From: site <[email protected]>\r\n";
$header .= "To: $name <$email>\r\n";
$header .= "Subject: $subject\r\n";
$header .= "Reply-To: site <[email protected]>" . "\r\n";
$header .= "MIME-VERSION: 1.0\r\n";
$header .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$phpversion = phpversion();
$header .= "X-Mailer: PHP v$phpversion\r\n";
mail($email,$subject,$body,$header);
like image 377
Jack B Nimble Avatar asked Oct 02 '08 06:10

Jack B Nimble


4 Answers

  • Don't use HTML in your email.
  • Send it via a legitimate mail server with a static IP and reverse-DNS (PTR) that points to the machine's real host name (and matches a forward lookup).
  • Include a Message-ID (or ensure that the local mailer adds one for you).
  • Run your email through SpamAssassin and see which bad-scoring rules it matches. Avoid matching them.
  • Use DomainKeys Identified Mail to digitally sign your messages.
like image 163
Ted Percival Avatar answered Oct 11 '22 11:10

Ted Percival


I just successfully tried the following from my Yahoo! Web Hosting account:

$email = "[email protected]";
$subject = "Simple test";
$body = "Simple test";
$header = "From: site \r\n";
$header .= "To: $name \r\n";
$header .= "Subject: $subject\r\n";
$header .= "Reply-To: site " . "\r\n";
$header .= "MIME-VERSION: 1.0\r\n";
$header .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$phpversion = phpversion();
$header .= "X-Mailer: PHP v$phpversion\r\n";
mail($email,$subject,$body,$header);

However, you have some duplication in your header you should only need to do the following:

$email = "[email protected]";
$subject = "Simple test";
$body = "Simple test";
$header = "From: site \r\n";
$header .= "MIME-VERSION: 1.0\r\n";
$header .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$phpversion = phpversion();
$header .= "X-Mailer: PHP v$phpversion\r\n";
mail($email,$subject,$body,$header);
like image 30
Brian Matthews Avatar answered Oct 11 '22 09:10

Brian Matthews


In addition to Ted Percival's suggestions, you could try using PHPMailer to create the emails for you rather than manually building the headers. I've used this class extensively and not had any trouble with email being rejected as spam by Yahoo, or anyone else.

like image 33
Shabbyrobe Avatar answered Oct 11 '22 10:10

Shabbyrobe


There is also the possibility that 'sendmail' (which is underneath the PHP mail() function) needs extra parameters. If you have a problem with return headers (such as Return-Path) not being set with what you set them to be, you may need to use the fifth mail() parameter. Example:

mail('[email protected]', 'Subject', $mail_body, $headers, " -f [email protected]");

There is some further evidence that true vanilla sendmail may have problem with this! Hopefully you have 'postfix' as PHP's underlying mail() support on your target server.

like image 39
jschrab Avatar answered Oct 11 '22 11:10

jschrab