How to Use the Gmail SMTP server to send email notifications on Linux

Source: Internet
Author: User
Tags virtual private server gmail password

How to Use the Gmail SMTP server to send email notifications on Linux

Suppose you want to configure a Linux application to send mail information from your server or Desktop client. Email information may be a briefing, status update (such as Cachet), Monitoring Alarm (such as Monit), disk time (such as RAID mdadm), and so on. When you want to build your own mail sending server to transfer information, you can replace it with a free public SMTP server to avoid maintenance.

Google's Gmail service is one of the most reliable and free SMTP servers. To send email notifications from an application, you only need to add the Gmail SMTP server address and your identity creden。 to the application.

SMTP servers using Gmail may encounter some restrictions, which are mainly used to prevent people who frequently abuse the server to send spam and use email marketing. For example, you can only send messages to up to 100 addresses at a time, and cannot send more than 500 recipients each day. Similarly, if you do not want to be marked as spam senders, you cannot send too many undeliverable emails. When you reach any limit, your Gmail account will be temporarily locked for one day. In short, the SMTP server of Gmail is very good for your personal use, but it is not suitable for Commercial batch mail.

Now, it's time to show you how to use Gmail's SMTP server in Linux.

 

Google Gmail SMTP server settings

If you want to send an email through your application using the SMTP server of Gmail, please keep in mind the following details.

  • Mail sending server (SMTP server): smtp.gmail.com
  • Authentication: Yes
  • Use secure connection: Yes
  • User name: your Gmail account ID (for example, "alice", if your mailbox is alice@gmail.com)
  • Password: your Gmail Password
  • Port 587

The exact configuration varies depending on the application. In the rest of this tutorial, I will show you some examples of using the Gmail SMTP server on Linux.

 

Send email from Command Line

As the first example, let's try the most basic mail function: Use the Gmail SMTP server to send an email from the command line. To this end, I will use a command line mail client called mutt.

Install mutt first:

For Debian-based systems:

  1. $ sudoapt-get install mutt

For Red Hat based systems:

  1. $ sudoyum install mutt

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

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

Everything is ready. Use mutt to send an email:

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

To add an attachment to an email, use the "-a" option.

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

Using the Gmail SMTP server means that the mail is displayed from your Gmail account. In other words, the recipient will regard your Gmail address as the sender address. If you want to use your domain name as the sender of the email, you need to use the Gmail SMTP forwarding service.

 

Send email notification when the server is restarted

If you run important websites on a virtual private server (VPS), we recommend that you monitor the restart of VPS. As a more practical example, let's look at how to create email notifications for each restart event on your VPS. Assume that the VPS uses systemd and shows you how to create a custom systemd startup service for automatic email notification.

First, create the following script reboot_policy.sh for email notification.

  1. $ sudovi/usr/local/bin/reboot_notify.sh
  1. #!/bin/sh
  2. echo"`hostname` was rebooted on `date`"| mutt -F /etc/muttrc -s "Notification on `hostname`" alice@yahoo.com
  1. $ sudochmod+x /usr/local/bin/reboot_notify.sh

In this script, I use the "-F" option to specify the system-level mutt configuration file location. Therefore, do not forget to create the/etc/muttrc file and enter the Gmail SMTP information as described above.

Now let's create a custom systemd service, for example.

  1. $ sudomkdir-p /usr/local/lib/systemd/system
  2. $ sudovi/usr/local/lib/systemd/system/reboot-task.service
  1. [Unit]
  2. Description=Send a notification email when the server gets rebooted
  3. DefaultDependencies=no
  4. Before=reboot.target
  5. [Service]
  6. Type=oneshot
  7. ExecStart=/usr/local/bin/reboot_notify.sh
  8. [Install]
  9. WantedBy=reboot.target

After creating a service, add and start the service.

  1. $ sudosystemctl enable reboot-task
  2. $ sudosystemctl start reboot-task

From now on, you will receive a notification email every time you restart the VPS.

 

Send email notifications through server monitoring

As the last example, let me show a real-life application, Monit, which is an extremely useful server monitoring application. It provides comprehensive VPS monitoring capabilities (such as CPU, memory, processes, file systems) and email notification functions.

If you want to receive email notifications for any event generated by Monit on VPS, you can add the following SMTP information in the Monit configuration file.

  1. set mailserver smtp.gmail.com port 587
  2. username "<your-gmail-ID>" password "<gmail-password>"
  3. using tlsv12
  4. set mail-format {
  5. from:<your-gmail-ID>@gmail.com
  6. subject: $SERVICE $EVENT at $DATE on $HOST
  7. message:Monit $ACTION $SERVICE $EVENT at $DATE on $HOST : $DESCRIPTION.
  8. Yours sincerely,
  9. Monit
  10. }
  11. # the person who will receive notification emails
  12. set alert alice@yahoo.com

This is an example of an email notification sent by Monit due to overload of CPU load.

 

Summary

As you have seen, free SMTP servers like Gmail have so many different usage methods. However, I reiterate that the free SMTP server is not applicable for commercial purposes and only for personal projects. No matter which application you are using the Gmail SMTP server, you are welcome to share your use case freely.

Via: http://xmodulo.com/send-email-notifications-gmail-smtp-server-linux.html

Author: Dan Nanni Translator: cposture Proofreader: martin2011qi, wxy

This article was originally compiled by LCTT and launched with the honor of Linux in China

This article permanently updates the link address:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.