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):