Page 1 of 3 123 LastLast
Results 1 to 10 of 26

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

  1. #1
    Join Date
    Apr 2006
    Beans
    9
    Distro
    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.

    problem
    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.

    solution
    First, look at this diagram from Wikipedia: http://en.wikipedia.org/wiki/Email#Workings. 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)

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

    Install the needed programs
    Code:
    $ 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:
    Code:
    $ mkdir -p ~/etc/.certs
    $ chmod 0700 ~/etc/.certs
    $ cd ~/etc/.certs
    $ wget https://www.verisign.com/support/thawte-roots.zip --no-check-certificate
    $ unzip thawte-roots.zip
    $ cp Thawte\ Server\ Roots/ThawtePremiumServerCA_b64.txt ThawtePremiumServerCA.crt

    Configure msmtp
    Replace UPPERCASE text with your personal settings
    Code:
    $ 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:
    Code:
    # config options: http://msmtp.sourceforge.net/doc/msmtp.html#A-user-configuration-file
    defaults
    logfile /tmp/msmtp.log
    
    
    # isp account
    account isp
    auth login
    host SMTP.YOURISP.COM
    port 25
    user YOURNAME@ISP.COM
    from YOURNAME@ISP.COM
    password *****
    
    
    # gmail account
    account gmail
    auth on
    host smtp.gmail.com
    port 587
    user YOURNAME@gmail.com
    password *****
    from YOURNAME@gmail.com
    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:
    Code:
    $ chmod 600 ~/.msmtprc
    Configure nail
    Code:
    $ gedit ~/.mailrc
    Code:
    # set smtp for nail
    # ref: http://ubuntuforums.org/showpost.php...94&postcount=6
    # docs: http://msmtp.sourceforge.net/doc/msm...guration-files
    
    # isp account (default)
    # $ nail -s "subject line" -a /path/file recipient@email.com < /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 recipient@email.com < /path/body.txt
    account gmail {
    set from="YOURNAME@gmail.com (YOURNAME)"
    set sendmail="/usr/bin/msmtp"
    set message-sendmail-extra-arguments="-a gmail"
    }

    Send test messages for both accounts
    Code:
    $ echo -e "testing email from the command line" > /tmp/test_email
    $ nail -s "isp test" YOURNAME@gmail.com < /tmp/test_email
    $ nail -A gmail -s "gmail test" YOURNAME@gmail.com < /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:
    Code:
    $ gedit /tmp/msmtp.log
    Hope this helps someone. I know I could have used it!
    Original source: http://phosphorusandlime.blogspot.co...ine-email.html
    Last edited by klenwell; March 3rd, 2009 at 06:51 AM. Reason: adding instructions for installing nail on newer versions of Ubuntu

  2. #2
    Join Date
    Dec 2006
    Beans
    6,923

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

    Hi,

    Great guide!!

    Have you considered in your own setup using mutt rather than nail? There are many guides to using this more fully featured program but in blatant self-promotion I post my own here:

    http://ubuntuforums.org/showthread.php?t=565326
    *********************************/mutt.html

    The second link uses msmtp as you have in your guide, a great program for sure. Anyway thanks for making the effort to put this together, nail has always been a bit of a mystery to me and it is good to see a working config.

    Andrew
    You think that's air you're breathing now?

  3. #3
    Join Date
    Apr 2006
    Beans
    9
    Distro
    Ubuntu 7.10 Gutsy Gibbon

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

    Quote Originally Posted by andrew.46 View Post
    Great guide!!

    Have you considered in your own setup using mutt rather than nail? There are many guides to using this more fully featured program but in blatant self-promotion I post my own here:

    http://ubuntuforums.org/showthread.php?t=565326
    *********************************/mutt.html
    Thanks, Andrew. At one point in my many false starts, I installed mutt and even came across your guide (which is very well done). Mutt is just a little more than I needed in this case.

    I have formerly been an advocate for the simple MTA ssmtp, which some would call a Mail Sending Agent (MSA).
    Ha! Just the term I was looking for! I knew it must have its own TLA.

    Tom

  4. #4
    Join Date
    Feb 2006
    Beans
    33

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

    Thanks for this simple and straightforward tutorial mate. I have been looking for something like this for a long time.

    There is, however, one addendum I'd like to make for all the Hardy users out there. It appears that nail is now known as heirloom-mailx in Hardy. Don't fret, though, because it can still be found in the Hardy repositories; just install heirloom-mailx instead of nail:
    Replace
    Code:
    sudo apt-get install nail
    with
    Code:
    sudo apt-get install heirloom-mailx
    Also, when you test the mail sending function, you have to replace
    Code:
    nail -A gmail -s "gmail test" USERNAME@gmail.com < /tmp/test_email
    with
    Code:
    mailx -A gmail -s "gmail test" USERNAME@gmail.com < /tmp/test_email
    Keep up the good work mate, even if it means you have to sacrifice other Friday nights. I know that puking your guts out in some dark alley can be quite entertaining, but the community comes first, right?

  5. #5
    Join Date
    Mar 2005
    Location
    South Africa
    Beans
    55
    Distro
    Ubuntu 11.10 Oneiric Ocelot

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

    Tnx, but I cant find a way to use mailx to send files as attachments to gmail?
    If tried piping through uuencode and mpack but no go.
    Any help please
    Im using kubuntu 8.04 kde 4
    Last edited by HJB; May 5th, 2008 at 10:08 PM.

  6. #6
    Join Date
    Apr 2006
    Beans
    9
    Distro
    Ubuntu 7.10 Gutsy Gibbon

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

    Quote Originally Posted by HJB View Post
    Tnx, but I cant find a way to use mailx to send files as attachments to gmail?
    If tried piping through uuencode and mpack but no go.
    Any help please
    Im using kubuntu 8.04 kde 4
    As a proof of concept, the following worked in nail:

    Code:
    nail -s "isp test" -a /tmp/test_email klenwell@gmail.com < /tmp/test_email
    Google puts some restrictions on the type of files it will deliver so try with a simple case first. Also note that this guide is for nail (mailx-heirloom), so you may need to adjust your config file for mailx.

    I'm not much help on the subject beyond this, but I imagine more details would help anyone who wished to assist.

  7. #7
    Join Date
    May 2008
    Beans
    4

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

    Thank you klenwell for sharing this info with us. And thanks all for commenting it.
    Saves me a lot of time and having fun with my commandline...

  8. #8
    Join Date
    Dec 2007
    Location
    Auckland, New Zealand
    Beans
    7
    Distro
    Kubuntu 9.04 Jaunty Jackalope

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

    Quote Originally Posted by hazica View Post
    There is, however, one addendum I'd like to make for all the Hardy users out there. It appears that nail is now known as heirloom-mailx in Hardy. Don't fret, though, because it can still be found in the Hardy repositories; just install heirloom-mailx instead of nail:
    Replace
    Code:
    sudo apt-get install nail
    with
    Code:
    sudo apt-get install heirloom-mailx
    Thanks, hazica, but my Hardy doesn't see heirloom-mailx, and my mailx doesn't have a -A option, and doesn't understand the mutiple account stuff in the given .mailrc. I wonder if I'm missing a repository that you've used?

  9. #9
    Join Date
    May 2007
    Location
    Townsville, Australia
    Beans
    1,823
    Distro
    Lubuntu 14.04 Trusty Tahr

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

    Quote Originally Posted by klenwell View Post
    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.
    Hope this helps someone. I know I could have used it!


    thanks for the info!!!!
    Ubuntu user # 16304 www.nocleanfeed.com
    If someone asks you to sudo rm -rf anything, don't do it, and don't run any command with rm in it unless you know exactly what you're doing.things i have learnt changing from Xp pro to Ubuntu

  10. #10
    Join Date
    Nov 2006
    Beans
    350
    Distro
    Xubuntu

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

    I can't find heirloom-mailx either. Anybody know where to find it?
    Last edited by r.stiltskin; July 26th, 2008 at 12:32 AM.

Page 1 of 3 123 LastLast

Tags for this Thread

Bookmarks

Posting Permissions

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