Sending automated mail through Office365 from Ubuntu

As part of my home server setup, one of the things I am setting up is an automated alert system that would send emails when events happen (for e.g. hard disk SMART errors, CPU temperature violation, hard disk out of space etc.). I was looking around to find the best way to send automated mail and it looks like ssmtp is the answer.

I have an Office365 account for my domain which provides email support – I wanted the mails to be sent through the Office365 servers. In the past, sending mail from a Linux box required installing a full blown Mail Transfer Agent (postfix or god forbid, sendmail). Thankfully, ssmtp has removed the need for installing and configuring a full fledged MTA just for sending mails through a ISP/SMTP provider.

The steps are simple – install ssmtp

sudo apt-get install ssmtp

Then edit /etc/ssmtp/ssmtp.conf file to fill in the details:

#
# Config file for sSMTP sendmail
#
# The person who gets all mail for userids < 1000
# Make this empty to disable rewriting.
root=[sender email id]

# The place where the mail goes. The actual machine name is required no 
# MX records are consulted. Commonly mailhosts are named mail.domain.com
mailhub=smtp.office365.com:587

# Authentication Information
AuthUser=[sender login id - usually same as email id]
AuthPass=[sender password]
UseTLS=YES
UseStartTLS=YES

# Where will the mail seem to come from?
#rewriteDomain=[your domain]

# The full hostname
hostname=[your FQDN]

# Are users allowed to set their own From: address?
# YES - Allow the user to specify their own From: address
# NO - Use the system generated From: address
FromLineOverride=YES

In my case, I created a new user in my Office365 account that will be used only to send these alert emails. The main reason is that the password is available in plaintext in the /etc/ssmtp/ssmtp.conf file and therefore I avoided using any of my primary email accounts. Additionally, I also changed the permissions on the file to 640 so only owner and group has read access to the file. Note that when you do this only root and the group for ssmtp.conf will be able to send mail. All other users will be unable to send mail due to the lack of configuration.

Finally, to test the setup, run

sudo ssmtp [recipient email address]

Then type the following:

From: [sender email id]
To: [recipient email id]
Subject: [subject]

[Body of the mail]

and ctrl-d to complete the mail. If all goes well, your mail should show up in the recipient's inbox. Check /var/log/syslog for log output if you face issues.

2 Replies to “Sending automated mail through Office365 from Ubuntu”

  1. I did everything as documented, but when I try to send mail from command line, i see:

    ==
    [root@shop ssmtp]# send-mail: 550 5.7.60 SMTP; Client does not have permissions to send as this sender

    ==

    any ideas?

Leave a Reply

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