Sending emails from cmd line

2024-09-01

You can send emails from cmd line in Ubuntu using mutt and gmail (which is useful for scripting notifications for example) as follows.

  1. Set up Gmail app password

    • Google account -> Security -> Enable 2-Step Verification
    • Google account -> Security -> 2-Step Verification -> App passwords -> Create
    • App password should be a 16-character code
  2. Set up mutt

    • Install mutt
    sudo apt-get install mutt
    
    • Create mutt config file ~/.muttrc with the following content
    set from = "{your-email}@gmail.com"
    set realname = "{sender}"
    set smtp_url = "smtp://{your-email}@[email protected]:587/"
    set smtp_pass = "{Gmail app password}"
    set ssl_starttls = yes
    set ssl_force_tls = yes
    

    Make sure to keep Gmail app password as it is, do not remove spaces. It should be **** **** **** ****.

  3. Test

echo "Hello there" | mutt -s "test subject" recipient@example.com

Using mutt to mail to your designated sms email is basically how you programmatically send SMS notifications.