gmail 设置 smtp_如何在Linux上使用Gmail或SMTP设置电子邮件警报

gmail 设置 smtp

gmail 设置 smtp

Linux machines may require administrative intervention in countless ways, but without manually logging into them how would you know about it? Here’s how to setup emails to get notified when your machines want some tender love and attention.

Linux机器可能需要无数种方式进行管理干预,但是如果不手动登录它们,您将如何知道呢? 当您的机器需要一些温柔的关怀和关注时,这是设置电子邮件以得到通知的方法。

Of course, this technique is meant for real servers, but if you’ve got a Linux box sitting in your house acting as a home server, you can use it there as well. In fact, since many home ISPs block regular outbound email, you might find this technique a great way to ensure you still get administration emails, even from your home servers.

当然,该技术适用于真实服务器,但是如果您在家中安装了Linux盒子作为家庭服务器,则也可以在其中使用它。 实际上,由于许多家庭ISP阻止常规的出站电子邮件,因此您可能会发现此技术是确保即使从家庭服务器中仍能收到管理电子邮件的好方法。

总览 (Overview)

Configuring the Email sending ability for a machine immediately gives us the upshot that a lot of the system’s administration problems and critical alerts will be automatically sent to that system’s designated administrator. The most noteworthy example is that cron job execution errors fall into this category and therefore will notify the admin that there was a problem with scheduled jobs such as backups as soon as they happen including the standard error (stderr) output in the report. We will also be able to incorporate emailing from bash scripts.

为一台机器配置电子邮件发送功能后,我们可以Swift将许多系统的管理问题和严重警报自动发送给该系统的指定管理员。 最值得一提的示例是cron作业执行错误属于此类,因此将在计划的作业(例如备份)发生时立即通知管理员,包括报告中的标准错误(stderr)输出。 我们还将能够合并来自bash脚本的电子邮件。

Don’t worry—it usually doesn’t spam you with too much information and you can just de-configure it if it becomes to annoying.

不用担心-它通常不会向您发送过多的信息,如果您感到烦恼,则可以取消配置它。

On Debian based systems, other then a lot of very versatile emailing related programs there is also the ssmtp package, which is well suited if all you want to do is have an agent to send emails using another email server (MTA), without having to setup an entire postfix or sendmail configuration to do so.

在基于Debian的系统上,除了很多非常通用的电子邮件相关程序之外,还有ssmtp软件包,如果您要做的只是让代理使用另一个电子邮件服务器(MTA)发送电子邮件,该软件包非常适合。设置整个postfix或sendmail配置。

先决条件和假设 (Prerequisites and assumptions)

Before we get started, you’ll want to make sure that all of these conditions are met:

在开始之前,您需要确保满足所有以下条件:

  • You have an SMTP server that can receive the emails from your machines and send them to the recipient (i.e. your corporate exchange or Gmail).

    您有一台SMTP服务器,可以从您的计算机接收电子邮件并将其发送给收件人(即您的公司交易所或Gmail)。
  • You have the credentials for a user that is able to send Email on that server (i.e. a mailbox or a Gmail account).

    您具有能够在该服务器上发送电子邮件的用户(即邮箱或Gmail帐户)的凭据。
  • This procedure has been used and tested on *Debian based systems (Lenny, Ubuntu & Mint), so if your not on one of those distributions your mileage may vary.

    此过程已在基于* Debian的系统(Lenny,Ubuntu和Mint)上使用和测试,因此,如果您不在这些发行版之一上,则里程可能会有所不同。
  • You will see me use VIM as the editor program, this is just because I’m used to it… you may use any other editor that you’d like.

    您会看到我将VIM用作编辑器程序,这仅仅是因为我已经习惯了……您可以使用任何其他想要的编辑器。

*It is also used and tested on DD-WRT, but will only be covered in a future article (update: isn’t the future fun?) due to the setup and configuration variations necessary for such an embedded platform.

*它还可以在DD-WRT上使用和测试,但由于此类嵌入式平台所需设置和配置不同,因此只会在以后的文章中进行介绍(更新:将来是否有趣?)。

