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:
$ sudoapt-get install mutt
For Red Hat based systems:
$ 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 ).
$ vi~/.muttrc
setfrom="<gmail-id>@gmail.com"
set realname ="Dan Nanni"
set smtp_url ="smtp://<gmail-id>@smtp.gmail.com:587/"
set smtp_pass ="<gmail-password>"
Everything is ready. Use mutt to send an email:
$ 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.
$ 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.
$ sudovi/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
$ 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.
$ sudomkdir-p /usr/local/lib/systemd/system
$ sudovi/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 creating a service, add and start the service.
$ sudosystemctl enable reboot-task
$ 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.
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
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: