Hi

I have successfully installed WordPress on my DigitalOcean VPS. I am using an Ubuntu 20.04 server with a LAMP stack and the addition of PHPMyAdmin. I have also opted to install PHP-FPM over the standard mod_php that is configured by default with Apache 2.4, as it offers better performance at a cost of slightly more RAM.

I’m now trying to get the email side of things working in WordPress, but I’m not having any luck. I have a Google Workplace (formerly G Suite) plan that provides an email address at my registered FQDN, and I have MX DNS records pointing towards Google’s G Suite. My two emails accounts are as follows:

I want to send emails from my server in a couple of ways from either of the two emails above to myself or at a personal email address.

Firstly, I have setup a cron job that automates a WordPress and MySQL database backup and uploads it to the cloud in a compressed archive. This is something I wish to log and email to myself at webmaster@example.com to say it has complete.

Secondly, and most importantly, I need to be able to send emails from WordPress, such as submission results from contact forms and sending out emails to register new WordPress members. I would like to receive the emails at the Administrator email set in WordPress which currently is also webmaster@example.com

WordPress uses wp_mail similar to PHP’s mail function as stated here

Now after reading through guides on the internet including some of the tutorials here on DigitalOcean, the majority of guides recommend using Postfix as the MTA (Mail Transfer Agent) with Linux. With this in mind I have installed Postfix and setup Google G Suite to use ‘less secure apps’. I have configured Postfix with the following:

# Install Postfix
debconf-set-selections <<< "postfix postfix/mailname string example.com"
debconf-set-selections <<< "postfix postfix/main_mailer_type string 'Internet Site'"
sudo apt-get install libsasl2-modules mailutils postfix -y

/etc/postfix/main.cf

# See /usr/share/postfix/main.cf.dist for a commented, more complete version

# Debian specific: Specifying a file name will cause the first
# line of that file to be used as the name. The Debian default
# is /etc/mailname.
#myorigin = /etc/mailname

smtpd_banner = $myhostname ESMTP $mail_name (Ubuntu)
biff = no

# appending .domain is the MUA's job.
append_dot_mydomain = no

# Uncomment the next line to generate "delayed mail" warnings
#delay_warning_time = 4h

readme_directory = no

# See http://www.postfix.org/COMPATIBILITY_README.html -- default to 2 on
# fresh installs.
compatibility_level = 2

# TLS parameters
smtpd_tls_cert_file=/etc/ssl/certs/ssl-cert-snakeoil.pem
smtpd_tls_key_file=/etc/ssl/private/ssl-cert-snakeoil.key
smtpd_tls_security_level=may

smtp_tls_CApath=/etc/ssl/certs
smtp_tls_wrappermode = yes
smtp_tls_security_level = encrypt
smtp_tls_session_cache_database = btree:${data_directory}/smtp_scache

smtpd_relay_restrictions = permit_mynetworks permit_sasl_authenticated defer_unauth_destination
myhostname = example.com
alias_maps = hash:/etc/aliases
alias_database = hash:/etc/aliases
mydestination = $myhostname, example.com, localhost
relayhost = [smtp-relay.gmail.com]:587
mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128
mailbox_size_limit = 0
recipient_delimiter = +
inet_interfaces = all
inet_protocols = all

# Enable SASL authentication
smtp_sasl_auth_enable = yes

# Disallow methods that allow anonymous authentication
smtp_sasl_security_options = noanonymous

# Location of sasl_passwd
smtp_sasl_password_maps = hash:/etc/postfix/sasl/sasl_passwd

# Enable STARTTLS encryption
smtp_tls_security_level = encrypt

# Location of CA certificates
smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt

/etc/postfix/sasl/sasl_passwd

[smtp-relay.gmail.com]:587 webmaster@example.com:password1234

Create the hash db file for Postfix

sudo postmap /etc/postfix/sasl/sasl_passwd

Secure Postfix hash database and email password files

sudo chown root:root /etc/postfix/sasl/sasl_passwd /etc/postfix/sasl/sasl_passwd.db
sudo chmod 0600 /etc/postfix/sasl/sasl_passwd /etc/postfix/sasl/sasl_passwd.db

My Apache server is running under the www-data user account and this appears in the /var/log/mail.log file

Oct 10 14:42:39 example postfix/pickup[17456]: 452783FF9A: uid=33 from=<www-data>
Oct 10 14:42:39 example postfix/cleanup[17673]: 452783FF9A: message-id=<k0SIaGCJN9mZHjmUQvGABJZK1SVxB0iUjrMAzkaE4Ok@example.com>
Oct 10 14:42:39 example postfix/qmgr[17457]: 452783FF9A: from=<www-data@example.com>, size=993, nrcpt=1 (queue active)
Oct 10 14:42:39 example postfix/local[17675]: 452783FF9A: to=<info@example.com>, relay=local, delay=0.04, delays=0.02/0.01/0/0.01, dsn=5.1.1, status=bounced (unknown user: "info")
Oct 10 14:42:39 example postfix/cleanup[17673]: 4CC483FF9B: message-id=<20201010134239.4CC483FF9B@example.com>
Oct 10 14:42:39 example postfix/qmgr[17457]: 4CC483FF9B: from=<>, size=2961, nrcpt=1 (queue active)
Oct 10 14:42:39 example postfix/bounce[17676]: 452783FF9A: sender non-delivery notification: 4CC483FF9B
Oct 10 14:42:39 example postfix/local[17675]: 4CC483FF9B: to=<www-data@example.com>, relay=local, delay=0.01, delays=0/0.01/0/0, dsn=2.0.0, status=sent (delivered to mailbox)
Oct 10 14:42:39 example postfix/qmgr[17457]: 4CC483FF9B: removed
Oct 10 14:42:39 example postfix/qmgr[17457]: 452783FF9A: removed

There is talk of solutions here on stackoverflow here

Could someone shed some light here please?

Submit an answer

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!