Scripting with Postfix/mail

Sometimes you’re automating tasks that requires you to be informed of changes at the moment they’re occurring (or at least, not too long after). Turns out if you’re planning only to send mail, it takes abouts 3 minutes to setup postfix and send your first automate mail message!

First, you have to install postfix and mailutils, two packages that are fortunately already (likely) installed and readily available from the default repository. In a shell, type:

sudo apt-get install postfix mailutils

This will launch the install script for postfix and provide a couple of mail manipulating commands. (If the install script does not prompt you, run sudo dpkg-reconfigure postfix just after the install finishes.) The actual questions you get depends on the version of postfix and/or the distro you’re using, but there are only three that you should change to get the thing to work.

First, pick “Internet with smarthost”.

Input the “mail name”, the domain name that will suffix your username by default. For example, if your user is karl and your domain is toaster.com, the sender will appear as karl@toaster.com.

The SMTP relay host name really depends on your service provider. It is likely something like relay.myprovider.com.

The root and postmaster recipient should be the username you’re using to send mails; for example, if you run your scripts as user buildbot, input buildbot.

In other destinations, you should see the SMTP host in the list of default locations. Just press OK if it’s there.

Then select default suggestions until you reach the end of the script—just like a typical Windows install: agree, yes, ok, ok, yes, install, ok, yes, later, yes, tl;dr, yes install dammit, except that there are only 3-4 questions left.

Finally, a script command such as:

mail -s "${subject}"  ${user} < ${body_file}

will send a mail to the user ${user}. Or you can send to a list of recipients using:

mail -s "${subject}" ${users[@]} < ${body_file}

(The user list is necessarily the last arguments.) It may be better to send individual mails if you do not want the headers to show the other recipients.

And voilà, you’re ready to get automated mailing from your custom scripts. If you’re using some other system, say, Hudson, just use its facilities. mail and sendmail commands are really not that fun to work with.

One Response to Scripting with Postfix/mail

  1. […] you will need to install postfix and mailutils as we discussed here and also package curl. All these are in Ubuntu’s default repositories, and should be easy to […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: