I am getting pretty close to deploying m webap and am currently looking into ways to monitor exceptions once deployed. Django has the ability to email whenever it encounters a 500 error. I don’t really want any emails being sent out (yet) so I decided that I should probably just stick with local mail delivery until such time that I want to be notified right away of a problem.

I’ve set up local mail delivery before using Postfix but I always end up having to figure out how to do it over and over. This post is really going to be for me to remember how to do it but I figured that others may like to know about my methodologies for exception monitoring. The brilliant part about that is since I will be using a pretty beefy server, all my webapps will be able to use the same local email delivery so all my mails indicating problems will be in one place.

sudo apt-get install postfix

Under “General type of mail configuration” select Local only

System mail name: localhost (I will presume from here on out that you are sending email to @localhost)

Once complete, issue this command to tell postfix where to deliver mail:

sudo postconf -e "home_mailbox = Maildir/"

Now we install mutt:

sudo apt-get install mutt

And then configure mutt to know where our email will be delivered to and what kind of mailbox it will be:

vim ~/.muttrc

Paste this into the file:

set mbox_type=Maildir
set folder="~/Maildir"

set mask="!^\\.[^.]"
set mbox="~/Maildir"
set record="+.Sent"
set postponed="+.Drafts"
set spoolfile="~/Maildir"

Then start mutt so that it can create our mailbox.

mutt

You will probably get a message that says:

/home/USER/Maildir is not a mailbox.

Don’t worry about it for now. Postfix will fix that for us on first delivery.

This part is optional - set where any mail sent to root is forwarded to.

sudo vim /root/.forward

and in it, type:

USER@localhost

If you want all mail for root to be sent to the user mike, you’d write mike@localhost

Now you can test this out using python

import smtplib
import string
SUBJECT = "Test email"
TO = "root@localhost" # This is to test forwarding from root to another user. if you didn't set up forwarding, use your username
FROM = "python@localhost"
text = "blah blah blah"
BODY = string.join((
        "From: %s" % FROM,
        "To: %s" % TO,
        "Subject: %s" % SUBJECT,
        "",
        text
        ),
"\r\n")
server = smtplib.SMTP('localhost')
server.sendmail(FROM, TO,BODY)
server.quit()

Now you can send mail to any users on the system. If I ever want change the delivery from local to something that gets sent out, I can follow one of the many articles on the internet describing how to set up relaying through Gmail and set my local user’s .forward to an email (not verified, hypothesized).