Jesin's Blog

Welcome to the Portal of Technology

  • Facebook
  • GitHub
  • RSS
  • Twitter
  • Home
  • Categories
    • Domains
    • Linux
    • Networking
    • PHP
    • Virtualization
    • Web Design
    • Web Servers
    • Windows
  • WordPress Plugins
    • Custom Error Pages
    • HTTP Digest Authentication
    • Mailgun Email Validator
  • Toolbox
    • DNS Lookup Tool
    • htdigest Generator Tool Online
    • htpasswd Generator Tool Online
    • HTTP Headers Lookup Tool
    • MD5 Encryption Tool
    • Open Port Check Tool
    • SHA-1 Encryption Tool
    • URL Encoding/Decoding Tool
  • About Me
  • Contact Me
  • Sitemap
Home ›
Linux ›
How to Configure MSMTP for Cron

How to Configure MSMTP for Cron

October 31, 2013 Linux Jesin A 3 Comments

The Cron utility of emails the output and errors of an executed job to the appropriate Linux user. This requires a properly setup mail server such as sendmail.

However you can use msmtp (an SMTP client) to receive emails from cron jobs to each users’ preferred email ID through your preferred email provider.

Make sure msmtp is installed and configured by following the instructions here.

Method 1: msmtp for all cron jobs

By following this method you’ll be using msmtp system-wide for all cron jobs.

A few modifications to the /etc/msmtprc file are required to make it work with crontab. First we need an aliases file so set its path in the msmtprc file.

aliases /etc/aliases

This file should contain Linux usernames to email mappings like this.

root: root@example.com
jesin: jesin@gmail.com
default: email@aol.com

The default is the default email to which an email will be sent if no match for a user is found.

Next you need to set an account as default in the /etc/msmtprc file. So add this line to the end

account default : provider

Check if you already have sendmail or a sendmail wrapper installed using the which command.

which sendmail

If you get an output for this you should uninstall that package first using the yum remove or apt-get remove command.

Create a symbolic link for msmtp in the path used by sendmail.

ln -s /usr/bin/msmtp /usr/sbin/sendmail

Now test this from the command line using a Linux username as a recipient.

echo "From: root \
To: jesin \
Subject: Hello World \
\
This is the email body" | sudo sendmail -d -t jesin

The -d option is to display detailed debugging information so that it is easier to troubleshoot if it doesn’t work as expected.

Create a cron job which echos some text which causes an email to be sent.

crontab -e
* * * * *	echo "A message from Cron"

Have a look at your syslog or messages file to know what is happening

tail -f /var/log/syslog

You’ll have to wait for 60 seconds for the cron job to execute.

Oct 31 07:38:50 localhost crontab[7915]: (jesin) LIST (jesin)
Oct 31 07:38:51 localhost crontab[7916]: (jesin) BEGIN EDIT (jesin)
Oct 31 07:39:40 localhost crontab[7916]: (jesin) REPLACE (jesin)
Oct 31 07:39:40 localhost crontab[7916]: (jesin) END EDIT (jesin)
Oct 31 07:40:01 localhost CRON[7954]: (jesin) CMD (echo "A message from Cron")

Make sure to remove the cron job immediately else an email will be triggered every minute and your Email Service provider might suspend your account.

Method 2: msmtp for specific cron jobs

If you need status emails only from specific cron jobs you can do so by simply redirecting the stdout and stderr messages.

crontab -e
* * * * *	/scripts/backup.sh 2>&1 | msmtp youremail@provider.com

The 2>&1 redirection operator sends both the stdout and stderr of backup.sh to msmtp which sends an email with this content in the email body.

Related posts:

linux category thumbnailHow to use MSMTP with Gmail, Yahoo and PHP Mail windows category thumbnailUsing Sendmail on Windows linux category thumbnailConfigure BIND DNS Split View

Tags: sendmail

Comments

  1. Jim says

    November 30, 2013 at 8:54 pm

    Writing crontab line is a headache for many people, for them http://www.easycron.com/generator/cronjob is a helper to get it straight.

    Reply
  2. FreeSoftwareServers says

    September 20, 2016 at 10:56 pm

    This post really helped me debug my sendmail issues, both with the Cron Example plus the sendmail -d flag, but its missing something Ironic for the title. There is an alias called “cron” that you can use in /etc/aliases.

    http://msmtp.sourceforge.net/doc/msmtp.html#Aliases-file

    # Example aliases file

    # Send cron to Mark
    cron: mark_jones@example.com

    Thanks!

    Reply
    • Craig Chambers says

      March 22, 2017 at 7:14 pm

      Thanks for this post, I set up msmtp years ago and recently noticed that it was not working for cron in the syslog. One piece of feedback is that the echo command that you use to debug lost all the newlines when I tried it (Ubuntu 14.04 server), so that the from fields contained the rest of the text (and my mail provider marked it as spam).

      I fixed it by adding explicit newlines for the echo command and telling echo to interpret escape sequences:

      echo -e “From: root\n \
      To: jesin \n\
      Subject: Hello World\n \
      \n\
      This is the email body” | sudo sendmail -d -t jesin

      Reply

Leave a Reply Cancel reply

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

Get a wealth of information delivered to your inbox. Subscribe and never miss a single article.

  • Tutorials and howtos
  • Code, scripts and commands
  • Online Tools

* No spam, unsubscribe anytime

Hire Me

  • SSL installation and hardening (A+ on Qualys SSL test)
  • Apache & Nginx configuration
  • Email deliverability improvement (10/10 on Mail Tester & MailGenius)
  • WordPress customization, optimization and migration
  • and much more…

    Tools

    • DNS Lookup Tool
    • htdigest Generator Tool Online
    • htpasswd Generator Tool Online
    • HTTP Headers Lookup Tool
    • MD5 Encryption Tool
    • Open Port Check Tool
    • SHA-1 Encryption Tool
    • URL Encoding/Decoding Tool

    Nav

    • Home
    • About Me
    • Contact Me
    • Privacy Policy
    • Sitemap
    Vultr SSD VPS

    Creative Commons License
    Jesin's Blog by Jesin A is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
    Based on a work at websistent.com.