sending email from command-line

#################################################
IMPORTANT UPDATE: this post has been deprecated, please refer to this post instead: exim4 and gmail
#################################################

to send email from command line (“mail -s subject recipient <content>”), you need:

  1. to install bsd-mailx package (sudo apt-get install bsd-mailx)
  2. at this point if you try to send an email, it won’t work (once you’ve sent it, chect /var/mail/<user> to see error msg (“cat /var/mail/$USER | less”), you should find a mailing to remote domains not supported error msg.
  3. it’s because you need a send mail program (sendmail, qmail, postfix, exim4? (=> best is either exim4 or postfix — qmail is outdated)), we will install exim4
  4. sudo apt-get install exim4
  5. now we need to configure exim4 to allow it to send emails. Since we’re on a home desktop computer, with dynamic ip-address, and since mail coming from dynamic-ip addresses are considered spam by some mail user agents, we will configure exim4 to send email through one of our email accounts (called “smarthost”), in our case gmail.
  6. edit /etc/exim4/update-exim4.conf.conf and set:
    "dc_smarthost='smtp.gmail.com'"
    "dc_eximconfig_configtype='smarthost'"
  7. edit /etc/exim4/passwd.client and add the follownig two lines (without the second line, it did not work for me):
    smtp.gmail.com : rdam...@gmail.com : passwd
    gmail-smtp.l.google.com : rdam...@gmail.com: passwd
  8. run “sudo update-exim4.conf”
  9. enable pop support (if not already done) in your gmail account (log into your account, click on “setting” / “Forwarding and POP/IMAP”, click on “enable pop” (any) then save changes
  10. try it: “mail -s “test” youremailaddress <your_content>” (ctrl+D on blank line to validate your body content) and watch your gmail account, you should have received your email by now (to debug ‘mail’ command, add ‘-v’ option)

summary

first enable pop support from your email account (gmail, hotmail, laposte.net…) , then run the following commands:

sudo apt-get install exim4 bsd-mailx
sudo vi /etc/exim4/update-exim4.conf.conf
    -- set the following lines --
    dc_eximconfig_configtype='smarthost'
    dc_smarthost='smtp.gmail.com'
sudo vi /etc/exim4/passwd.client
    -- set the following lines --
    smtp.gmail.com : rdam...@gmail.com : passwd
    gmail-smtp.l.google.com : rdam...@gmail.com: passwd
sudo update-exim4.conf

That should work by now.
[sources]

Leave a Reply