When No MTA Is Installed, How To Send An Email With A Cronjob?

Home » CentOS » When No MTA Is Installed, How To Send An Email With A Cronjob?
CentOS 16 Comments

Hi,

When no MTA is installed, How to send an email with a cronjob?

I have below entrty in my cronjob?

my /etc/cron.d/backup file looks like this.

MAILTO=myemail@example.com
15 11 * * * root /root/scripts/backup.sh

Can I send this email via SMTP server?

Hope to hear from you.

16 thoughts on - When No MTA Is Installed, How To Send An Email With A Cronjob?

  • Install postfix and configure to accept messages from ‘localhost’ only.

    I guess you have a ‘real’ email id in your setup.

    Some global service providers may accept email from the postfix MTA in your server.

    Alternately, you can configure postfix to relay the message via a
    ‘smart_host’ (preferred method). It will require you to provide a valid email address + plus password for your postfix (client) to do authenticate itself and send the outgoing message.

    There is ample documentation on the ‘Net to accomplish this.

    — Arun Khan

  • CentOS 6 64 bit, zimbra is running with multi server setup. One mailbox and 2 mta servers.

    on mailbox server where no mta is installed, I want to backup mailboxes with a script. I have set it up to /etc/cron.d/backup file. After the backup, I want to an email to be sent to admin account.

    for the moment, I get an OUTPUT in this way. ( my /etc/cron.d/backup looks like this )

    35 13 * * * root /root/scripts/backup.sh > /tmp/backup.out 2>&1

    I always login to the mailbox server and see the OUTPUT which is NOT so PERFECT.

  • Am 21.07.2013 10:42, schrieb Indunil Jayasooriya:

    If you decide a full blown MTA is too heavy weight in your environment on the different nodes (Postfix isn’t the ideal choice for a so called null client), then have a look at ssmtp for example. It is provided by EPEL

    http://pkgs.org/CentOS-6-rhel-6/epel-x86_64/ssmtp-2.61-19.el6.x86_64.rpm.html

    It does exactly what you intend to do.

    There is another null client called msmtp

    http://msmtp.sourceforge.net/

    but that one isn’t available being packaged for CentOS.

    Alexander

  • If I remember correctly, this does require an outside host–that is, you have to use your ISP’s mailserver as relay.

    That compiles very easily on CentOS, however. Again, not sure if it can be used without making use of your ISP’s mail server.

    I have an ancient page (slow site, not always up) on ssmtp, at http://www.scottro.net/qnd/qnd-ssmtp.html. It hasn’t been maintained for awhile, there’s a better page at the arch wiki.

    https://wiki.archlinux.org/index.php/SSMTP

  • Am 21.07.2013 14:56, schrieb Scott Robbins:

    You are correct, a null client sends towards a central mailhub. That will be a properly configured MTA which does everything necessary to be compliant with current mail exchange standards.

    We are in a professional, enterprise environment I presume. Thus the self compilation approach is discouraged. Even if it is a small program which compiles easily. Unless you anyhow manage to maintain your own set of RPMs and your local yum repository.

    Alexander

  • Good point. I see an rpm for Fedora 17, but nothing earlier. I use it on a personal machine–professionally, I’ve never had the need for it.

  • On Sun, 21 Jul 2013 14:12:55 +0530

    I usually do this:

    import SMTPlib
    server = SMTPlib.SMTP(‘smtp.gmail.com:587’)
    server.ehlo()
    server.starttls()
    server.ehlo()
    server.login(‘user@gmail.com’, password)
    server.sendmail(from, to, message_

  • The following script works for us to send notifications to a mobile “server”
    that can not process very long text messages. Unless you have perfect control over who is allowed to use this script and what is being sent, it is VERY bad security practice. Replace the bracketed setup stuff (inside <> ) with the appropriate sources and destinations. “$argv” passess the raw data to be transmitted. Depending on what’s in the message, you may have to modify the 3rd through 6th lines. YMMV.

  • I doubt it queues up mail, it’s very simple and lightweight. It CAN be a useful tool for those who don’t want a large and complicated program lurking there all the time. i’ve no clue how other mail handlers are configured, never having done it, but it for sure beats sendmail all hollow if all you want to do is send an occasional mail and can’t wrap your mind around its configuration.

    Of course, no one is requiring you to use it.

  • This is how I have implemented a “retry” using the cleancode-email program that I mentioned earlier today.

    for(;;)
    {
    FILE *capturestderr;
    system(temp);
    capturestderr=openfile(STDERRCAPTURE,”r”);
    fseek(capturestderr,0L,SEEK_END);
    if (ftell(capturestderr))
    {
    logtime(log);
    fprintf(log,”%s — %s ** SEND FAILED ** on EMAIL MAILOUT, retrying\n”,subscription.name,subscription.email); closefile
    (capturestderr); sleep(2);
    }
    else
    {
    closefile(capturestderr);
    break;
    }
    }
    remove(STDERRCAPTURE);

  • Hi larry,

    I think your method is the easiest way.

    I currently have below cronjob that sends the output to /tmp/backup.out file. it is OK.

    35 13 * * * root /root/scripts/backup.sh > /tmp/backup.out 2>&1

    How can I then send this /tmp/backup.out to an email address.

    Here’s the one I created

    [root@host ~]# ls -al /root/send_email.py
    -rwxr-xr-x. 1 root root 382 2013-07-23 15:18 /root/send_email.py

    [root@host ~]# cat /root/send_email.py
    #!/usr/bin/python

    import SMTPlib from email.MIMEMultipart import MIMEMultipart from email.MIMEText import MIMEText

    from_addr = ‘root@host.example.com’
    to_addrs = [‘myemail@gmail.com’]
    msg = open(‘/tmp/backup.out’,’r’).read()

    server = SMTPlib.SMTP(‘192.168.9.5’, 25)
    server.sendmail(“root@host.example.com”, “myemail@gmail.com”, msg)
    #server.sendmail(from_addr, to_addrs, msg)

    Should I APPEND the PATH of the script (/root/send_email.py) to
    /root/scripts/backup.sh file. ?

    I just ran this script (i.e – /root/send_email.py). It worked. I got the email. But no subject there. How can I add a subject called ” CRON JOB
    OUTPUT ” to the email receive .

    Hope to hear from you.

  • It almost certainly isn’t easier than installing ssmtp. Everything that you’d have to to to get ssmtp to work you will also need to do to that python code, plus you have to manually send email for every job where you need it.

    You’re going to have to create your own RFC compliant headers. See how that’s already more complicated than using ssmtp? (Cron already does that work if you install an MTA, such as ssmtp)

  • @ Indunil Jayasooriya

    IIRC, in one of your follow up post, you mention that you have the Zimbra suite installed and running on *this* machine (which is why you might have had to remove the distro’s default postfix in the first place).

    If above is true then you should try to figure out how to send the cron emails using Zimbra’s MTA (which BTW is also postfix).

    Why are you trying to re-invent the wheel?

    Am I missing something here?

LEAVE A COMMENT