Use Mailx with SMTP on CentOS to Send Email and Fix WordPress Mail Delivery

Many VPS environments use stripped-down Linux images that do not include a working mail-sending stack. When that happens, the server cannot deliver notification email properly, and features that depend on email, such as password recovery or account confirmation, stop working.

WordPress has plenty of SMTP plugins that can solve this problem on a per-site basis, but that quickly becomes tedious if the same server hosts a large number of WordPress installations. A cleaner fix is to enable SMTP sending at the server level. One simple tool for that job is Mailx.

Install Mailx

Using CentOS as an example, Mailx is very easy to install with yum. Before installing it, it is a good idea to remove old packages such as Postfix or Sendmail if they are no longer needed and might conflict with the setup.

yum -y remove postfix
yum -y remove sendmail

Then install Mailx:

yum -y install mailx

Configure SMTP mail sending

Most mail providers offer several ways to send mail, usually plain SMTP, SMTPS, and SMTP with STARTTLS. Plain SMTP is the simplest but also the least secure, so in practice it is better to use SMTPS or STARTTLS. In the following examples, QQ Mail is used, but the same pattern works for other providers too. Mailx reads its configuration from /etc/mail.rc. Edit that file and choose one of the following configurations.

SMTPS configuration

set nss-config-dir=/etc/pki/nssdb
set ssl-verify=ignore
set smtp=smtps://smtp.qq.com:465
set smtp-auth=login
set smtp-auth-user=xxx@qq.com
set smtp-auth-password=xxx
set from=xxx@qq.com

STARTTLS configuration

set smtp-use-starttls
set nss-config-dir=/etc/pki/nssdb
set ssl-verify=ignore
set smtp=smtp.qq.com:587
set smtp-auth=login
set smtp-auth-user=xxx@qq.com
set smtp-auth-password=xxx
set from=xxx@qq.com

After saving one of the configurations above, test sending an email with the following command. If everything is configured correctly, the mailbox should receive the test message quickly.

echo "Hello" | mail -v -s "test" xxx@qq.com

With the settings above, email can already be sent successfully, but you may still see the warning “Error in certificate: Peer’s certificate issuer is not recognized.” To fix that, generate the required certificate data with the following commands.

mkdir -p /root/.certs/
echo -n | openssl s_client -connect smtp.qq.com:465 | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > ~/.certs/qq.crt
certutil -A -n "GeoTrust SSL CA" -t "C,," -d ~/.certs -i ~/.certs/qq.crt
certutil -A -n "GeoTrust Global CA" -t "C,," -d ~/.certs -i ~/.certs/qq.crt
certutil -L -d /root/.certs

After generating the certificate store, update the mail.rc configuration and change nss-config-dir to the generated /root/.certs path.

With the same pattern, you can also use providers such as 126 or 163 Mail to send email from the server through SMTP. Once it is configured successfully, every WordPress site hosted on that server, along with any other application written in any other language, can send mail directly through the same server-level setup.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *