Showing posts with label ssmtp. Show all posts
Showing posts with label ssmtp. Show all posts

Thursday, May 15, 2014

ssmtp to email cron output

Cron output in Ubuntu gets discarded. The default install of Ubuntu does not install a Mail Transport Agent (MTA), so cron cannot mail you the output.

You can install an MTA -several are easily available in the Ubuntu repositories- but configuration is not trivial. MTAs date from before the IMAP- and SMTP-based e-mail systems we use today, which makes them highly flexible and configurable...but not necessarily the easiest solution.

ssmtp is a limited-purpose MTA intended to replace the 'mail' command and send all output to an SMTP mailserver. ssmtp is provided by the 'ssmtp' package.

Install


sudo apt-get install ssmtp

Configuration file #1: /etc/ssmtp/ssmpt.conf

This tells ssmtp how to send email to my account.
It's not the best idea to store your e-mail password in world-readable plain text. See how to protect the password properly.

# Config file for sSMTP sendmail
#
# The person who gets all mail for userids < 1000
# Make this empty to disable rewriting.
root=me@example.com

# The place where the mail goes. The actual machine name is required no
# MX records are consulted. Commonly mailhosts are named mail.domain.com
mailhub=my_mail_server.net:465
AuthUser=me@example.com
AuthPass=my_email_password
UseTLS=Yes

# Where will the mail seem to come from?
rewriteDomain=example.com

# The full hostname
hostname=hey_its_my_computer

# Are users allowed to set their own From: address?
# YES - Allow the user to specify their own From: address
# NO - Use the system generated From: address
FromLineOverride=YES


Configuration File #2: /etc/ssmtp/

This directs root mail to my account.

# sSMTP aliases
#
# Format:       local_account:outgoing_address:mailhub
#
# Example: root:your_login@your.domain:mailhub.your.domain[:port]
# where [:port] is an optional port number that defaults to 25.
root:me@example.com:my_mail_server.net:465



And now cron output lands in my normal inbox.

Sunday, June 1, 2008

Sending e-mail from the command line

I want to send automatic e-mail as a cron job from my Xubuntu 8.04 laptop. Originally, I tried routing it through the legacy mail spools - it is, after all, on the same laptop as my e-mail - but gave up in despair. Thanks to Ubuntu Forums for the info.

  1. sudo apt-get install ssmtp heirloom-mailx
    or install ssmtp and nail using Synaptic.
  2. sudo mousepad /etc/ssmtp/ssmtp.conf to edit the ssmtp config file. Modify or add the following lines to the config file:
    mailhub=mail.mymailserver.com
    AuthUser=me@mymailserver.com
    AuthPass=my_mail_password
    
    The nail config file does not need to be changed at all.
  3. Using the nail command to send e-mail:
    Tip: Pay close attention to the <RETURN>s and <CTRL+D>s
    First line: nail recipient@address.com<RETURN>
    Second line: Subject: subject<RETURN>
    Third line: E-mail body text<CTRL+D>

    Example:
    nail me@virusmagnet.com
    Subject: Lotta viruses coming into this account
    I need to stop responding to the spam.
  4. Generating e-mail as part of a script or cron job:

    Script nail -s "Test 3" recipient@email.com < /tmp/test_email will send the following e-mail.
    From: You (automatic)
    To: recipient@email.com
    Subject: Test 3
    Message Body: (read from file /tmp/test_email)


    nail -s "Test 3" recipient@email.com < /dev/null will send an e-mail with a blank body - good for quick reminders using the subject line only.

    To generate e-mail from cron events, use the command crontab -e to edit your crontab file:

    26 21 * * * nail -s "Test 3" recipient@email.com < /tmp/test_email

    This crontab entry will send the same "Test 3" e-mail every day at 9:26 p.m.

Many elements can be added or customized - this is simple enough to generate automatic e-mails to myself.

UPDATE: September 9, 2008: I could do this in evolution, but it's more fun to use cron and nail or zenity
This crontab entry:

0 6 28 * * /usr/bin/nail -s "Reminder bot: Pay The Store Rent!" me@example.com </dev/null

will send the following e-mail at 6:00 am on the 28th of each month,

From:    me <my computer>
To:      me@example.com
Subject: Reminder bot: Pay The Store Rent!
Date:    Thu, 28 Jul 2008 06:00:01 -0500 
Text:    None
So I won't forget to pay the rent.... It's important.
This sends an e-mail to a known (to nail) e-mail server, so all machines that check the account get the mail.
Alternately, if I want a a pop-up window:

0 6 28 * * DISPLAY=:0.0 /usr/bin/zenity --info --title "Reminder Bot" --text "Pay The Store Rent"

will give me a pop-up window instead, and

0 6 28 * * DISPLAY=:0.0 /usr/bin/zenity --notification --text "Reminder Bot: Pay The Store Rent"

will give me a tiny notification-area icon.