Email sending issue with “localhost.localdomain”

emailsendmail

I have a DigitalOcean server which runs Ubuntu 14.04. I am having problems with sending emails from the PHP mail() function (which uses sendmail internally).

I think the issue may be related to my hosts file config. Here is what I have in /etc/hosts at the moment:

127.0.0.1 localhost localhost.localdomain ip-xxx-xxx-xxx-xxx

And in etc/hostname:

ip-xxx-xxx-xxx-xxx

(In the above two, I replaced the IP address digits with x)

Now I have a domain pointing to this server, let's call this mydomain.com.

So when my website mydomain.com sends an email, the email is going in to the junk mail folder. I ran a test on https://www.mail-tester.com and one of the issues it flags up is:

enter image description here

I have tried adding mydomain.com to the line above in the hosts file but this results in the email arriving after a long time or not arriving at all.

Here are the Received headers:

Received: from localhost.localdomain (unknown [xxx.xxx.xxx.xxx])
    (using TLSv1.2 with cipher xxx (256/256 bits))
    (No client certificate requested)
    by mail-tester.com (Postfix) with ESMTPS id xxx
    for <test-xxx@mail-tester.com>; Mon, 29 Apr 2019 18:35:18 +0200 (CEST)

Received: from localhost.localdomain (localhost [127.0.0.1])
    by localhost.localdomain (8.15.2/8.15.2/Debian-3) with ESMTP id xxx
    for <test-xxx@mail-tester.com>; Mon, 29 Apr 2019 17:35:18 +0100

Received: from mydomain.com (www-data@localhost)
    by localhost.localdomain (8.15.2/8.15.2/Submit) with SMTP id xxx
    for <test-xxx@mail-tester.com>; Mon, 29 Apr 2019 17:35:18 +0100

Can someone please advise what the issue may be and how to fix?

Best Answer

Nobody cares about hops inside your infrastructure and how many localhosts are passed. But be sure to let your outgoing server say "Hello, I'm mail.domain.tld" instead of "Hello, I'm localhost.localdomain", because no halfway properly configured server will be interested to talk to unknown mailservers.

I see in your referenced headers that you use postfix(1) for processing outbound emails. Look for the variable myhostname inside your main.cf (usually located in /etc/postfix or /usr/local/etc/postfix) and fill it properly. Example: myhostname=mail.domain.tld (replace it with your mailservers name). Also be sure that smtp_banner= contains at least $myhostname like smtp_banner=$myhostname ESMTP Postfix.

See this for additional info.