5 minutes how-to: Sending emails from CLI with Gmail (using mutt)
Don’t bother configuring postfix or sendmail, openssl related stuff and so on when you are in the need of a quick way to send e-mail from CLI ie. for your administrative scripts.
You can accomplish it in a few steps using mutt. If your distribution didn’t come with mutt, go grab it via yum or apt-get or compile from source Once you got mutt, create a .muttrc file under your user home with the following content: [ obviously you must change username and password accordingly with your personal credentials ]
# character set on messages that we send
set send_charset="utf-8"
# personality settings
set realname = "**DESIRED*DISPLAY*NAME**"
set from = "**YOUR*USERNAME**@gmail.com"
# IMAP settings
set imap_user = "**YOUR*USERNAME**@gmail.com"
set imap_pass = "**YOUR*PASSWORD**"
set folder = "imaps://imap.gmail.com:993"
set spoolfile = "+INBOX"
set postponed ="+[Gmail]/Drafts"
set imap_keepalive=900
# use headercache for IMAP
set header_cache =/var/tmp/.mutt/cache/headers
set message_cachedir =/var/tmp/.mutt/cache/bodies
set certificate_file =/var/tmp/.mutt/certificates
# SMTP mailing configuration (for sending mail)
set smtp_url = "smtp://**YOUR*USERNAME**@smtp.gmail.com:587/"
set smtp_pass = "**YOUR*PASSWORD**"
Remember to create the cache path:
$ mkdir -p /var/tmp/.mutt/cache/
You also must accept Google Mail SSL certificate or e-mails will never leave your box. To do so, fire up mutt:
$ mutt
It will present you with this dialog:
Press “a” to (a)ccept always.
Then press “q” to quit mutt.
Now you can test sending an email:
$ mutt -s “Subject” dest@example.com < message.txt
You may also attach a file:
$ mutt -s “subject of the email” dest@example.com < message.txt -a /path/to/attachment/MyDoc.pdf
Obviously at this point you may integrate the above mutt command into your shell scripts: only limit is your imagination. :)