6

I'm using the phpmailer class to send emails. Currently gmail and yahoo do not mark emails as spam, but hotmail always does. How can I prevent this? My code is below.

require_once('../PHPMailer/class.phpmailer.php');
//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded

$mail = new PHPMailer();

$mail->IsSMTP();    // set mailer to use SMTP
$mail->Host = "mail.example.com";    // specify main and backup server
$mail->SMTPAuth = true;    // turn on SMTP authentication
$mail->Username = "xxx";    // SMTP username -- CHANGE --
$mail->Password = "xxx";    // SMTP password -- CHANGE --
$mail->Port = "25";    // SMTP Port

$mail->From = "no-repy@example.com";    //From Address -- CHANGE --
$mail->FromName = "xxx";    //From Name -- CHANGE --
$mail->AddAddress($email, $passerusername);    //To Address -- CHANGE --
$mail->AddReplyTo("no-reply@example.com", "xxx"); //Reply-To Address -- CHANGE --

$mail->WordWrap = 50;    // set word wrap to 50 characters
$mail->IsHTML(false);    // set email format to HTML

$mail->Subject = "AuthSMTP Test";
$mail->Body    = "AuthSMTP Test Message!";

if(!$mail->Send())
{
   echo "Message could not be sent. <p>";
   echo "Mailer Error: " . $mail->ErrorInfo;
   exit;
}

echo "Message has been sent";
Kenster
  • 23,465
  • 21
  • 80
  • 106
Anonymous
  • 179
  • 2
  • 3
  • 10
  • Probably things like server SPF records, reverse DNS, and the like. (not too helpful I know, but this is why it's a comment!) – jprofitt Mar 28 '12 at 00:56
  • also if it helps, i use a vps server, not a shared one with hostgator – Anonymous Mar 28 '12 at 01:00
  • 1
    Is the `From` and `AddReplyTo` supposed to be different addresses or is that a typo? If you have that in your actual code, it may cause the mail to be filtered as spam. – jk. Mar 28 '12 at 01:23
  • ya fixed that 20 mins ago and its seems to be working. took me 3 hours to realize that mistake wow... – Anonymous Mar 28 '12 at 01:35

1 Answers1

18

This involves setting a few mail headers to beat the filters.

I have added the following to the very start of php mailers CreateHeader method...

$result = '';

$result .= $this->HeaderLine("Organization" , SITE); 
$result .= $this->HeaderLine("Content-Transfer-encoding" , "8bit");
$result .= $this->HeaderLine("Message-ID" , "<".md5(uniqid(time()))."@{$_SERVER['SERVER_NAME']}>");
$result .= $this->HeaderLine("X-MSmail-Priority" , "Normal");
$result .= $this->HeaderLine("X-Mailer" , "Microsoft Office Outlook, Build 11.0.5510");
$result .= $this->HeaderLine("X-MimeOLE" , "Produced By Microsoft MimeOLE V6.00.2800.1441");
$result .= $this->HeaderLine("X-Sender" , $this->Sender);
$result .= $this->HeaderLine("X-AntiAbuse" , "This is a solicited email for - ".SITE." mailing list.");
$result .= $this->HeaderLine("X-AntiAbuse" , "Servername - {$_SERVER['SERVER_NAME']}");
$result .= $this->HeaderLine("X-AntiAbuse" , $this->Sender);

that was done some time ago - I haven't revisited for about a year I think! Try it and come back if you still have problems.

Ian Wood
  • 6,515
  • 5
  • 34
  • 73
  • if you could accept as answer I'd be over the moon! (people lik it when you accept answers - they get credit and you get a better reputation) – Ian Wood Aug 08 '12 at 10:55
  • @TuralTeyyuboglu oops my bad - sowwie – Ian Wood Aug 09 '12 at 12:35
  • @IanWood I was looking for emails to gmail. Mine were already working fine to hotmail. Anyways +1 for the headers. didn't knw we could do this – ravi404 Sep 20 '12 at 20:55
  • 2
    Outlook has a very particular way of building html for the emails, it’s pretty easy to see if an email has been composed by another email client’s html composer. Being caught on the Forged_Mua_Outlook spam rule will kill your IP. Very risky. – MPaulo Dec 07 '12 at 05:58
  • quite possibly - I don't profess to know the ins and outs of the internals of how emails/ips get blocked. This 'solution' is organic I have had cause to tweak every now and then but the principle is simply to find values that do the job. I shall remove any outlook reference in future. – Ian Wood Dec 07 '12 at 10:24
  • What is $result variable where to use it? @IanWood – NomanJaved Mar 07 '21 at 21:55