Use external SMTP server for system mails on Linux

To use an external SMTP for all system e-mails, you have to install these:

sudo apt-get install msmtp msmtp-mta

Where msmtp-mta transform the external reference in the sendmail command usable by any application using sendmail. In this way you haven’t to install and configure Postfix since you’ll rely on an external SMTP service.

Create the config file for msmtp

This is an example based on the popular Gmail by Google:

# Example for a system wide configuration file
# /etc/msmtprc

# A system wide configuration file is optional.
# If it exists, it usually defines a default account.
# This allows msmtp to be used like /usr/sbin/sendmail.
account default
aliases /etc/aliases

# The SMTP smarthost.
# host mailhub.oursite.example

# Construct envelope-from addresses of the form "user@oursite.example".
#auto_from on
#maildomain oursite.example

# Construct envelope-from addresses of the form "user@oursite.example".
# this fix the error: msmtp: account default from /etc/msmtprc-php: envelope-from address is missing
### auto_from on

# Use TLS.
tls on
auth on
tls_trust_file /etc/ssl/certs/ca-certificates.crt

# Syslog logging with facility LOG_MAIL instead of the default LOG_USER.
syslog LOG_MAIL

host smtp.gmail.com
port 587
from senderaddress@example.com
user realaccount@example.com
password GMAIL-AUTH-PASSWORD

Replace these with the real data from your e-mail account.

In this example, the realaccount@example.com in GMail is the user that created the Gmail app password. He has to have the senderaddress@example.com configured as sender address alias in GMail.

Add aliases

To match local users with sender address, create the aliases file:

nano /etc/aliases

# See man 5 aliases for format
# postmaster: root
postmaster: senderaddress@example.com
root: senderaddress@example.com
default: senderaddress@example.com

If you’ve any process sending emails using a specific username, add to this list with the right email to use. Any occurrence of the original address will be translated to the right address.

First test

Type this to test the new configuration:

sendmail alertrecipient@example.com
WRITE YOUR TEXT
Ctrl+D

Additional step

If you need to use the mail command, install mailutils without installing postfix:

sudo apt-get install --no-install-recommends mailutils

Then you can use something like:

echo -e Print a variable here $MYVAR.\n\n– \nSign here | mail -s Type your subject here $EMAIL

Here some variables are added as you can use them in a custom script.

Using msmtp command directly

If you already use the base mail command and you don’t want to replace it with mail, you can use this:

(echo -e Subject: Type your subject here; echo; echo -e “Print a variable here $MYVAR.\n\n– \nSign here) | msmtp -a default $EMAIL

Done

Now any application using sendmail will actually use your external SMTP service. Use a mail server supporting TLS to avoid transmitting clear text filled with system information.

Tested on Ubuntu Linux 18

Advertisements

Apache CentOS 6 cannot send email and Drupal get HTTP request status fails

I’m installing a Pressflow 6 on a new machine running CentOS 6. I’m using Apache MPM Worker with FastCGI. Then I get the classical e-mail error:

Unable to send e-mail. Please contact the site administrator if the problem persists.

Then I try to use sendmail:

sendmail -v yourmail@example.com < testmail

Where testmail is a file containing these lines:

Subject: test mail Ozu
Yasujiro Ozu
[blank line here]

And i get the message. PHP cannot send email through apache!

Trying a simple php script to send mail like drupal core do I got this error:

sendmail: fatal: chdir /var/spool/postfix: Permission denied

Then I check this variable following this awesome post:

# /usr/sbin/getsebool httpd_can_sendmail
httpd_can_sendmail --> off

Enable httpd_can_sendmail solve this issue:

setsebool -P httpd_can_sendmail 1

And wait. It will be a long wait using the -P option. And then PHP and Drupal can send mail.

Then check again the variable:

# /usr/sbin/getsebool httpd_can_sendmail
httpd_can_sendmail --> on

Now httpd can send mail. Try your script again.

The SMTP Authentication Support module is not working. This is another of these variables, the same that causes Drupal to show the “HTTP request status fails” message.

setsebool -P httpd_can_network_connect 1

And wait again. Both the SMTP module and the base Drupal networking are now working and Status report is all green.