How I Resolved My Ubuntu Sendmail Woes
I’ve hosted my sites with 1and1 for years now and after countless issues with them (mainly their woeful support) I’m finally taking the time to move my sites away.
When investigating the options I read that there is a fair bit of server configuration required to set their droplets setup. However, with a combination of my average (but improving) command line skills, and their excellent documentation, everything so far has gone without a hitch.
That is, everything but setting up sendmail…
The majority of the sites that we host contain a contact form of some sorts, which uses an MTA (Mail Transfer Agent) like sendmail to send the emails. When you go to most hosting companies and purchase a server everything comes pre-installed and works out the gate. With a DigitalOcean droplet you need to install and configure everything (yes, everything) yourself.
After getting Ubuntu installed and a LEMP stack set up, the last thing on my list was to install sendmail so emails would get sent out from the server.
I must’ve spent about a day in total trying to get it to work; reading countless forums threads, posting my own questions… all without a resolution. And finally… finally I managed to get it working. To save others in a similar situation I wanted to go through all the steps I went through to get this to work.
Note: Throughout the steps below I’ll be using the domain mysite.co.uk as an example. Wherever this is referenced you should swap it out for your own domain.
Step 1: Install sendmail
Obviously we need to begin by installing sendmail. To do this we can simply run the following two commands:
sudo apt-get update sudo apt-get install sendmail
Note: at this point you’re recommended to run the ‘sudo sendmailconfig’ command but we’ll come to that a little later.
Step 2: Check /etc/hostname
When I setup the droplet I stupidly entered the hostname not as an actual qualified domain, but the name of the site. So, for example, I named it ‘MySite‘ instead of ‘mysite.co.uk‘.
If you’ve done this too you can change the hostname on your server by running the following command:
sudo nano /etc/hostname
In this file there should be one line containing the hostname. Make sure it’s correct and if it isn’t, amend it accordingly. Then save and close the file.
Step 3: Check /etc/hosts
Next is to check that we have the correct host entries. Run the following command:
sudo nano /etc/hosts
For the record my working one looks exactly like so:
127.0.1.1 mysite.co.uk www.mysite.co.uk 127.0.0.1 localhost localhost.localdomain mysite.co.uk
I’m not sure what the 127.0.1.1 entry does but I didn’t want to remove it just in case it was related to something else. I’ve read posts by people saying they’ve removed it but I didn’t want to add to my list of problems. One thing at a time.
Step 4: Check /etc/mail/local-host-names
Similar to steps 2 and 3 above, we need to make sure that the correct domain name is set in our /etc/local-host-names file. We can do this by running the following:
sudo nano /etc/mail/local-host-names
Mine contains two entries:
Again, amend accordingly if yours doesn’t contain the hostname.
Step 5: Restart services
Following any hostname changes lets restart a few things:
sudo /etc/init.d/networking restart sudo service hostname restart
I’m not sure what they do, if anything, but I figured it doesn’t do any harm to restart stuff. We want to be 100% sure everything is configured as it should be, especially if we’ve had to make changes in any of the files from the steps above.
Step 6: Configure sendmail
Nice and easy. Just one simple command to run to setup sendmail as follows:
You’ll be prompted with about three questions which you can just hit enter on.
Note: At this point I had some errors output along the lines of FEATURE() being before MAILER(). Apparently that’s fine so don’t worry if you see any errors like that.
Step 7: Test
Time to test. Try and send an email like so:
echo "This is the body of the email" | mail -s "This is the subject line" firstname.lastname@example.org
Once executed there are two things to check:
1. Did you receive the email?
2. Were there any errors in the maillog? You can check the maillog by running the following:
sudo tail -f /var/log/mail.log -n 20
If you got the email we’re looking good.
Attention! Read this…
When I tried the test in step 7 I used my own personal Hotmail address. The email was received (albeit in spam) and I was ecstatic. However…
It later turned out that emails sent to the same domain as the hostname (i.e. email@example.com) still weren’t working. After checking the maillog I saw the following:
Nov 20 08:42:12 MySite sendmail: tAKDgCIM028155: from=www-data, size=645, class=0, nrcpts=1, msgid=
, relay=www-data@localhost Nov 20 08:42:12 MySite sm-mta: tAKDgCeX028156: ... User unknown Nov 20 08:42:12 MySite sendmail: tAKDgCIM028155: firstname.lastname@example.org, ctladdr=www-data (33/33), delay=00:00:00, xdelay=00:00:00, mailer=relay, pri=30645, relay=[127.0.0.1] [127.0.0.1], dsn=5.1.1, stat=User unknown Nov 20 08:42:12 MySite sm-mta: tAKDgCeX028156: from= , size=645, class=0, nrcpts=0, proto=ESMTP, daemon=MTA-v4, relay=localhost [127.0.0.1]
Notice the bit about ‘5.1.1 User Unknown‘? Safe to say, I was ready to throw the towel in at this point and use something else such as Mandrill. I then miraculously came across a post on about page 8 of Google that had the solution that hadn’t been listed anywhere else.
Step 8: Edit /etc/mail/sendmail.mc
Run the following:
sudo nano /etc/mail/sendmail.mc
Go right to the bottom and add the following two lines:
define(`MAIL_HUB', `mysite.co.uk.')dnl define(`LOCAL_RELAY', `mysite.co.uk.')dnl
Note: I suggest you copy and paste the code above exactly as it is. There are backticks, and a dot at the end of the domain so be sure to have them included.
Then run sendmailconfig one more time:
Step 9: Test again
Cross your fingers, run a test now, and you should be able to send emails to all domains, including the same domain as the hostname.
One Final Note
If you followed the steps recommended by DigitalOcean when setting up your server you may have installed a firewall. I installed UFW here and recommend you just make sure port 25 is in the list of allowed ports. To do this you can do the following:
sudo ufw allow 25
As with some of the other things above, I’m not sure if this is a necessary step but I wanted to remove all doubt as best I could.
So there we have it. For many, steps 1-7 will hopefully allow you to send mail from the server, but it was step 8 for me that was required. Oddly it wasn’t documented anywhere and I stumbled upon it by chance.
Finally I now have the DigitalOcean droplets running exactly as I need and look forward to moving all our other sites over to them. Sorry 1and1 but you will never have another penny out of me.