How to send an e-mail after a cron job

130,932

Solution 1

You can use the MAILTO option in crontab to define your email address and receive all output and errors in all crons running.

open crontab using

crontab -e

on the top of the file use MAILTO option as

[email protected]

cron looks for MAILTO feature to decide where it should send cron logs. it send is to root by default if the crons are running with root.

put it there on the top and remove any mail command reference from the crons.

Test and verify if you receive cron alerts after this.

Solution 2

On FreeBSD

1: Check log:

tail -f /var/log/cron
tail -f /var/log/maillog

2: Replace sendmail with ssmtp vi /etc/rc.conf file.. and add:

sendmail_enable="NO"
sendmail_submit_enable="NO"
sendmail_outbound_enable="NO"
sendmail_msp_queue_enable="NO"

3: Launch following commands:

killall sendmail
cd /usr/ports/mail/ssmtp/
make install replace clean

4: Edit ssmtp conf file:

vi /usr/local/etc/ssmtp/ssmtp.conf

.... and add following lines:

[email protected]
mailhub=smtp.example.com:465
RewriteDomain=example.org
UseTLS=YES
[email protected]
AuthPass=password222
FromLineOverride=YES
Hostname=yourhostname

5: Enter following command:

echo ‘ssmtp_enable=“YES”’ >> /etc/rc.conf

p.s. other explained options of ssmtp.conf are here:

http://www.techrepublic.com/blog/it-security/use-ssmtp-to-send-e-mail-simply-and-securely/

Solution 3

Problem in this case was that message had been delivered to spam folder on gmail (which is always worth checking). Adding sender as a contact should avoid this.

Apart from that, looking at the received message's full headers may give clues as to why it was flagged as spam (and so how to influence filtering not to do this).

Share:
130,932

Related videos on Youtube

Booth
Author by

Booth

Updated on September 18, 2022

Comments

  • Booth
    Booth over 1 year

    I have added a cron job for my incremental backup, and I configured it like this:

    0 23 * * * /usr/bin/rsync -ravzX /mnt/external/project/ /media/backup/project/ | mail -s "Backup Success" [email protected]

    But i didn't receive any e-mails.
    How can I find out what's wrong?

    • masegaloeh
      masegaloeh about 9 years
      Check mail.log entry
    • Paul Haldane
      Paul Haldane about 9 years
      Can you send mail successfully using mail from the command line?
    • Booth
      Booth about 9 years
      @paul: its working in mail command
    • Booth
      Booth about 9 years
      @masegaloeh: no log entry
    • Paul Haldane
      Paul Haldane about 9 years
      Has the message gone into spam folder? If not (and there's nothing in the cron log either) then you need to look at the mail logs of the machines involved in delivery. You can see what these are by looking at the full headers of the successfully delivered test message.
    • Reaces
      Reaces about 9 years
      @user3774387 I edited your post, fixing some grammar and spelling. If you disagree with the changes feel free to roll-back.
    • Dennis Nolte
      Dennis Nolte about 9 years
      try to use the full path for the mail command (same as you did with rsync)
    • MadHatter
      MadHatter about 9 years
      It is very likely that the mail is being sent, and simply misfiled by google. But we will need to see your mail logs (most likely /var/log/maillog, around the time of the cron job) to know for sure.
    • Booth
      Booth about 9 years
      @paul: you are right.My backup mail is in spam folder.Why it goes to spam?how to move it on inbox?
  • Jenny D
    Jenny D about 9 years
    This would have been a great answer if the question had been "How do I get SSMTP to send mail via gmail on FreeBSD". Unfortunately, the question is about cron & mail on Linux.
  • Booth
    Booth about 9 years
    My backup mail is in spam folder.Why it goes to spam?how to move it on inbox?
  • sandeep.s85
    sandeep.s85 about 9 years
    The email delivery is something you have to consider as per your requirements. If you are the only one who receives such email then simply mark that email as "Not Spam" and it will land in inbox next time. But if you are looking for a generic email solution that will deliver email in inbox then i recommend you must read the best practices like Setup a Reverse DNS record for your Server IP address, a PTR record , an SPF record that permits legitimate servers an outgoing email on behalf of that domain etc..
  • papiro
    papiro over 7 years
    What exactly was the OP's question: "How can I send an email after a cron job?", or "How can I find out what's wrong (with my mail setup)?"
  • Houman
    Houman about 4 years
    Can you also set REPLYTO? Or FROM? Didn't seem to work for me.