I was in a mess… PHP mail took hours to be delivered

Recently something has changed with the email security check adopted by Gmail and often emails sent via PHP mail() (using the PHP mail server) took hours and hours before being delivered.

From the Google Toolbox I just discovered that my emails were bouncing as not considered secure by Google:

I discovered that the first problem was that the email header “smtp.mailfrom” parameter was set to an anonymous email by the mail server:

and Google classified that origin as not secure.

So how can we change that header parameter when you send an email via a PHP page so that Google doesn’t consider your email insecure?

When you use the mail() method you can set some additional parameters:

So you can change the smtp.mailfrom parameter passing the from mail as the last argument of the mail() method:

Now the smtp.mailfrom parameter is well accepted by Google and the emails are delivered in 0 seconds instesd of 11 hours:

So, if you have time problems delivering your email and you are using the mail() function remember you can change the header smtp.mailfrom parameter by adding this string as last argument: “-f yourFromEmail@domain.ext” where yourFromEmail@domain.ext is the From email of your form (must be an existing email on an existing domain):

        $email["to"] = "$to"; 

        $email["headers"] = "";
        $email["headers"] .= 'MIME-Version: 1.0' . "\r\n" ;
        $email["headers"] .= 'Content-type: text/html; charset=UTF-8' . "\r\n" ;
        $email["headers"] .= "Content-Transfer-Encoding: 8bit\r\n";
        $email["headers"] .= "Date: ".date("r (T)")."\r\n";
        $email["headers"] .= "Bcc: " . $bcc . "\r\n" ;
        $email["headers"] .= "From: " . $from . " <" . $from . ">" . "\r\n" ;
        $email["headers"] .= "Reply-to: " . $from . "\r\n" ; 

        $email["subject"] = "$subject" ; 
        $email["body"] = "$body" ; 

        mail($email["to"], $email["subject"], $email["body"], $email["headers"], "-f " . $from );