Skip to content

Instantly share code, notes, and snippets.

@jarun
Last active September 2, 2017 18:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jarun/5adc5bc1451b64179107d1a3a64f2b6b to your computer and use it in GitHub Desktop.
Save jarun/5adc5bc1451b64179107d1a3a64f2b6b to your computer and use it in GitHub Desktop.
How to send emails (with Gmail as SMTP server) from the cmdline when 2-Step Verification is ON

Generate an App password for your Google account. Select the custom option from the drop-down menu. The password will work with any of the utilities below.

Compose, 'sendemail' with attachment

  1. Install sendemail and dependencies

    sudo apt install libio-socket-ssl-perl libnet-ssleay-perl libterm-readkey-perl sendemail
    
  2. Apply patch1 to simplify the mailing experience

    sudo patch /usr/bin/sendEmail < sendEmail-1.56-5.patch
    
  3. Apply patch2 to add some colored confirmation when mail is sent

    sudo patch /usr/bin/sendEmail < sendEmail-1.56-5_color.patch
    
  4. Add the following alias in your shell's rc file

    alias mail='sendemail -f yourmail@gmail.com -s smtp.gmail.com:587 -o tls=yes -xu yourmail -t '
    
  5. Send a mail with 2 attachments

    mail johndoe@somedomain.com -a attachment1.pdf attachment2.mp3
    

Send emails using ssmtp

  1. Install ssmtp

    sudo apt install ssmtp
    
  2. Configure ssmtp (/etc/ssmtp/ssmtp.conf)

root=yourmail@gmail.com
mailhub=smtp.gmail.com:465
FromLineOverride=YES
AuthUser=yourmail@gmail.com
AuthPass=generated_app_password
UseTLS=YES
  1. Send mails from the cmdline

Send attachments using mpack

mpack -s "sub: attachment" attachment_file johndoe@somedomain.com
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment