Sending mails properly is probably the hardest task to achieve with Digital Ocean. I will present two ways of doing that, and I strongly recommend going with the second one – using a 3rd party SMTP service.
1. way: Sendmail
This is the default way, the sendmail service is most likely already installed on your system. Still, run this command to make sure
sudo apt-get install sendmail
Then let's restart the service:
sudo service sendmail restart
That should be it, to test this, try the following line:
time echo testmail | sendmail [email protected]
This will also output the time required to send the mail. Check your mailbox if it arrived properly. (might be in the spam folder)
If you are having issues, check the known issues section below this post.
2. way: Google, Yahoo etc. SMTP service via msmtp
You can find a more detailed guide on digital ocean website as well. This guide is basically the same with less details.
I prefer this option as most of these big companies provide a free SMTP service for thousands of mails daily. The only downside is, that you usually can't change the sender address.
If you are a WordPress user, you can use an SMTP mailing plugin instead of these steps.
To install msmtp:
sudo apt-get install msmtp
Next thing is configuring this service. You will need the root account for this. Switch to root and let's go:
For Gmail add this:
account gmail tls on tls_certcheck off auth on host smtp.gmail.com port 587 user [email protected] from [email protected] password mypassword
For Yahoo use this:
account yahoo tls on tls_starttls off tls_certcheck off auth on host smtp.mail.yahoo.com user address from [email protected] password mypassword
Next thing, changing the permission. The msmtprc wont run without setting the proper permissions to the local configurations file:
chmod 600 ~/.msmtprc
To test the connection from command line, use the following code:
echo Test mail | msmtp --debug -a gmail [email protected]
This will probably end in the spam folder as well, but at least we know it works.
Setting up PHP for msmtp
There are a few final changes to make this thing work with php. First of all, let's change the ownership to www-data process:
chown www-data:www-data /etc/.msmtp_php
This will make sure that the apache www-data process can access the mail sender application. Log out from the root account and go back to your user account now.
After that we need to edit the php.ini file:
sudo nano /etc/php5/php.ini
Okay, this is a long freaking file. Hit CTRL + W to find this line:
sendmail_path = ...
Change that line to this:
sendmail_path = "/usr/bin/msmtp -C /etc/.msmtp_php --logfile /var/log/msmtp.log -a gmail -t"
Good. Let's create the log file:
touch /var/log/msmtp.log chown www-data:www-data /var/log/msmtp.log
Restart the httpd process, and the smtp mail should work on php after:
service httpd restart
Fixing long sendmail delay
There is a known issue, where sendmail actually sends the mails, but they are delayed by minutes. I have found a nice solution on Dave Pagel's blog:
Let's open up the hosts file first.
sudo nano /etc/hosts
There should be line similar to this one:
127.0.0.1 localhost.localdomain localhost
If there isn't, then add it, otherwise change it to this:
127.0.0.1 localhost.localdomain localhost YOUR_HOST_NAME
Replace the YOUR_HOST_NAME with your host name, so if your domain name is example.com then your host is simply example. So in example.com's case it would look like this:
127.0.0.1 localhost.localdomain localhost example
- Chapter #1 – Preparation & Backup
- Chapter #2 – Droplet Setup
- Chapter #3 – SSH and Users
- Chapter #4 – Virtual Hosts
- Chapter #5 – phpMyAdmin
- Chapter #6 – FTP & SFTP
- Chapter #7 – Mailing
- Chapter #8 – Swap file
- Chapter #9 – Saving space automatically
Latest posts by Ernest Marcinko (see all)
- Ajax Search Pro for WordPress 4.12 Developer notes - March 1, 2018
- Ajax Search Pro for WordPress 4.11.10 Developer Notes - January 13, 2018
- Saving space automatically – Beginners guide to Digital Ocean - May 6, 2016