DeeEmm

Pragmatism in code

Waxing lyrical about life the universe and everything software related since lunchtime 2006.

Configuring Joomla to use Zoho mail with MSMTP on Ubuntu.

Configuring Joomla to use Zoho mail with MSMTP on Ubuntu.

A while ago I switched to using zohomail for the email handling on a number of sites that I run. Zohomail is a nice alternative to hotmail or gmail and is also free for a limited number of users which makes it perfect when you just need to add a contact method to a website or just need something to process user messages.

I had previously been running zohomail under the SMTP service on my Joomla installs as I do not have sendmail installed. I find that it's simply too much to maintain and that SMTP is much simpler. Unfortunately for some reason whilst updating one of my sites the SMTP function simply stopped working and no amount of googling could give me an answer. So unperturbed I decided to engineer an alternative to the native Joomla SMTP functionality. After a bit of research and stumbling across Emanuel Tessores excellent article I decided to give MSMTP a shot

MSMTP is a very simple and easy to use smtp client with excellent sendmail compatibility. It can be used to in place of sendmail to allow you to send emails via a third party SMTP server such as zoho, gmail or hotmail. Installation and setup is relatively straightforward being installable via apt-get via the following command

sudo apt-get install msmtp

Once installed you need to create two files; a configuration file for the email server details that you wish to connect to and a log file to record each mail transaction. You can do this by using the commands below

sudo mkdir /etc/msmtp
sudo mkdir /var/log/msmtp

Next you need to create the config file

sudo touch /etc/msmtp/yourservername
sudo nano /etc/msmtp/yourservername

With the config file opened in nano you need to add the following information - (simply cut and paste it in.)

# Define here some setting that can be useful for every account
defaults
        logfile /var/log/msmtp/general.log

# Settings for default account
account default
        protocol smtp
        host smtp.zoho.com
        tls on
        tls_starttls off
        tls_certcheck off
        port 465
        auth plain
        user username[A]yourdomain.com
        password yourpassword
        from username[A]yourdomain.com
        logfile /var/log/msmtp/yourservername.log

# If you don't use any "-a" parameter in your command line,
# the default account "default" will be used.
account default: default

Obviously you need to substitute the relevant account details for those of your own email account.

One point to note is that to be able to use the email account it will have to be configured as a 'send mail' address. This can be done by logging in to the zoho webmail client and going to > settings > mail > send mail as and then adding the email address you wish to use. You will also need to confirm ownership of the email by pasting in a confirmation code emailed out to the email address.

With the configuration file finished save it and then run the following command to change it's permissions. If you don't do this MSMTP will not work.

sudo chmod 600 -R /etc/msmtp/yourservername 

Now that the email address and MSMTP are configured it's time to test. This is easy enough to do from the command line but first you will need to temporarily change the file ownership of the config and log files to allow you to run them. This is very easily done as follows

sudo chown -R yourusername:yourusername /var/log/msmtp 
sudo chown -R yourusername:yourusername /etc/msmtp/

(Replace yourusername with the username you used to log into your server with) Now lets send a test message:

sudo echo -e "Subject: Test Mail\r\n\r\nThis is a test mail" | msmtp --debug --from=sender[A]yourdomain.com -t receipient[A]thierdomain.com --file=/etc/msmtp/yourservername

Again, note the parts to be changed. It is important that the from address is exactly the same as the one you specified in the configuration file and set up as the 'send mail' address, if there is a discrepancy here it will likely result in a 553 Relaying disallowed response from the server.

When you hit return you should be able to see the entire transaction scroll up on the terminal screen, if everything is okay you will get a 250 Message received response at the end. If you get any other response you will need to note the error and address it accordingly.


With the email now set up to work from the command line it's now time to switch out the sendmail function to use MSMTP instead. To do this you will need to edit your php.ini file. this should reside at  /etc/php5/apache2/php.ini you can check this viewing the php info print out under > system information > php information in the administration panel. Open up php.ini in nano by using the following command.

sudo nano /etc/php5/apache2/php.ini

Locate or search for the following

sendmail_path =

Depending on your server configuration it may or may not be empty. You need to change it to the following:

sendmail_path = "/usr/bin/msmtp -C /etc/msmtp/yourservername --logfile /var/log/msmtp/yourservername.log -a default -t"

Again - replace the relevant details with those of your own and save your changes. be sure to restart your server afterwards.

sudo service apache2 restart

Finally you need to reverse those permissions you set earlier by issuing following commands...

sudo chown -R www-data:www-data /var/log/msmtp 
sudo chown -R www-data:www-data /etc/msmtp/

...and also checking that you have the Joomla mail settings set to use the php mail function NOT sendmail or SMTP. You can set this in > system > global configuration > server > mail settings.

Great, now for some more testing.

I tested my setup by filling out and submitting a contact form but you could also use the mess message feature (limit the number of messages by only sending it to a user groups with one or two members) - if everything is okay you should see a 'message is sent' message.

Whilst this is not a straightforward solution it does completely do away with sendmail which is not trivial to setup and manage. It also allows you to use any SMTP server you want, I used zohomail but you could easily use your  ISPs SMTP server or googlemail, yahoomail etc. (a google will turn up results that contain the relevant SMTP settings for gmail / yahoo mail / hotmail)

 

A couple of caveats.... 

1: You need to ensure that you have the correct MX records set up for your domain for your zohomail account to work correctly with your domain. These are as follows:

10 mx.zohomail.com
20 mx2.zohomail.com

(This does not apply if you are using an @zohomail.com email address.)

I recommend making sure everything works with your regular email client before jumping into this. It might save some headaches later

2: For some reason the code highlighter has gone nuts and will not let me post the @ sign in the code snippets so I have replaced these with [A] but I'm guessing that you probably realized that already ;)

DM

 

Virtuemart PayPal checkout issues
EasySocial, raising the bar for Joomla social exte...

Related Posts

 

Comments

No comments made yet. Be the first to submit a comment
Already Registered? Login Here
Guest
Saturday, 19 August 2017
If you'd like to register, please fill in the username, password and name fields.

Captcha Image