Follow this Blog!

Dec 4, 2018

f Comment

CentOS Postfix Fixing "Host or domain name not found" Error in SECONDS!

Amazon If you are pulling your hair out trying to figure out why Postfix doesn't work on your CentOS Linux system, you've come to the right place.

In your mail log at /var/log/maillog you see errors looking like this:

postfix/smtp: to=, relay=none, delay=0.04, delays=0.03/0.01/0/0, dsn=4.4.3, status=deferred (Host or domain name not found. Name service error for name=smtp.elasticemail.com type=AAAA: Host not found, try again)

postfix/error: to=, relay=none, delay=0.01, delays=0.01/0/0/0, dsn=4.4.3, status=deferred (delivery temporarily suspended: Host or domain name not found. Name service error for name=smtp.elasticemail.com type=A: Host not found, try again)

postfix/smtp: to=, relay=none, delay=2709, delays=2709/0.03/0/0, dsn=4.4.3, status=deferred (Host or domain name not found. Name service error for name=smtp.elasticemail.com type=A: Host not found, try again)

If you use a different relay host such as smtp.gmail.com, then the message will contain the words "name=smtp.gmail.com".

Some forums talk about messing with your DNS settings in /etc/resolv.conf or /var/spool/postfix/etc/resolv.conf, but don't do it. First you don't need to. Second, you can't find /var/spool/postfix/etc/resolv.conf on your CentOS operating system anyway.

Let's read on on how to fix your "Host or domain name not found. Name service error for name=xxx type=A: Host not found, try again" or "Host or domain name not found. Name service error for name=xxx type=AAAA: Host not found, try again" error.

My Centos version (via "cat /etc/centos-release"): CentOS Linux release 7.5.1804 (Core)
My postfix version (via "postconf -d | grep mail_version"): mail_version = 2.10.1
My PHP version (via "php -v"): PHP 5.5.38 (cli) (built: Sep 14 2018 10:46:10)

Solution

Log into your CentOS box as root. Let's assume we are using smtp.elasticemail.com:2525 as our external SMTP server which requires authentication. Also, let's assume when we send emails, the "From" email address will be set to from@email.com.

Install Postfix.

# yum install -y postfix cyrus-sasl-plain

Remove Sendmail if any.

# yum erase -y sendmail*

Open /etc/postfix/main.cf for editing and add the following lines:

myhostname = email.com
relayhost = [smtp.elasticemail.com]:2525
smtp_sasl_auth_enable = yes
smtp_sasl_security_options = noanonymous
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd

The value of myhostname should match the part past @ of the "from" email address.

Having the wrong content in /etc/postfix/main.cf is one of the causes of the "Host or domain name not found" error.

Create /etc/postfix/sasl_passwd file and add the authentication details of the external SMTP server you use:

[smtp.elasticemail.com]:2525 user@example.com:password

Your SMTP server provider will provide you with user name and password.

Prevent non-root access.

# chmod 0600 /etc/postfix/sasl_passwd

Create a database file.

# postmap /etc/postfix/sasl_passwd

Restart Postfix service.

# postfix stop
# postfix start

That's it! Let's create some PHP code that sends some mail. Your PHP code may look like this:
$headers = 'Content-type: text/html; charset=utf-8' . "\r\n".
    "From: from@email.com" . "\r\n" .
    "Reply-To: from@email.com" . "\r\n";

mail('destination@email.com', '=?utf-8?B?'.base64_encode('some subject which can include UTF8 characters').'?=', 'some message which can include UTF8 characters', $headers, "-f from@email.com")
Run the PHP code. A success message of sending out a mail in /var/log/maillog looks like this:

postfix/pickup: uid=0 from=
postfix/cleanup: message-id=
postfix/qmgr: from=, size=439, nrcpt=1 (queue active)
postfix/smtp: to=, relay=smtp.elasticemail.com[xxx.xxx.xxx.xxx]:2525, delay=1.6, delays=0.03/0.01/1/0.51, dsn=2.0.0, status=sent (250 OK)
postfix/qmgr: removed

Questions? Let me know!
Please leave a comment here!
One Minute Information - by Michael Wen
Find Michael on Google or Facebook
ADVERTISING WITH US - Direct your advertising requests to Michael