建立 (Setup)

To install the ssmtp (Simple S.M.T.P) package, use the following command:

要安装ssmtp(简单SMTP)软件包,请使用以下命令:

sudo aptitude install ssmtp

sudo aptitude install ssmtp

Then edit the configuration file:

然后编辑配置文件:

sudo vim /etc/ssmtp/ssmtp.conf

sudo vim /etc/ssmtp/ssmtp.conf

Adjust and add as necessary the  following parameters:

调整并根据需要添加以下参数:

  • root=username@gmail.com

    root=username@gmail.com

    Change it from postmaster to the machines admin’s Email.

    将其从postmaster更改为机器管理员的Email。

  • mailhub=smtp.gmail.com:587

    mailhub=smtp.gmail.com:587

    Your mail server in our case this is Gmail so we have to specify the port as 587, for regular SMTP servers this is usually not necessary.

    在本例中,您的邮件服务器是Gmail,因此我们必须将端口指定为587,对于常规SMTP服务器,通常不需要这样做。

  • hostname=username@gmail.com

    hostname=username@gmail.com

    Usually the name of the machine is automatically filled by the package setup, if the machine has a mailbox this should be fine, but if it doesn’t or the name is not the same as the mailbox adjust accordingly.

    通常,计算机名称由软件包设置自动填充,如果计算机具有邮箱,则应该可以,但是如果没有邮箱,或者名称与邮箱名称不同,则进行相应的调整。

  • UseSTARTTLS=YES

    UseSTARTTLS=YES

    Enable TLS for secure session communication.

    启用TLS以进行安全的会话通信。

  • AuthUser=username

    AuthUser=username

    The username of the sending mailbox.

    发送邮箱的用户名。

  • AuthPass=password

    AuthPass=password

    The password of the sending mailbox..

    发送邮箱的密码。

  • FromLineOverride=yes

    FromLineOverride=yes

    Sends the hostname instead of root[root@hostname.FQDN].

    发送主机名而不是root [root@hostname.FQDN]。

In order to make the default (root) “from” field be the server name, edit the /etc/ssmtp/revaliases file:

为了使默认的(根)“ from”字段成为服务器名称,请编辑/ etc / ssmtp / revaliases文件:

sudo vim /etc/ssmtp/revaliases

sudo vim /etc/ssmtp/revaliases

And add into it the desired translation which in our Gmail examples case will be:

并在其中添加所需的翻译,在我们的Gmail示例案例中将为:

root:machine-name@some-domain.com:smtp.gmail.com

root:machine-name@some-domain.com:smtp.gmail.com

Incredibly this is all you have to do to enable the ability. From now on, the machine will Email you when something is up.

令人难以置信的是,这是启用该功能所需要做的一切。 从现在开始,机器将在出现问题时通过电子邮件发送给您。

确认设置 (Confirming setup)

Lets test that our ssmtp setup was correct by sending an Email:

通过发送电子邮件来测试我们的ssmtp设置是否正确:


echo "Test message from Linux server using ssmtp" | sudo ssmtp -vvv your-email@some-domain.com


echo "Test message from Linux server using ssmtp" | sudo ssmtp -vvv your-email@some-domain.com

The “-vvv” turns on verbosity output so don’t get alarmed… this is just in case you encounter any problems, you will have some sort of output to Google for. If all goes well, you should be getting the Email in a couple of seconds.

“ -vvv”会打开详细信息输出,因此请不要担心……这是万一您遇到任何问题,您将获得某种输出给Google。 如果一切顺利,您应该在几秒钟内收到电子邮件。

We will show scripted examples of this setup in future articles.

我们将在以后的文章中显示此设置的脚本示例。



May your Emails be of the non-bouncing variety :)

希望您的电子邮件属于非退信性:)

翻译自: https://www.howtogeek.com/51819/how-to-setup-email-alerts-on-linux-using-gmail/

gmail 设置 smtp

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值