Tuesday, August 7, 2012

PHP: How to avoid a system generated e-mail going into spam?

Something that gets overlooked often: If you are sending mail manually (i.e. with mail(), not with a framework) and using sendmail, make sure you set the SMTP envelope sender address.
$headers = "MIME-Version: 1.0\r\n"
  ."Content-Type: text/plain; charset=utf-8\r\n"
  ."Content-Transfer-Encoding: 8bit\r\n"
  ."From: =?UTF-8?B?". base64_encode($from_name) ."?= <$from_address>\r\n"
  ."X-Mailer: PHP/". phpversion();
mail($to, $subject, $body, $headers, "-f $from_address");
-f tells sendmail what address to use in the SMTP wrapper; by default it is the real hostname of the machine, while the from address tends to be an alias. Some spam filters get jumpy when the two addresses don't match.
Other, more obvious advice:
  • make sure the from/reply to address resolves to the machine you are actually sending from
  • make sure no spam gets sent from your machine (open relay, other users, a website where you can enter any email address without confirmation)
  • don't use techniques which are used by spammers to trick filters (e.g. text as image)

No comments:

Post a Comment