Sometimes you just don’t have the patience to open a GUI. Imagine you are working on a terminal remotely through a very feeble internet connection and after hours of data wrangling you got your results in one small package. Now all you want is to email this 200kb document (average size of a 20k word .txt document). You can either load a GUI, open a browser, open gmail (the login page itself is 2MB), attach the file and send the email or just execute a one line command which does everything for you. With some minimal setup you can do the latter – sending email via CLI just like any other shell command. You can even include this in your scripts (send mail when the script finishes running etc).
We will do this using a terminal program called “mutt” which also has a brilliant CLI interface and will configure it to use gmail via imap. First step is to install mutt using a package manager, (apt/ yum/ pacman for linux and brew for macosx). I am doing this in Arch with pacman. I am installing mutt and smtp-forwarder and then create necessary folders and files for mutt.
sudo paman -S mutt smtp-forwarder mkdir -p ~/.mutt/cache/headers mkdir ~/.mutt/cache/bodies touch ~/.mutt/certificates touch ~/.mutt/muttrc
Edit the muttrc file with your favourite text editor and add these configurations, (make sure to change the username to your username and if your are using two factor authentication with gmail the password has to be generated from App passwords.
set ssl_starttls=yes set ssl_force_tls=yes set imap_user = 'username@gmail.com' set imap_pass = 'yourpassword' set from= 'username@gmail.com' set realname='yourname' set folder = imaps://imap.gmail.com/ set spoolfile = imaps://imap.gmail.com/INBOX set postponed="imaps://imap.gmail.com/[Gmail]/Drafts" set header_cache = "~/.mutt/cache/headers" set message_cachedir = "~/.mutt/cache/bodies" set certificate_file = "~/.mutt/certificates" set smtp_url = 'smtps://username@smtp.gmail.com:465/' set imap_pass = 'yourpassword' set move = no set imap_keepalive = 900 set editor = vim bind pager j next-line bind pager k previous-line set sort = threads set sort_aux = reverse-date-sent unset imap_passive set imap_check_subscribed set mail_check=60 set timeout=10
That is it! Now we can send mail from terminal by just passing some text or a file with the text to the mutt command,
echo "email body" | mutt -s "email-subject" -- recipient@gmail.com mutt -s "email-subject" -- recipient@gmail.com < file_with_body_text.txt
we can even attach files like this,
echo "please find attached" | mutt -s "email-subject" -a "attachment.pdf" -- recipient@gmail.com