PHP, Gmail and spam on Echominder

October 23rd, 2007 – by Matt

Gmail is great - it has an excellent spam filter and you can even use it as a gateway for your regular email. For instance, I’m in the process of moving some non-profit websites, and I’m implementing Gmail for Non-Profits (same as the Education edition) - a free service from Google that will allow you to create Gmail filters for your email accounts. All email is still sent to yourname@yoursite.com, but Gmail intercepts any incoming email (via MX record changes) and filters the email for spam, then forwards it on to yoursite.com. There aren’t any client-side changes necessary: it is all invisible to your end users.

But… Gmail’s spam filter is, well, REALLY really good. When we launched echominder, we discovered that echominder emails were going into the Gmail SPAM box. Not good. A quick search on Google found lots of other people with the same problem, but no answers.

I was originally using PHP’s mail() function and creating my own MIME headers and such (for the audio file attachment). This worked for our internal company and most other emails, but not for Gmail.

I switched to PHP’s PEAR add-on library and finally got it working, but it was interesting how Gmail treated the messages. Scenario code below is selective, truncated…

Scenario A) Where I began…

$message = new Mail_Mime();
$message->setTXTBody() and ->setHTMLBody() and ->addAttachment($file,”audio/x-wav”);
Mail::factory(”mail”)->send($to, $headers, $message->get());

What happened here is I would get a non-SPAM message, but there was no text, just a no-type attachment (not the wav file, but just text/encoding junk).

Scenario B) Only send one type…

$message->setTXTBody();

Here I got the correct message, readable, with a WAV file attachment I could listen to. However, it went to SPAM! Didn’t matter which type I sent: TXT or HTML. Comment out one of them, and the message came in clear but in SPAM. Leave both in and the message would come in as junk, but NOT SPAM. Hmmm….

Scenario C) Add the crlf type to the mime creation…

$crlf = “\n”;
$message = new Mail_Mime($crlf);

Okay, so now I had basically the same thing as Scenario A: both TXT and HTML is set, all I did was add the $crlf parameter to the class construction. Shouldn’t make a difference… \n is supposed to be the default. Now… it went to SPAM, but in the clear! Argh, weird. Leave out the \n, it goes to Inbox but garbage. Put it in, it goes to SPAM but in the clear!

Scenario D) Send it using SMTP instead of Mail() class…

Mail::factory(”smtp”,array(”host” => “intuit mail servers internal IP address“, “auth” => “authentication info“))->send($to, $headers, $message->get());

This is the same as Scenario A - setTXT and setHTML, Mail_Mime() without a crlf parameter, just sent using a forwarding server rather than the originating server. Presto! It worked! Seems that maybe Gmail tries to ping or otherwise connect to the originating server. Since echominder is in a DMZ and doesn’t allow those types of connections, Gmail must have been thinking it was trying to send SPAM from an invalid server address.

Anyway, the ultimate solution? PEAR and Factory(”smtp”) to a mail server that allows forwarding from your server.

- Matt

leave a comment




You can use some HTML, but don't get too fancy.