How to make crontab email me with output?

156,186

Solution 1

In the end I used sSMTP. It's far simpler than either Postfix or sendmail and does the job beautifully.

For future reference, here's how to use sSMTP with Yahoo Mail (don't worry, it's a lot less complex than it looks):

  • Use your package manager to download ssmtp. I used Synaptic, but sudo apt-get install ssmtp should work as well.

  • Open the config file at /etc/ssmtp/ssmtp.conf.

  • Make the config look like this:

    [email protected]       <--- Use your real email here
    mailhub=smtp.mail.yahoo.com:587       <--- This will depend on your email provider
    FromLineOverride=YES
    UseSTARTTLS=YES
    [email protected]   <--- Use your real email here
    AuthPass=yourRealYahooPassword        <--- Use your real email password here
    TLS_CA_File=~/cert.pem
    
  • Create the cert.pem file with OpenSSL. I used openssl req -x509 -newkey rsa:2048 -keyout key.pem -out cert.pem -days 9999 -nodes (more info at How to create a self-signed certificate with OpenSSL). You can stick the file anywhere, but I just chucked it in ~/. Wherever you put it, make sure you point the TLS_CA_File= line in ssmtp.conf to the correct location.

  • Open the file /etc/ssmtp/revaliases and add the line (replacing the appropriate sections with your details):

    yourPCUsername:[email protected]:smtp.mail.yahoo.com:587
    

    If you're running as root, I assume you would need to add another line replacing your name with 'root'.

  • That's it, you're good to go! To test, the easiest way (IMO) is to create a file with the following in it:

    To: [email protected]                       <--- Use your real email here...
    From: "whateverYaWant" <[email protected]>  <--- ...and here
    Subject: Some Notifying Email
    MIME-Version: 1.0
    Content-Type: text/plain
    
    Body of your email goes here. Hello world!
    
  • Check that you don't have the real sendmail installed by running sendmail -V. It should say 'sSMTP'.

  • Finally, run cat fileWithEmailInIt.txt | sendmail -i -t, then wait a few seconds (10-30) and check your email!

Additional note 1: If you have trouble with Gmail, try option 1 of this answer.
(Thanks to Ben Creasy!)

Additional note 2: If mail is sending from the command line but not through crontab, try changing FromLineOverride to NO in /etc/ssmtp/ssmtp.conf. You can also get more detailed logging by adding Debug=YES to ssmtp.conf - the extra logging goes to /var/log/mail.log.
(Thanks Jakub Kukul!)

Solution 2

Install Postfix. It is more complicated than most other packages but it's still not complicated.

sudo apt-get install postfix

Select "Internet Site" and then accept all the defaults. Then we just need to stop outside connections, turning this into a "null client". Run: sudoedit /etc/postfix/main.cf and find the inet_interfaces setting (near the end) and change it to loopback-only, like so:

inet_interfaces = loopback-only

