I was running a Django server on Ubuntu 12.04 and saw a lot of errors logged in gunicorn's error log file:
One line of the error messages caught my eye, send_mail. Gunicorn is trying to send my the error message via the send_mail() function but failed. I realized that I didn't setup the email settings in settings.py.
I searched online for a solution and found two:
- send email via Gmail's smtp
- setup your mail server
Option 1 seems like a quick and dirty way to get things done, but it has a few drawbacks:
- Sending email via Gmail's smtp means the FROM field will be your Gmail address rather than your company or whatever email address you want it to be.
- To access Gmail, you need to provide your username and password. This means you have to store your gmail password either in the settings.py file or to be more discreet in an environment variable.
- You have to allow less secured app to access your gmail. This is a setting in your gmail's account.
I decided to setup my own mail server because I don't want to use my personal email for contacting clients. To do that, I googled and found that there are two mail servers that I can use:
Since postfix is newer and easier to config, I decided to use it.
- Install postfix and Find main.cf
Note: main.cf is the config for postfix mail server
Great, it is already installed. Then, I went to /etc/postfix/ to find main.cf and it is not there! Weird, so I tried to reinstall postfix:
After installation, I saw a message:
I see. So, I followed the instruction and copied the main.cf file to /etc/postfix/:
Add the following lines to main.cf:
Then, reload this config file:
Now, let's test to see if we can send an email to our mailbox via telnet:
Once connected, enter the following line by line:
If everything works out, you sould see a comfirmation message resemables this:
It is guaranteed that this email will end up in the spam box if you use Gmail. So take a look at your spam inbox to see if you received the test mail (it may take a minute to show up).
If you recevied the test email, then it means postfix is working properly. Now, let's config Django to send email via postfix.
First, I added the following line to my settings.py file:
Then, I opened up Django shell to test it:
Again, check your spam inbox. If you received this mail, then it means Django can send email via postfix, DONE!