User Tools

Site Tools


notes:postfix-stunnel-smtps

This is an old revision of the document!


Relaying mail with Postfix and Stunnel through an SMTPS smarthost on port 465

Do you have setup your own VPS and you want to send mail from it to the outside world without running a full-blown mail server? You can install Postfix and use it in satellite mode, relaying email to another mail server.

However, all mail services aren't equal. Ones like Gmail are sophisticated and support the more recent TLS protocol, while many shared and budget hosting services don't. The latter often use an older SSL protocol, and Postfix SMTP client does not support the obsolete “wrappermode” protocol, which uses TCP port 465 on the SMTP server. A solution is to create your own local SSL tunnel between Postfix and the relay server with stunnel.

Stunnel

Install stunnel with

sudo apt-get install stunnel

Install stunnel in Ubuntu or Debian with

sudo apt-get install stunnel

Enable it on startup by editing /etc/default/stunnel4 to

#ENABLED=0
ENABLED=1

Create a .conf file in etc/stunnel directory with

vim /etc/stunnel/stunnel.conf.

and paste the following text inside

[smtp-tls-wrapper]
accept = 11125
client = yes
connect = MY_SMTP_HOSTNAME:465

In this case, 11125 is our local port (but could be different). The connect line has the fully qualified domain name and port number at the external relay host (SMTP server). Check with your e-mail or web hosting provider if you're unsure. Cpanel has this information under “Email Accounts” in a “Configure Email Client” option:

Test your SMTPS tunnel with telnet localhost 10465. If everything is working, you should see the greeting line from your smarthost, something like “220 omta14.emeryville.ca.mail.comcast.net comcast ESMTP server ready”; type quit to disconnect.

mailutils postfix

With the smtp_sasl_password_maps parameter, we configure the Postfix SMTP client to send username and password information to the mail gateway server. As discussed in the next section, the Postfix SMTP client supports multiple ISP accounts. For this reason the username and password are stored in a table that contains one username/password combination for each mail gateway server.

/etc/postfix/sasl_passwd:
# destination                   credentials
[mail.isp.example]              username:password
# Alternative form:
# [mail.isp.example]:submission username:password

Important

Keep the SASL client password file in /etc/postfix, and make the file read+write only for root to protect the username/password combinations against other users. The Postfix SMTP client will still be able to read the SASL client passwords. It opens the file as user root before it drops privileges, and before entering an optional chroot jail.

  • Use the postmap command whenever you change the /etc/postfix/sasl_passwd file.
  • If you specify the “[” and “]” in the relayhost destination, then you must use the same form in the smtp_sasl_password_maps file.
  • If you specify a non-default TCP Port (such as “:submission” or “:587”) in the relayhost destination, then you must use the same form in the smtp_sasl_password_maps file.

In this example, I’m using the Comcast SMTP server as my smarthost — replace smtp.comcast.net:465 with the mail server you wish to use as a smarthost.

Install stunnel and Postfix with sudo apt-get install stunnel mailutils postfix

If you weren’t automatically prompted to configure Postfix, run sudo dpkg-reconfigure postfix to access the configuration wizard. Configure Postfix as a “Satellite system”. You must enter a valid domain name for “System mail name”, so use example.com. For “SMTP relay host”, enter [127.0.0.1]:10465

warning: SASL authentication failure: No worthy mechs found

sudo apt-get install libsasl2-modules

Stunnel configuration

Install stunnel in Ubuntu or Debian with…

sudo apt-get install stunnel

Enable it on startup by editing /etc/default/stunnel4:

#ENABLED=0
ENABLED=1

Create a .conf file in etc/stunnel directory. I named this one /etc/stunnel/stunnel.conf. 11125 is our local port. The connect line has the fully qualified domain name and port number at the external relay host (SMTP server). Check with your e-mail or web hosting provider if you're unsure. Cpanel has this information under “Email Accounts” in a “Configure Email Client” option:

[smtp-tls-wrapper]
accept = 11125
client = yes
connect: MY_SMTP_HOSTNAME:465

Test your SMTPS tunnel with telnet localhost 10465. If everything is working, you should see the greeting line from your smarthost, something like “220 omta14.emeryville.ca.mail.comcast.net comcast ESMTP server ready”; type quit to disconnect.

Postfix configuration

Put these lines in /etc/postfix/main.cf and be sure to comment out any earlier ones that compete with them. The relayhost is localhost (127.0.0.1), not the external relay server, because we will be creating a local tunnel for the SSL. Only smtp (client) settings need to be tweaked; the stmtpd (server) settings can be left alone, including the TLS configuration.

relayhost = [127.0.0.1]:11125
inet_interfaces = loopback-only
# SASL Settings
smtp_use_tls = yes
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl/sasl_passwd
smtp_sasl_security_options =

The SASL settings point to a password file, which we haven't yet created. Let's do that now, using the same email address from which we send messages. Change permissions (chmod) on the file to 600 so that your password can't be read by others:

The last step is to provide Postfix with your username and password for the smarthost. Without authentication,your smarthost mail server is not going to allow you to relay mail. Run sudo vim /etc/postfix/saslpass and add a line to the file:

[127.0.0.1]:10465 your-username@comcast.net:your-password

Convert the text-based password file to as hash-based file that Postfix can understand with

sudo postmap /etc/postfix/sasl/sasl_passwd

Restart both servers:

sudo service stunnel4 restart
sudo service postfix reload

Send a test message to an external e-mail account. I prefer mutt mail client; you might use the mail command instead:

At this point, the entire setup should be working. Try to send yourself a test message using echo This is a test message. | mail -s “Test Message” your-email-address@example.com and, optionally, monitor the Postfix mail log with tail -F /var/log/mail.log

http://www.postfix.org/SASL_README.html http://quietmint.com/linux/postfix-relaying-mail-through-an-smtps-smarthost-on-port-465/ http://tech.surveypoint.com/posts/relay-mail-with-postfix-and-stunnel/

notes/postfix-stunnel-smtps.1416748210.txt.gz · Last modified: 2014/11/23 13:10 by admin