How to send email notifications using Gmail SMTP server on Linux

Suppose you want to configure a Linux app to send out email messages from your server or desktop. The email messages can be part of email newsletters, status updates (e.g., Cachet), monitoring alerts (e.g., Monit), disk events (e.g., RAID mdadm), and so on. While you can set up your own outgoing mail server to deliver messages, you can alternatively rely on a freely available public SMTP server as a maintenance-free option.

One of the most reliable free SMTP servers is from Google's Gmail service. All you have to do to send email notifications within your app is to add Gmail's SMTP server address and your credentials to the app, and you are good to go.

One catch with using Gmail's SMTP server is that there are various restrictions in place, mainly to combat spammers and email marketers who often abuse the server. For example, you can send messages to no more than 100 addresses at once, and no more than 500 recipients per day. Also, if you don't want to be flagged as a spammer, you cannot send a large number of undeliverable messages. When any of these limitations is reached, your Gmail account will temporarily be locked out for a day. In short, Gmail's SMTP server is perfectly fine for your personal use, but not meant for commercial bulk emails.

With that being said, let me demonstrate how to use Gmail's SMTP server in Linux environment.

Google Gmail SMTP Server Setting

If you want to send emails from your app using Gmail's SMTP server, you need to adjust the security setting of the Gmail account to be used. Go to the Google account settings, and enable the option to allow less secure apps, which is off by default.

Then you will need to provide your app with the following details.

  • Outgoing mail server (SMTP server): smtp.gmail.com
  • Use authentication: yes
  • Use secure connection: yes
  • Username: your Gmail account ID (e.g., "alice" if your email is alice@gmail.com)
  • Password: your Gmail password
  • Port: 587 (TLS) or 465 (SSL)

Exact configuration syntax may vary depending on apps. In the rest of this tutorial, I will show you several useful examples of using Gmail SMTP server in Linux.

Send Emails from the Command Line

As the first example, let's try the most basic email functionality: send an email from the command line using Gmail SMTP server. For this, I am going to use a command-line email client called Mutt.

First, install Mutt as follows.

For Debian-based system:

$ sudo apt-get install mutt

For Red Hat based system:

$ sudo yum install mutt

Create a mutt configuration file (~/.muttrc) and specify in the file Gmail SMTP server information as follows. Replace <gmail-id> with your own Gmail ID. Note that this configuration is for sending emails only (not receiving emails).

$ vi ~/.muttrc
set from = "<gmail-id>@gmail.com"
set realname = "Dan Nanni"
set smtp_url = "smtp://<gmail-id>@smtp.gmail.com:587/"
set smtp_pass = "<gmail-password>"

Now you are ready to send out an email using mutt:

$ echo "This is an email body." | mutt -s "This is an email subject" alice@yahoo.com

To attach a file in an email, use "-a" option:

$ echo "This is an email body." | mutt -s "This is an email subject" alice@yahoo.com -a ~/test_attachment.jpg

Using Gmail SMTP server means that the emails appear as sent from your Gmail account. In other words, a recipient will see your Gmail address as the sender's address. If you want to use your domain as the email sender, you need to use Gmail SMTP relay service instead.

Another note is that if you are concerned about placing your SMTP password in the plain-text .muttrc file, you can encrypt the password using GnuPG.

Send Email Notification When a Server is Rebooted

If you are running a virtual private server (VPS) for some critical website, one recommendation is to monitor VPS reboot activities. As a more practical example, let's consider how to set up email notifications for every reboot event on your VPS. Here I assume you are using systemd on your VPS, and show you how to create a custom systemd boot-time service for automatic email notifications.

First create the following script reboot_notify.sh which takes care of email notifications.

$ sudo vi /usr/local/bin/reboot_notify.sh
#!/bin/sh

echo "`hostname` was rebooted on `date`" | mutt -F /etc/muttrc -s "Notification on `hostname`" alice@yahoo.com
$ sudo chmod +x /usr/local/bin/reboot_notify.sh

In the script, I use "-F" option to specify the location of system-wide mutt configuration file. So don't forget to create /etc/muttrc file and populate Gmail SMTP information as described earlier.

Now let's create a custom systemd service as follows.

$ sudo mkdir -p /usr/local/lib/systemd/system
$ sudo vi /usr/local/lib/systemd/system/reboot-task.service
[Unit]
Description=Send a notification email when the server gets rebooted
DefaultDependencies=no
Before=reboot.target

[Service]
Type=oneshot
ExecStart=/usr/local/bin/reboot_notify.sh

[Install]
WantedBy=reboot.target

Once the service file is created, enable and start the service.

$ sudo systemctl enable reboot-task
$ sudo systemctl start reboot-task

From now on, you will be receiving a notification email every time the VPS gets rebooted.

Send Email Notification from Server Usage Monitoring

As a final example, let me present a real-world application called Monit, which is a pretty useful server monitoring application. It comes with comprehensive VPS monitoring capabilities (e.g., CPU, memory, processes, file system), as well as email notification functions.

If you want to receive email notifications for any event on your VPS (e.g., server overload) generated by Monit, you can add the following SMTP information to Monit configuration file.

set mailserver smtp.gmail.com port 587
    username "<your-gmail-ID>" password "<gmail-password>"
    using tlsv12

set mail-format {
 from: <your-gmail-ID>@gmail.com
 subject: $SERVICE $EVENT at $DATE on $HOST
 message: Monit $ACTION $SERVICE $EVENT at $DATE on $HOST : $DESCRIPTION.

       Yours sincerely,
          Monit
  }

# the person who will receive notification emails
set alert alice@yahoo.com

Here is the example email notification sent by Monit for excessive CPU load.

Conclusion

As you can imagine, there will be so many different ways to take advantage of free SMTP servers like Gmail. But once again, remember that the free SMTP server is not meant for commercial usage, but only for your own personal project. Also, for security reasons, it is a probably good idea to create a throw-away Gmail address just for email notifications, instead of using your own personal Gmail account. If you are using Gmail SMTP server inside any app, feel free to share your use case.

Subscribe to Xmodulo

Do you want to receive Linux FAQs, detailed tutorials and tips published at Xmodulo? Enter your email address below, and we will deliver our Linux posts straight to your email box, for free. Delivery powered by Google Feedburner.


Support Xmodulo

Did you find this tutorial helpful? Then please be generous and support Xmodulo!

The following two tabs change content below.
Dan Nanni is the founder and also a regular contributor of Xmodulo.com. He is a Linux/FOSS enthusiast who loves to get his hands dirty with his Linux box. He likes to procrastinate when he is supposed to be busy and productive. When he is otherwise free, he likes to watch movies and shop for the coolest gadgets.

8 thoughts on “How to send email notifications using Gmail SMTP server on Linux

  1. If you use GMail SMTP with twofactor-authentication, you should create an unique application password for your SMTP access.

  2. If i remember correctly this wont work without changing some security settings in Gmail to allow SMTP with password auth and then use an application password (even without two factor auth). Or something like that :-) at least Google wouldn't allow SMTP with user password auth by default last time i checked.

  3. Configure your mail server to use Gmail as a relay. Dozens of guides, takes just as long as the solution in the tutorial and faster than any other comment suggestions and it works without any additional configuration for anything that wants to send emails from your machine.

  4. The instructions are very clear and precise. One small details bothers me a little bit.

    The GMAIL password is stored as a plain text in the .muttrc file. Anybody who has access to the machine (telnet, ssh, rsh) can read the .muttrc file.

    Is there a way to send a mail via GMAIL SMTP service without a password once the password was entered once?

Leave a comment

Your email address will not be published. Required fields are marked *