email goes to spam (gmail), PHP

Posted September 25, 2014 39.3k views

i managed to make sendmail work and i can send email out with mail() in PHP. however that email always goes to spam. my domain is new, has never been used before. i read that i have to create reverse DNS (i really have no idea how to do it) to avoid emails to be delivered to spam folder. does anyone know what exactly do i have to do to see email sent from my php script to appear in INBOX (gmail)?

thank you!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Submit an Answer
2 answers


you are right, you need a reverse DNS record. DigitalOcean sets this record based on the hostname of your droplet automatically. You can check your settings with the command:

jkostolansky@server:~$ hostname

If you wish to update your rDNS record, you can rename your droplet in the control panel:

  1. navigate to the droplet listing
  2. select the droplet to rename
  3. choose the “Settings” tab and the “Rename” option
  4. fill in the “New Hostname” field and click the “Rename” button

And I recommend you to create SPF and DKIM records, too.

by Pablo Carranza
A carefully tailored SPF record will reduce the likelihood of your domain name getting fraudulently spoofed and keep your messages from getting flagged as spam before they reach your recipients.
  • hmmm ok, my domain is (i made it up).

    when i execute “hostname” in shell i get this:

    demo@Superman:~$ hostname

    Superman is my droplet name and currently i have 3 domains there. do i have to rename the droplet to and emails wont go to spam anymore?

  • Yes, you need to rename it. You can host as many domains on one droplet as you want, but you need to choose one “primary”. This is the domain which you will use for your PTR (reverse DNS record).

    Try to rename your droplet and let us know if this fixes your problem.

  • I have the same problem, so if my hostname is “web”, Should I rename my droplet to or ?

  • You can use any subdomain, if you wish. Just make sure that a correct A DNS record for this subdomain is set.

    You can check the correctness of your settings on this page. If you see something like resolved to resolved to; 

    everything is OK.

  • Thanks! all is ok, I suposse that take a few hours to propagate all the changes.

  • i renamed it to my domain name but when i go to the page you mentioned i get this:

    Check reverse DNS

    IP address or host name:
    FCrDNS test result:
    Reverse DNS for not found.

    i am not sure about /etc/hosts if i need to modify that or not but i did some changes

    demo@Superman:~$ more /etc/hosts localhost Superman Superman localhost localhost.localdomain Superman

    not sure if it was a good idea to edit /etc/hosts at all :(

  • update - i checked after 5 min and now it reports

    CrDNS test result: resolved to resolved to;
    rDNS if forward confirmed.

    Dynamic IP test result: not looks like dynamic IP address.

    so this is all? should i test mail() now and see if it goes to gmail spam folder?

  • Yes, that’s all :) try it and let me know if it helps.

  • i can tell you that now my email arrives to INBOX :-))))
    it was just a droplet name i had to change to domain name
    i am just not sure if changing /etc/hosts helped too or not
    thank you 100x !!!

  • oh i realized something. i was testing it to more gmail accounts. suprisingly it goes into spam always, unless i later click “NOT SPAM” from that moment on emails from my domain will go to inbox. that means everyone else will receive it to spam. how can this happen if my domain is new, it has never been registered before? it looks like google things i am sending spam although no one ever used this domain :( what can i do to make sure emails to users will be delivered to inbox?

  • You can never be sure if a mail will go into inbox or spam. You can try to set SPF and DKIM (links in my first answer). Check if your IP address isn’t blacklisted somewhere. And it depends on the mail content, too. Spam filtering is a big issue nowadays.

  • i read other pages and they suggested i check if my IP is blacklisted. it seems that IP is on blacklist - do you think this is true? and if it is, how come, since i purchased droplet few months ago and i never use it to run big websites, had no traffic and i have never sent out any emails. any clues?

    if you run this, you will see it says blacklisted IP :(

  • i think i will go with other VPS hosting. digital ocean is cheap but its just a server. i wasted much more money tryng to configure this s**t - its better to pay $40 instead of $5 and everything is working. dissapointed, really. again i learned that cheap things are not good.

  • You are right, if you can’t set a mail server correctly, a server is not for you (whether it is 5$ or 500$ per month). Nobody said that it is easy. Yes, your IP is on the blacklist. And the reason is:

    This IP address is HELO'ing as "localhost.localdomain"
    which violates the relevant standards (specifically: RFC5321).

  • you are right, its much easier and for me - cheaper

    i am not sure what does that lcoalhost.localdomain mean… maybe this is because of the hosts file, that was present there

  • Hi kostolansky:

    I followed exactly the way as you suggested, but my mail still goes to junk box in hotmail.

    1. I renamed my droplet to my domain name, and checked the rDNS, which shows as correct, while when I type hostname in my droplet, it still shows the old name, and the email sent from there still goes into the junk box of any hotmail receiver. It goes into inbox in gmail though, but this was previously correct before I renamed my droplet.
    2. I checked the black list also, and my droplet’s IP is not there.

    Any idea to solve this? Already working on this for days but cannot figure it out. Thanks a lot!

  • Hi! Have you created SPF and DKIM records? It can help.

You’re PTR should coincide with your hostname, just ask DO to update your PTR.