Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Gmail's free SMTP service to send monitoring notifications on Linux servers

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "how to send monitoring notifications on Linux servers using Gmail's free SMTP service". In daily operations, it is believed that many people have doubts about how to use Gmail's free SMTP services to send monitoring notifications on Linux servers. The editor consulted all kinds of materials and sorted out simple and useful methods of operation. I hope it will be helpful to answer the question of "how to use Gmail's free SMTP service to send monitoring notifications on Linux servers"! Next, please follow the editor to study!

Google Gmail SMTP server settings

If you want to use Gmail's SMTP server to send emails through your application, please keep in mind the detailed instructions below.

Mail sending server (SMTP server): smtp.gmail.com

Use Certification: yes

Use secure connection: yes

User name: your Gmail account ID (e.g. "alice", if your email address 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'll show you some examples of applications that use Gmail SMTP servers on Linux.

Send mail from the command line

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

Install mutt first:

For Debian-based systems:

The code is as follows:

$sudo apt-get install mutt

For Red Hat based systems:

The code is as follows:

$sudo yum install mutt

Create a mutt configuration file (~ / .muttrc) and specify the Gmail SMTP server information in the file as below. Will be replaced with your own Gmail ID. Note that this configuration is only for sending (not receiving) mail.

The code is as follows:

$vi ~ / .muttrc

Set from = "@ gmail.com"

Set realname = "Dan Nanni"

Set smtp_url = "smtp://@smtp.gmail.com:587/"

Set smtp_pass = ""

When everything is ready, use mutt to send an email:

The code is as follows:

$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

The code is as follows:

$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 email will show that it was sent from your Gmail account. In other words, the recipient will treat your Gmail address as the sender's address. If you want to use your own domain name as the sender, you need to use the Gmail SMTP forwarding service.

Send an email notification when the server is restarted

If you run some important websites on the virtual private server (VPS), it is recommended to monitor the restart behavior of VPS. As a more practical example, let's look at how to create email notifications for each restart event on your VPS. This assumes that you are using systemd on your VPS and shows you how to create a custom systemd startup service for automatic email notifications.

First create the following script reboot_notify.sh, which is responsible for mail notifications.

The code is as follows:

$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 this script, I use the "- F" option to specify the location of the system-level mutt configuration file. So don't forget to create the / etc/muttrc file and fill in the Gmail SMTP information as described earlier.

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

The code is 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

After the service is created, add and start the service.

The code is as follows:

$sudo systemctl enable reboot-task

$sudo systemctl start reboot-task

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

Send email notifications through the server using monitoring

As a final example, let me show you a real-life application, Monit, which is an extremely useful server monitoring application. It has comprehensive VPS monitoring capabilities (such as CPU, memory, processes, file systems) and mail notification capabilities.

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

The code is as follows:

Set mailserver smtp.gmail.com port 587

Username "" password ""

Using tlsv12

Set mail-format {

From: @ 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

This is an example of an email notification sent by CPU because of its overload.

At this point, the study on "how to use Gmail's free SMTP service to send monitoring notifications on Linux servers" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report