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. Google has removed the old "less secure apps" option, so you'll need an app password instead: turn on 2-Step Verification for the account, then create an app password at https://myaccount.google.com/apppasswords.
After having set up your Gmail Account for sending mails, you can log in to your server and run through the following steps.
-
sudo apt-get install ssmtp mailutils
-
Open the following file in your favorite text editor (vim, nano, ...):
nano /etc/ssmtp/ssmtp.conf
-
Append the following lines:
root=username@gmail.com mailhub=smtp.gmail.com:465 rewriteDomain=gmail.com AuthUser=username@gmail.com AuthPass=your-app-password FromLineOverride=YES UseTLS=YES
-
Finally, send mails like this:
echo "Hi there!" | mail -s "My Subject" recipient@example.com