How to make crontab email me with output?
In the end I used sSMTP
. It's far, 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 far less complex than it looks):
Use Synaptic to download ssmtp. Alternatively you could run
sudo apt-get install ssmtp
.Open the config file at
/etc/ssmtp/ssmtp.conf
.Make the config look like this:
root=[[email protected]] mailhub=smtp.mail.yahoo.com:587 FromLineOverride=YES UseSTARTTLS=YES AuthUser=[[email protected]] AuthPass=[yourRealYahooPassword] TLS_CA_File=~/cert.pem
Create the cert.pem file with OpenSSL. I used the command
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 theTLS_CA_File=
line in ssmtp.conf to the correct location.Open the file
/etc/ssmtp/revaliases
and add the line:[yourPCUsername]:[[email protected]]:smtp.mail.yahoo.com:587
If you're running as root, I would think you 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]] From: "whateverYaWant" <[[email protected]]> Subject: Some Notifying Email MIME-Version: 1.0 Content-Type: text/plain Body of your email goes here! Hello world!
Save and close the file, then to check you don't have the real sendmail installed, run
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!
Obviously, replace [[email protected]]
with your email (without the brackets) and [yourRealYahooPassword]
with your Yahoo Mail password (again, without the brackets).
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!)
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.
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.