When building a web app that will have registered users, you will need to have a way to send email to users during sign-up. The number one reason is to confirm that their email address is valid. Another reason is to have a “forgot password” feature.
If you are on a shared hosting, there is no need to set this up. But if your on a VPS, you may need to setup your own SMTP server. Now, there are many options out their, like Amazon SES and Mailgun, but they will cost you a little bit of extra cash.
In my case, I wanted to try out sendmail on a Digital Ocean droplet. Although this doesn’t cost a dime, it will cost more time to setup! To save you some trouble, here’s what I did:
Login into your droplet and do the following:
- Update ubuntu
sudo apt-get update
- Install sendmail
sudo apt-get install sendmail
Find this line:
Make sure that it looks like this:
127.0.0.1 localhost.localdomain localhost yourhostname
Change yourhostname to the name of your droplet.
- Run Sendmail’s config and answer ‘Y’ to everything:
- Type this in the console:
echo "test message" | sendmail -v firstname.lastname@example.org
Change email@example.com to the receiving email address.
- Check your inbox. Most probably its in the spam folder if your domain has no reputation yet.
Why is Sendmail So Slow?
This happened while I was testing. Sendmail is very slow, an upwards of 60 secs in sending a single mail!
To solve it you need to make sure that you change the localhost entry in /etc/hosts. In Digital Ocean it has an additional entry:
Comment that out and only have a single 127.0.0.1. See number 3 in the install section above.
Using It In A Web App
In my case, I built my app with nodejs. I installed nodemailer and use the sendmail transport. The docs are pretty straightforward. If you are on PHP you can just use the mail() function.
That’s it for now.