Postfix on Debian

These notes are geared for using it as a simple relay, for instance on a web server that needs to send out mail notices to registered users.

In this setup, the web server will not talk directly to other domains, or accept incoming connections. It will instead use am established remote SMTP server to relay mail (a company-run or ISP mail server, or even GMail) that supports authentication and SSL. We don’t want problems with blacklists, talking to AOL/Earthlink, open relays and other headaches associated with running a mail server.

In general, I find Postfix easier to understand and configure than EXIM (the Debian default for ages), and QMail (which I used for years, and still use for primary mail servers).


$ sudo aptitude install postfix libsasl2 libsasl2-modules

When the install runs you will be asked how to configure the host. Select local only. You will then be asked for the hostname.


The main configuration file is in /etc/postfix/ These are the important settings to configure a local relay. We don’t want the web server to accept outside mail or keep mailboxes. Outgoing messages are delivered to another mail server, via SSL. Notice that relayhost is blank.

myhostname =
mynetworks =
mailbox_size_limit = 0
recipient_delimiter = +               
inet_protocols = all
relayhost = 
inet_interfaces =

Make sure the following defaults are commented out:

# default_transport = error
# relay_transport = error

Sasl setup

Sasl is useful if you are running a relay and want to connect to an SSL-based SMTP server. First you need to edit /etc/postfix/sasl_passwd and put in your relay info. It might look like this:

$ cat /etc/postfix/sasl_passwd

Add this to /etc/postfix/

smtp_sasl_security_options = noanonymous 
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd 
smtp_sasl_auth_enable = no

Then you have to ensure the permissions are correct for that file, and rebuild the “hash”:

$ sudo chown root:root /etc/postfix/sasl_passwd 
$ sudo chmod 600 /etc/postfix/sasl_passwd 
$ sudo postmap hash:/etc/postfix/sasl_passwd 

To delete a bunch of messages to/from a person in the queue

It’s possible that messages may get backed up, maybe from a code error or something. Here’s how to clear them out.

$ mailq | tail +2 | awk 'BEGIN { RS = "" } / { print $1 } ' | tr -d '*!' | postsuper -d -


The postconf utility lets you change settings via the command-line:

sudo postconf -e 'inet_interfaces ='


After changing things, here's the proper way to restart:

$ sudo /etc/init.d/postfix reload

Rails and Ruby Enterprise Edition

If you have problems sending mail and get a OpenSSL::SSL::SSLError: hostname was not match with the server certificate error, try turning off tls in /etc/postfix/


Test script

Here’s a Ruby test script that can be run at the webserver to test that Postfix and local relay is working correctly. If the messages comes through ok, examine the mail headers to make sure your relay host was used.

require 'net/smtp'

def send_to(to)
  message = <<MESSAGE_END
  From: Foz <#{FROM}>
  To: Myself <#{to}>
  Subject: SMTP e-mail test

  This is a test e-mail message. It Worked!

  Net::SMTP.start('localhost') do |smtp|
    smtp.send_message message, FROM, to