Postfix error: Host or domain name not found

Shorter answer.

With a “Host not found, try again.” it could mean that your server is having DNS resolution issues, or Postfix itself is having DNS resolution issues. But that is the core of what has to be cleaned up.

As for how to clear it up, your question doesn’t provide enough details on your base OS or setup to give a succinct answer. You seem to be running Ubuntu/Debian if you are running sudo service postfix restart, but what other details can you provide?

Longer answer.

Possibly connected to resolv.conf mysteriously disappearing.

Based on the error:

Host or domain name not found. Name service error for name=smtp.gmail.com type=A: Host not found, try again.

It seems like you are using Gmail’s SMTP servers for outgoing mail. Unsure what your base OS is, but based on this answer on Ubuntu Forums this seems to be an issue with a missing resolv.conf on reboot; bold emphasis is mine:

I have the same problem on Ubuntu 14.04 LTS. When postfix is installed it copies /etc/resolv.conf to /var/spool/postfix/etc/resolve.conf. but for some reason after a reboot/restart the file is not copied and you get the blank file with only the header.

The suggested temporary solution is to copy the main OS resolv.conf to Postfix’s directory:

sudo cp /etc/resolv.conf /var/spool/postfix/etc/resolv.conf

This isn’t a permanent fix it seems but at least it’s something to check out.

Possibly connected to inet_protocols settings.

Also this answer on Server Fault explains a similar situation which focuses on IPv6 support. Pretty sure your issue is not IPv6-related since the error is about a “type=A” record which is IPv4 and for IPv6 it would be “type=AAAA.” But that said, it might worth experimenting with adjusting the inet_protocols setting in your Postfix config file. Here is what the official Postfix documentation explains:

When IPv4 support is enabled via the inet_protocols parameter, Postfix will look up DNS type A records, and will convert IPv4-in-IPv6 client IP addresses (::ffff:1.2.3.4) to their original IPv4 form (1.2.3.4). The latter is needed on hosts that pre-date IPV6_V6ONLY support (RFC 3493).

When IPv6 support is enabled via the inet_protocols parameter, Postfix will do DNS type AAAA record lookups.

When both IPv4 and IPv6 support are enabled, the Postfix SMTP client will choose the protocol as specified with the smtp_address_preference parameter. Postfix versions before 2.8 attempt to connect via IPv6 before attempting to use IPv4.

Examples:

  • inet_protocols = ipv4
  • inet_protocols = all (DEFAULT)
  • inet_protocols = ipv6
  • inet_protocols = ipv4, ipv6

Assuming you are on an Ubuntu/Debian setup open up the config here:

sudo nano /etc/postfix/main.cf

And find—or set the value—to the inet_protocols line to be either this:

inet_protocols = ipv4

Or be this:

inet_protocols = all

Then restart the Postfix service—or the whole server—and see if that clears things up:

sudo service postfix restart

The problem is that Postfix checks /etc/resolv.conf before the WiFi is connected. Therefore, /var/spool/etc/postfix/resolv.conf stays empty after the boot and mails cannot be sent.

To solve the problem, I disabled postfix service:

sudo update-rc.d postfix disable

…and I wrote this script to wait the end of the Wi-Fi connection before to start Postfix (saved in /usr/local/bin/postfix_wifi.sh):

#!/usr/bin/sh

host="smtp.gmail.com"
port=587

until nc $host $port -w 5
do
    sleep 10
done

sudo service postfix start
exit 0

…and I added this line to /etc/rc.local so that the script is run at boot:

/usr/local/bin/postfix_wifi.sh &