Sending Mails from your Ubuntu Server

April 2020

How can I send mails from the command line?

The first thing you'll need is a gmail account, that is dedicated to this purpose. So go ahead and create one. Then what you need to do is to also allow "less secure apps" to send e-mails. You can do that at https://myaccount.google.com/lesssecureapps.

After having set up your Gmail Account for sending mails, you can log in to your server and run through the following steps.

  1. sudo apt-get install ssmtp mailutils
  2. Open the following file in your favorite text editor (vim, nano, ...):
    nano /etc/ssmtp/ssmtp.conf
  3. Append the following lines:
    root=username@gmail.com
    mailhub=smtp.gmail.com:465
    rewriteDomain=gmail.com
    AuthUser=username@gmail.com
    AuthPass=password
    FromLineOverride=YES
    UseTLS=YES
  4. Finally, send mails like this:
    echo "Hi there!" | mail -s "My Subject" recipient@example.com

Interested in web development?