Mailing – Beginners guide to Digital Ocean

Ernest Marcinko Hosting, Tutorials 2 Comments

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:

nano ~/.msmtprc

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

Known issues

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

Chapters

<< Chapter #6      Chapter #8 >>

Ernest Marcinko

WordPress developer, javascript enthusiast, photoshop clicker and the founder of wp-dreams.com

Stay connected

Related Posts
Filter by
Post Page
Tutorials Wordpress Hosting Blog Testing
Sort by

Saving space automatically – Beginners guide to Digital Ocean

After a while, you will notice an exponential growth of used space, without even copying anything to it. How
2016-05-06 09:24:05
admin

18

Swap file – Beginners guide to Digital Ocean

If you reached this step, your server is up and running flawlessly. However after gaining some traffic it will run out
2015-01-21 15:48:02
admin

18

FTP & SFTP – Beginners guide to Digital Ocean

So far we have a running web server with a working database, but we don't have FTP access. SFTP However
2015-01-16 17:08:19
admin

18

Virtual Hosts - Beginners guide to Digital Ocean

By now we have a working private server with very basic features and a demo user. By default your website is data is
2015-01-15 19:23:28
admin

18

From XAMPP to Vagrant WordPress

2016-01-05 19:58:17
admin

8

Comments 2

  1. Daniel

    Nope, doesn’t work.

    This always happens to me and I never remember how I fix it, but I can’t just apt-get sendmail.

    I keep getting *** ERROR: FEATURE() should be before MAILER()
    *** ERROR: FEATURE() should be before MAILER()
    *** ERROR: FEATURE() should be before MAILER()
    *** ERROR: FEATURE() should be before MAILER()
    *** FEATURE(smrsh) must occur before MAILER(local)
    *** ERROR: MAILER(local) already included
    *** ERROR: MAILER(smtp) already included

    Even after I apt-get purge sendmail* and manually remove cf and mc files. Doesn’t matter. Always installs broken.

Leave a Reply

Your email address will not be published. Required fields are marked *