How to: Send emails from WordPress in a Linux host

How to: Send emails from WordPress in a Linux host

WordPress uses Php libraries in order to send emails. What this means is that if your php is not set up correctly it won’t know how to send emails. I was hoping all it would take was just a few entries with an SMTP server but apparently you need an additional library/application if you want to get this to work. Below I will cover a few options briefly if you want to enable your WordPress site running on Linux to work:

I. Use a Plugin

Using a plugin might be the easy way out but sometimes is the only option you may have. Most WordPress hosting providers have email enabled but if they don’t you really are out of luck. Things to keep in mind though: If you use SPF records you would need to allow email to originate from your hosting provider and that comes with a risk. If someone wanted to impersonate your emails they need only to get a hosting account with the same provider and your SPF records won’t be able to mark that email as SPAM as it comes from an allowed server.

You will find two sort of plugins available: Some allow you to use an external SMTP server while others integrate with third party solutions. In my case I recommend using SendGrid. They offer a plugin which leverages their API so you can avoid using SMTP all together if for some reason that is blocked. The main reason I find this as my choice when sending emails is that I can set up things like email tracking and manage subscriptions from SendGrid. Also, this helps me avoid sending emails from a shared host. The SendGrid plugin comes with a nice statistics page that integrates with your account. You can monitor how many emails you are sending, how many are opened, etc. This is a plus from simply sending an email. You can sign up using this address:

Another plugin you can use to use an external SMTP is

II. Configure Postfix

Postfix is if not the most used one of the most popular solutions for handling email in a Linux environment. It can be deployed in 4 different modes which give you a wide variety of options:

  • Internet: A full featured email server… think on the lines of I need an email server to send and receive emails for my users.
  • Satellite: An extern mail provider (e.g. Google) will be used for sending and mail. The server will not receive any mail.
  • Smarthost: This is equivalent to the Internet option with the difference that outgoing email is routed through a third party/external system. The reason you would do this is if you wanted to leverage deliverability from the likes of SendGrid or your ISP is blocking outgoing SMTP, etc.
  • Local only: This is used as a system internal mailserver. You can only send mail from user to user on the system.

Most users will find the Satellite to be most fitting for a WordPress installation. Remember you are going to need to leverage an external email server you have or a third party’s like Gmail or SendGrid to have this work.

You can install Postfix using the following command line:

sudo apt-get install postfix

It will take you through few steps. The important one are:

  1. Select option – Satellite or Internet depending if you are planning on using an external email server or not.
  2. Hostname – It is recommended you use a FQDNlike that is obviously a domain name you own. If you chose Internet this is going to be used for receiving emails. 

Those two options should be good enough to get you through. Just restart the server to be safe after making changes to the configuration.

/etc/init.d/postfix restart

Testing your mail config:

Run following PHP code with [email protected] replaced by your mail id:

php -a
mail ('[email protected]', "Hello World!", "My email setup works!");
exit ();

Post Installation Configuration – Password authentication (Gmail)

Most smtp-servers (SendGrid & Gmail for example) require a password authentication to send mail. Also you need to configure postfix to use password authentication (sasl) and encryption in some cases:

  • Install libsasl2-modules and sasl2-bin:
    sudo apt-get install libsasl2-2 libsasl2-modules sasl2-bin
  • Enable sasl-auth by adding these lines to /etc/postfix/
     # add to /etc/postfix/
     smtp_sasl_auth_enable = yes
     smtp_sasl_security_options = noplaintext noanonymous
     smtp_sasl_password_maps = hash:/etc/postfix/sasl_password
  • Create a file /etc/postfix/sasl_password with a line like: [email protected]:USERPASSWORD

    This example uses gmail as the smtp server. Be sure to replace each part with the settings for your provider.

  • Update postfix:
     sudo chmod 600 /etc/postfix/sasl_password
     sudo postmap hash:/etc/postfix/sasl_password 
     sudo postmap /etc/postfix/sender_canonical
     sudo /etc/init.d/postfix restart   

    You might need to execute the following if you get a permission denied when you postmap: chwon postfix:postfix /etc/postfix .

III. Install msmtp

This is a lightweight MTA (Mail Transfer Agent). Is like Postfix with only the Satellite option availabe. As installing postfix might be overkill this is an alternative worth exploring.

In order to install msmtp execute the following line from the command line:

sudo apt-get install msmtp ca-certificates

Then create a new configuration file:

sudo nano /etc/msmtprc

and place the following configuration information in it:

# Set defaults.

# Enable or disable TLS/SSL encryption.
tls on
tls_starttls on
tls_trust_file /etc/ssl/certs/ca-certificates.crt

# Set up a default account's settings.
account default
port 587
auth on
user [email protected]
password MySecretPassword
from [email protected]
logfile /var/log/msmtp/msmtp.log

You need to modify all the underlined items with your own.

Tell PHP to use it

sudo nano /etc/php5/conf.d

And add this line:

sendmail_path = /usr/bin/msmtp -t

Set up logging

sudo mkdir /var/log/msmtp
sudo chown www-data:adm /var/log/msmtp
sudo nano /etc/logrotate.d/msmtp

And then add these lines to the new file:

/var/log/msmtp/*.log {
  rotate 12

IV. Install SSMTP

I haven’t tried this option but it is fairly simple and lightweight as well:

Install it by running:

sudo apt-get install ssmtp

Then edit /etc/ssmtp/ssmtp.conf file, comment out existing mailhub line and add the following lines (this example is for gmail smtp server):

Now make sure that your php.ini has the correct sendmail_path:

sendmail_path = /usr/sbin/sendmail -t

Reload apache and your php should be able to send outgoing emails now.

Enhanced by Zemanta

You may also like...

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.