Step 1 – Install msmtp.
Step 2 – Configure msmtp.
Create and open a file called .msmtprc in your home directory.
Paste the following into this file. Replace username@gmail.com in both places with your gmail address and also replace yourpassword with your gmail password. When you have finished, save and close the file.
defaults logfile ~/msmtp.log account gmail auth on host smtp.gmail.com from username@gmail.com auth on tls on tls_trust_file /etc/ssl/certs/ca-certificates.crt user username@gmail.com password yourpassword port 587 account default : gmail
Since your password is saved as plain text in this file, run the following command to keep other users from viewing it.
Step 3 – Install heirloom-mailx.
You will most likely need to add a repository to install this. First open /etc/apt/sources.list with this command. You will need to enter your password twice if this is your first time running an admin command since your last session. This is normal.
Paste the following into the bottom of the file, then save and close the file.
deb http://security.ubuntu.com/ubuntu trusty-security main universe
Now update your repositories.
Step 4 – Configure mailx.
Create and open a file called .mailrc in your home directory.
Paste this into the file, then save and close it.
set sendmail="/usr/bin/msmtp" set message-sendmail-extra-arguments="-a gmail"
Step 5 – Login to your gmail account to allow less secure apps.
Follow this link -> https://myaccount.google.com/lesssecureapps to allow less secure apps to run. This needs to be turned on in order to send emails with this method.
You should now be able to send emails from the command line.
Change username@whatever.com to the recipient’s address. After you press the Enter key to run this command, it will allow you to write the body of the message. When you have finished writing the body of the message, press and hold CTRL + d to send the message. The terminal will briefly display EOT to let you know your message has been sent and then your terminal will exit.
The following commands will not require you to press CTRL + d to send, making them ideal for scripts.
You can insert the contents of a text file into the body of your message with the command below.
You can pipe the body into the email with the echo command.
You can send a message to multiple recipients like this.
And you can add an attachment like this.
I hope this tutorial has been helpful. This is probably my last post for the year. I’ll see you all in 2020!