Results 1 to 10 of 26

Thread: How To: Command-Line Email as Simply as Possible

Threaded View

  1. #1
    Join Date
    Apr 2006
    Ubuntu 7.10 Gutsy Gibbon

    How To: Command-Line Email as Simply as Possible

    Update: I'm adding additional instructions for installing nail on newer versions of Ubuntu. You can use mailx as others have suggested later in the thread, but I like nail's -A option, which the instructions below depend on and for which I could not find an equivalent in mailx. I've reposted a complete 6-step guide on my blog.

    I'm copying this from my blog where the markup may be a little neater. I only wish it could be a little simpler. Link at bottom.

    I want to send email using the command line on my laptop Ubuntu system. Mainly for cron jobs and automated backups. I want to be able to do so without having to set up a full-fledged MTA like sendmail or exim. And I want to be able to use either my ISP email account or a Gmail account.

    Seemed simple enough and it probably is for people who do this type of thing for a living. Took me all night. So hopefully this will save someone else (perhaps me again) hours of unnecessary frustration in the future.

    First, look at this diagram from Wikipedia: Without being able to find a simple step-by-step tutorial to guide me, the biggest problem I was having was sorting out what was my MUA, what was my MTA, and what if anything I needed to connect the two. Long story short, they are as follows:

    MUA (the client): nail (you can also use mailx or mutt or what you will)
    MTA (the mail server): your isp or gmail
    MSA (smtp middle man): msmtp (a simple MTA that gets mail from your local MTA to your real MTA or mailhub)

    For newer versions of Ubuntu (post-Gutsy), add Breezy archive to your repository
    $ sudo gedit /etc/apt/sources.list
    Add the following lines at the bottom of the file:
    # breezy repositories (added to install nail)
    # see for more info
    deb breezy universe
    $ sudo apt-get update

    Install the needed programs
    $ sudo apt-get install msmtp
    $ sudo apt-get install nail

    Install Thawte certificate for Gmail
    This is necessary (I think) for Gmail. Probably the most complicated step, though not too bad thanks to instructions here:
    $ mkdir -p ~/etc/.certs
    $ chmod 0700 ~/etc/.certs
    $ cd ~/etc/.certs
    $ wget --no-check-certificate
    $ unzip
    $ cp Thawte\ Server\ Roots/ThawtePremiumServerCA_b64.txt ThawtePremiumServerCA.crt

    Configure msmtp
    Replace UPPERCASE text with your personal settings
    $ gedit ~/.msmtprc
    This will open up an msmtp configuration file where you'll want to copy the following lines, with your correct settings, of course:
    # config options:
    logfile /tmp/msmtp.log
    # isp account
    account isp
    auth login
    port 25
    password *****
    # gmail account
    account gmail
    auth on
    port 587
    password *****
    tls on
    tls_trust_file /home/USER/etc/.certs/ThawtePremiumServerCA.crt
    # set default account to use (from above)
    account default : isp
    Change permission on this file or msmtp will complain:
    $ chmod 600 ~/.msmtprc
    Configure nail
    $ gedit ~/.mailrc
    # set smtp for nail
    # ref:
    # docs:
    # isp account (default)
    # $ nail -s "subject line" -a /path/file < /path/body.txt
    set from="YOURNAME@ISP.COM"
    set sendmail="/usr/bin/msmtp"
    set message-sendmail-extra-arguments="-a isp"
    # gmail account
    # $ nail -A gmail -s "subject line" -a /path/file < /path/body.txt
    account gmail {
    set from=" (YOURNAME)"
    set sendmail="/usr/bin/msmtp"
    set message-sendmail-extra-arguments="-a gmail"

    Send test messages for both accounts
    $ echo -e "testing email from the command line" > /tmp/test_email
    $ nail -s "isp test" < /tmp/test_email
    $ nail -A gmail -s "gmail test" < /tmp/test_email

    Check your gmail account and you should have two new messages -- one from that account and one from your ISP account. To check your log:
    $ gedit /tmp/msmtp.log
    Hope this helps someone. I know I could have used it!
    Original source:
    Last edited by klenwell; March 3rd, 2009 at 06:51 AM. Reason: adding instructions for installing nail on newer versions of Ubuntu

Tags for this Thread


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts