When I finished writing all the code for this website, I began researching the best way to send email from my VPS-hosted domain where the Django app lives. My goal was twofold; first I wanted the ability to send email from my app from any <user>@danhayes.dev email address I wanted. Second, I wanted to be able to receive email at one or more user accounts on my domain. In this post, I will go over a few of the various options for sending and receiving email on your domain.
The first option I explored to start sending email from my domain was Postfix. This was mainly because it was the first result when I Googled “sending email from server”. It turns out Postfix is only one piece of the puzzle if you want to do things the hard way and set everything up yourself. Author edit: This is not entirely true. I have since found that it’s possible to set up a send-only mail server using Postfix, but this will not be covered in this guide. I may write a follow up blog post utilising this. You see, the process of sending an email takes many steps, with many different services and software components, that each require their own intricate setup. To properly set up a Postfix mail server, you need to configure at minimum: Postfix MTA (Mail Transport Agent), an MDA (Mail Delivery Agent), and an IMAP (Internet Message Access Protocol)/POP3 (Post Office Protocol) server. You would probably also want a spam filter, antivirus, and a webmail client as well to round out the feature-set. As you can see the set up for a full and feature rich DIY mail server is not exactly trivial.
Enter G Suite
Having set Postfix aside for the moment because of the setup and maintenance required, I began researching other alternatives. The next logical step is to use an email service which has the ability to send email through an API (Application Program Interface). Many different email services are out there, each with their own advantages and disadvantages. I have been a long-time user of Gmail, so naturally I looked there first. Setting up Python to use the Gmail API is fairly easy, and you can begin sending messages in short time from your personal email address. However, if you want to send email from your own domain while making use of the Gmail service and features, you have to set up a G Suite account. Note however, that if you do go through the trouble of setting up an email server with Postfix and accompanying softwares, you could set up a free Gmail account to pull in the mail from that server, and even send mail from the server using an alias. This is where I hit my limitation with using G Suite to send emails from the server. I wanted to be able to send email from any arbitrary username on my domain, not just the one I had set up an account for. Not only would each username require its own setup, you’d also have to pay for each additional email address. The G Suite Gmail app is intended to be used as a webmail portal to your email account on a domain, not as a mass email sending service.
Gmail almost fit my needs, and I probably could have stopped there if I chose to use my G Suite user account to both send and receive email from the contact form, but this seemed both inelegant and wrong, so I turned to my final option: Mailgun. Mailgun is a mail service designed for use with its API. To send email, Mailgun needs a domain to work from. When you create an account with Mailgun you are given what’s called a sandbox domain that you can use to test, which sends mail from a subdomain of Mailgun itself. This sandbox domain is limited to sending email to the email address you signed up to Mailgun with, and up to 5 verified recipients, who have opted in to receiving emails from the sandbox. To bypass this restriction and send up to 10,000 emails a month for free, you have to set up and verify a domain you own. This is perfect, since this is what my use case is anyway. Sending more emails per month is possible as well for a price, but for this website, I’m not expecting to need to send more than 10,000 emails in a month.
If you want to send email from your own domain, there are a wealth of options to choose from. What you pick will ultimately depend on how much time you want to spend setting it up, and what your specific needs are. This post was meant purely as an introduction to sending email from a domain you own. In future posts, I will go over the server setup for Mailgun, Gmail, and tying it all together inside your Django app to be able to send email from your web app.