DEV Community

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

Posted on • Edited on

8

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! 🎉

AWS Q Developer image

Your AI Code Assistant

Implement features, document your code, or refactor your projects.
Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

Top comments (2)

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.

Collapse
 
aniketshitole profile image
Aniket Shitole

It's really very helpful. I've watched tons of videos on youtube, but couldn't find a solution. I've created my account on this platform just now, so just I can comment here.

Eliminate Context Switching and Maximize Productivity

Pieces.app

Pieces Copilot is your personalized workflow assistant, working alongside your favorite apps. Ask questions about entire repositories, generate contextualized code, save and reuse useful snippets, and streamline your development process.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay