Send mail notifications on Linux using the Gmail SMTP server

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

Guide Suppose you want to configure a Linux application to send mail messages from your server or desktop client. mail messages may be email newsletters, status updates (such as cachet), monitoring alerts (such as Monit), disk time (such as RAID mdadm), and so on. When you want to create your own mail sending server to deliver information, you can replace the use of a free public SMTP server to avoid suffering from maintenance.


Google's Gmail service is one of the most reliable free SMTP servers. To send an email notification from your app, simply add your Gmail SMTP server address and your credentials to your app. Using Gmail's SMTP servers comes with some restrictions that are used primarily to stop those who misuse the server frequently to send spam and use email marketing. For example, you can send messages to up to 100 addresses at a time and not more than 500 recipients a day. Similarly, if you don't want to be labeled as spammers, you can't send too many undeliverable messages. When you reach any limit, your Gmail account will be temporarily locked for a day. In short, Gmail's SMTP server is great for your personal use, but not for commercial bulk mail. Having said that, it's time to show you how to use Gmail's SMTP server in a Linux environment.

Google Gmail SMTP server Settings

If you want to send mail through your app using Gmail's SMTP server, keep in mind the detailed instructions that follow. • Mail sending server (SMTP server): smtp.gmail.com use authentication: Yes • use secure connection: Yes • username: your gmail account ID (e.g. "Ali Ce ", if your email is [email protected]) • password: your Gmail password • port: 587 The exact configuration will vary depending on the application. For the remainder of this tutorial, I'll show you some examples of applications that use Gmail's SMTP server on Linux.

send a message from the command line

As a first example, let's try the basic messaging feature: Send a message from the command line using the Gmail SMTP server. To do this, I will use a command-line mail client called Mutt. Install Mutt First: for debian-based systems:

$ sudo apt-get install mutt

For Red Hat based systems:

$ sudo yum install mutt

Create a Mutt configuration file (~/.MUTTRC) and, as follows, specify the Gmail SMTP server information in the file. will be replaced with your Gmail ID. Note that the configuration is only for sending messages (not receiving messages).

$ VI ~/.MUTTRC
Set from = "@gmail. com" Set realname = "Dan Nanni" Set smtp_url = "smtp://@smtp. gmail.com:587/" Set smtp_pass = ""

All ready, send a message using mutt:

$ echo "This was an email body." | Mutt-s "This was an email subject" [Email protected]

To add an attachment to an e-mail message, use the "-a" option

$ echo "This was an email body." | Mutt-s "This was an email subject" [Email protected]-a ~/test_attachment.jpg


Using a Gmail SMTP server means that messages will appear to be 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 as the mail sender, you need to use the Gmail SMTP forwarding service.

send an email notification when the server restarts

If you run some important websites on a virtual private server (VPS), it is recommended to monitor the restart behavior of the VPS. As a more practical example, let's look at how to set up an email notification on your VPS for each reboot event. This assumes that your VPS is using SYSTEMD and shows you how to create a custom SYSTEMD startup service for automatic mail notifications. First create the following script, reboot_notify.sh, to be responsible for mail notifications.

$ sudo vi/usr/local/bin/reboot_notify.sh
#!/bin/shecho "' hostname ' was rebooted on ' date ' | Mutt-f/etc/muttrc-s "Notification on ' hostname '" [email protected]
$ sudo chmod +x/usr/local/bin/reboot_notify.sh

In this script, I use the "-F" option to specify the system-level mutt configuration file location. So don't forget to create the/etc/muttrc file and fill in the Gmail SMTP message as described earlier. Now let's create a custom systemd service like the one below.

$ 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 Rebooteddefaultdependencies=nobefore=reboot.target[service] Type=oneshotexecstart=/usr/local/bin/reboot_notify.sh[install]wantedby=reboot.target

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

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

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

send mail notifications via server usage monitoring

As a last example, let me show a real-life application, Monit, which is an extremely useful server monitoring application. It has full VPS monitoring capabilities (such as CPU, memory, process, file system) and email notifications. If you want to receive email notifications for any events generated by Monit on the VPS, you can add the following SMTP information to the Monit profile.

Set MailServer smtp.gmail.com Port 587    username "" Password ""    using Tlsv12set Mail-format {from: @gmail. com subj ECT: $SERVICE $EVENT at $DATE on $HOST message:monit $ACTION $SERVICE in $EVENT on $DATE: $HOST.       Yours Sincerely, Monit}# the person who'll  receive notification emailsset alert [email protected]

This is an example of a mail notification sent by Monit because of a CPU load overload.

Summary

As you can see, a free SMTP server like Gmail has so many different ways to use it. But again, keep in mind that the free SMTP server is not intended for commercial use and is only applicable to individual projects. No matter which app you are using Gmail SMTP server in, you are welcome to share your use case freely.

Free to provide the latest Linux technology tutorials Books, for open-source technology enthusiasts to do more and better: http://www.linuxprobe.com/

Send mail notifications on Linux using the Gmail SMTP server

Related Article

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.