And finally restart Postfix with sudo /etc/init.d/postfix restart (reloading won't do).

You now have a Postfix install that won't relay email for outside machines, it'll just accept connections on 127.0.0.1 (and ::1 for IPv6).


On a separate note, your cron lines are probably not working because you're using non-relative paths and paths with Bash substitutions in. sh doesn't understand ~ and it might not have a proper PATH set. So replace them with (I'm just guessing at the actual paths):

1 0 * * *   /home/clonkex/Desktop/toskymesh.sh
59 6 * * *  /home/clonkex/Desktop/tooptus.sh
0 3 * * *   /usr/bin/snapraid sync

And if your scripts require to be run from a specific directory, make sure they cd into the right directory. Don't assume that cron will be in the right place as it likely won't.

Solution 3

In case you don't want email notifications, but want to log cron jobs' output somewhere to be able to inspect it if need be:

# apt install postfix

Select "Local only" when asked for a mail server configuration type.

To confirm that cron will be able to send mail:

# apt install mailutils
# echo test body | mail -s 'test subject' root
# cat /var/mail/root

Alternatively add a cron job:

* * * * * date

Wait for it to run (journalctl -ef or watch date), then cat /var/mail/root.

Solution 4

I had very good luck with exim4.

sudo apt-get install alpine exim4 mailutils eximon4 spf-tools-perl swaks

(alpine is just the mail client I like using)

After that, I ran

sudo dpkg-reconfigure exim4-config

and followed through the prompts. This page: https://help.ubuntu.com/community/Exim4 was very helpful as well. It took me about 10 minutes to get it running.

Solution 5

From sSMTP manpage: "It does not do aliasing, which must be done either in the user agent or on the mailhub. Nor does it honor .forwards, which have to be done on the recieving host. It especially does not deliver to pipelines."

So, if you want to receive all msgs sent to root on your extenal email, it's a bad idea to use sSMTP, because it does not support aliases.

Instead, you could user postfix. It's still very simple. Here's how to use it with gmail as your smtp for sending messages:

sudo apt-get install postfix mailutils

General type of mail configuration: Satellite system

System mail name: The preferred fully-qualified name of the mail server, e.g., mail.example.com

SMTP relay host (blank for none): [smtp.gmail.com]:587

Add to /etc/postfix/main.cf:

smtp_use_tls = yes
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl/sasl_passwd
smtp_sasl_security_options = noanonymous
smtp_sasl_tls_security_options = noanonymous

Create /etc/postfix/sasl/sasl_passwd

[smtp.gmail.com]:587 [email protected]:password

Then:

sudo postmap /etc/postfix/sasl/sasl_passwd
sudo chown -R root:postfix /etc/postfix/sasl
sudo chmod 750 /etc/postfix/sasl
sudo chmod 640 /etc/postfix/sasl/sasl_passwd*

Edit /etc/aliases and add:

root: [email protected]

Lastly, run:

sudo newaliases
sudo systemctl stop postfix.service
sudo systemctl start postfix.service

Now, you can test if redirecting is working:

echo "Test to root." | mail -s "Test message to root" root

Hope it helps.

Share:
156,186

Related videos on Youtube

Clonkex
Author by

Clonkex

Enjoyer of Overwatch/AoE4/Siege/Mordhau/Chiv2, sort-of-Accordion Player, avid Rollerblader (Seba High Light V1 2015), Christian. Rode a CB500X for a while, but sold it when my car engine cost me 12 grand. Now riding a 2020 Royal Enfield Himalayan (Itchy Boots ftw). Started programming at ~13 in Liberty BASIC. Progressed to DBPro at ~15 (and loved it). Now a decent programmer of many languages living in beautiful country NSW. Came up with my name at 13, when my gaming life was defined by the awesome games Clonk and Cube/Sauerbraten and by emulators like MAME, Gens and SNEX9x. Originally a fan of C++, then of Javascript, but C# is my current favourite. Boop.

Updated on September 18, 2022

Comments

  • Clonkex
    Clonkex almost 2 years

    How can I make crontab email me with the output of its jobs? I have [email protected] above the jobs, but it doesn't work.

    I know I need to use something like Postfix or Sendmail but for the life of me I cannot find out how to achieve this. I've searched and searched for a simple guide on setting up Postfix but they're all insanely complicated and expect you to be setting up a whole server to send and receive email and to know all the terms (like, what is a domain name for outgoing emails?).

    All I want to do is have crontab email me. Why is this so difficult??

    Additional Info:

    My crontab file looks like this:

    [email protected]
    1 0 * * * ~/Desktop/toskymesh.sh
    59 6 * * * ~/Desktop/tooptus.sh
    0 3 * * * snapraid sync
    

    More Additional Info:

    The output of ps -ef | grep '[s]endmail'

    root 6840 1370 0 10:26 ? 00:00:00 sendmail: MTA: accepting connections
    

    I know for sure cron is working, but I'm testing the mail command with echo Test | mail -s Test [email protected] anyway.

  • Clonkex
    Clonkex over 9 years
    Thanks, but Postfix is complicated, if only because there seem to be no simple guides whatsoever. Anyway, I got it working just fine with sSMTP which does exactly what I need and is genuinely simple (although it still took a lot of fiddling and random guessing to get it to actually work). I meant to post an answer but forgot - I actually managed to destroy my OS installation so as soon as I've got it going again fully I'll post my own answer :)
  • Clonkex
    Clonkex over 9 years
    And actually my cron jobs worked just fine with ~/Desktop/.
  • Richard
    Richard about 8 years
    What is the point of the .pem file? I see this tutorial doesn't include one: havetheknowhow.com/Configure-the-server/Install-ssmtp.html
  • Clonkex
    Clonkex about 8 years
    @Richard If I remember correctly, Yahoo Mail refused to receive the email without it, but I'm not 100% sure. You could try it without it first, and if it doesn't work just add the certificate.
  • Mark Berry
    Mark Berry about 7 years
    Thanks for the sSMTP suggestion.The Ubuntu Wiki has a helpful guide (but don't use an email address for hostname as currently shown). This answer suggests that you can use default server certificates, but I found I did not need a TLS_CA_File= line for sending via GMail.
  • Clonkex
    Clonkex about 7 years
    @MarkBerry Indeed, I found lots of information when I was struggling with this, but the problem was that it was all separated and not one single guide covered everything that needed to happen. I suspect it was made more difficult by Yahoo requiring the certificate, but iirc I also tried with a gmail account and couldn't succeed either.
  • craq
    craq about 7 years
    I needed inet_interfaces = localhost. loopback-only gave me postfix: fatal: config variable inet_interfaces: host not found: loopback-only
  • Mattwmaster58
    Mattwmaster58 about 6 years
    The Arch linux wiki page for sSMTP says it's deprecated. Use mSMTP instead
  • Clonkex
    Clonkex about 6 years
    @MattM. Not deprecated, just not maintained. It should be totally fine to use sSMTP in a non-production environment :)
  • E.S.
    E.S. almost 6 years
    Although I can send mail through commandline with this, no mail is sending through crontab... are there other steps missing?
  • Clonkex
    Clonkex almost 6 years
    @E.S. What command did you put in your crontab?
  • Jakub Kukul
    Jakub Kukul almost 4 years
    I had the same problem as @E.S. today - sending mail via commandline worked fine, but no mail was sent through crontab. I fixed it by changing FromLineOverride to NO in /etc/ssmtp/ssmtp.conf. During debugging, it was also helpful to add Debug=YES to ssmtp.conf - it resulted in more detailed information being logged to /var/log/mail.log.
  • linrongbin
    linrongbin almost 3 years
    Hi, thanks for your solution of using ssmtp, and thanks @JakubKukul, your comment fixed my issue about cannot sending email in crontab.