DEV Community

Cover image for Automatically send emails using Google SMTP with 3 easy steps
Stephen Whitmore
Stephen Whitmore

Posted on • Updated on

Automatically send emails using Google SMTP with 3 easy steps

If you need to automate tasks then you'll likely find this post useful. I like to be notified if an automated script fails or succeeds, depending on the situation.

You can get your scripts to email you using Google's SMTP server:

1. Set up a Gmail Password

Assuming you have a Google account and are signed in, visit https://myaccount.google.com/apppasswords.

Select "Other" from the Select app drop down:

Google app password landing page

Give the app password a name. It can be whatever. Click "Generate" and you'll see something like this:

Google app password generated

Save this somewhere else it shall be lost to the void.

When you click done your new app password will show up for you:

Google app password saved

2. Install mutt

If you're on Linux your distro will likely have the mutt package available in its repositories:

Fedora/RHEL/CentOS

sudo dnf install mutt

or

sudo yum install mutt

Ubuntu/Debian

sudo apt install mutt

Arch/Manjaro

sudo pacman -S mutt

Bonus: mutt is also available on MacOS via Homebrew:

brew install mutt

3. Configure mutt

Create a .muttrc file in your home directory.

vi ~/.muttrc

Fill it out with the configs, pasting in your app password from step 1 for the smtp_pass value:

set from = "<gmail-id>@gmail.com"
set realname = "<Your Name>"
set smtp_url = "smtp://<gmail-id>@smtp.gmail.com:587/"
set smtp_pass = "<smtp password generated in the google link above>"
Enter fullscreen mode Exit fullscreen mode

In our case "smtp password generated in the google link above" is vaykiyyakisiqmez.

Now test it out -

echo "This is an email body." | mutt -s "This is an email subject" recipient@example.com

Tada! 🎉

Top comments (1)

Collapse
 
jcthomas profile image
John Thomas

Extremely helpful thanks. Might be worth adding that a GMail account needs 2-factor to enable app passwords. The error page for the new account I set up just said "feature unavailable" with no explanation, so I spent hours getting sucked into Google workspace nonsense before I came back to this solution.