How to avoid emails going to spam when sending with PHP

Steve Sohcot
2 min readOct 15, 2020

--

I have a web application that sends e-mails, but it was ending up in the user’s spam/junk folder.

I was using PHP’s mail() function. I tried adjusting the “headers” that can be sent (one of the optional parameters in the mail() function), but that didn’t help.

I think the problem was that my mail server’s IP was on some sort of blacklist. I asked the host to look into it, and they said the tests came back okay. I asked my host if they could change the IP, but that didn’t work either. I’m pretty sure the cause was the host; as when I later transferred websites to places such as GoDaddy and HostGator I didn’t have this problem.

Anyways, the solution was to send an e-mail through SMTP instead of PHP’s mail(). For this app, I had Google Apps for Domains enabled, so that Gmail was my e-mail host. In code, I supplied the e-mail username/password. The e-mails sent even appear in my “Sent Mail” items.

Update: my current approach is to use 3rd party software to send out e-mails. Lately I’ve been using Amazon SES. In the past, I’ve also had good luck with Send Grid and MailGun. Other big names, that I haven’t tried exhaustively, are PostMarkApp and MadMimi.

Like this style of writing? 👍👍 Interested in creating a PHP web application and already know what a “variable” is and the concept of an “if” statement? Check out my book: Web Development for Intermediate Programmers — with PHP

--

--

No responses yet