tianshijianbing 2016-03-07
假定你想配置一个 Linux 应用,用于从你的服务器或桌面客户端发送邮件信息。邮件信息可能是邮件简报、状态更新(如 Cachet)、监控警报(如 Monit)、磁盘时间(如 RAID mdadm)等等。当你要建立自己的 邮件发送服务器 传递信息时 ,你可以替代使用一个免费的公共 SMTP 服务器,从而避免遭受维护之苦。
谷歌的 Gmail 服务就是最可靠的 免费 SMTP 服务器 之一。想要从应用中发送邮件通知,你仅需在应用中添加 Gmail 的 SMTP 服务器地址和你的身份凭证即可。
使用 Gmail 的 SMTP 服务器会遇到一些限制,这些限制主要用于阻止那些经常滥用服务器来发送垃圾邮件和使用邮件营销的家伙。举个例子,你一次只能给至多 100 个地址发送信息,并且一天不能超过 500 个收件人。同样,如果你不想被标为垃圾邮件发送者,你就不能发送过多的不可投递的邮件。当你达到任何一个限制,你的 Gmail 账户将被暂时的锁定一天。简而言之,Gmail 的 SMTP 服务器对于你个人的使用是非常棒的,但不适合商业的批量邮件。
说了这么多,是时候向你们展示 如何在 Linux 环境下使用 Gmail 的 SMTP 服务器 了。
如果你想要通过你的应用使用 Gmail 的 SMTP 服务器发送邮件,请牢记接下来的详细说明。
确切的配置根据应用会有所不同。在本教程的剩余部分,我将向你展示一些在 Linux 上使用 Gmail SMTP 服务器的应用示例。
作为第一个例子,让我们尝试最基本的邮件功能:使用 Gmail SMTP 服务器从命令行发送一封邮件。为此,我将使用一个称为 mutt 的命令行邮件客户端。
先安装 mutt:
对于 Debian-based 系统:
<span class="pln">$ </span><span class="kwd">sudo</span><span class="kwd">apt-get</span><span class="pln"> install mutt</span>
对于 Red Hat based 系统:
<span class="pln">$ </span><span class="kwd">sudo</span><span class="kwd">yum</span><span class="pln"> install mutt</span>
创建一个 mutt 配置文件(~/.muttrc),并和下面一样,在文件中指定 Gmail SMTP 服务器信息。将 <gmail-id> 替换成自己的 Gmail ID。注意该配置只是为了发送邮件而已(而非接收邮件)。
<span class="pln">$ </span><span class="kwd">vi</span><span class="pun">~/.</span><span class="pln">muttrc</span>
<span class="kwd">set</span><span class="kwd">from</span><span class="pun">=</span><span class="str">"<gmail-id>@gmail.com"</span>
<span class="kwd">set</span><span class="pln"> realname </span><span class="pun">=</span><span class="str">"Dan Nanni"</span>
<span class="kwd">set</span><span class="pln"> smtp_url </span><span class="pun">=</span><span class="str">"smtp://<gmail-id>@smtp.gmail.com:587/"</span>
<span class="kwd">set</span><span class="pln"> smtp_pass </span><span class="pun">=</span><span class="str">"<gmail-password>"</span>
一切就绪,使用 mutt 发送一封邮件:
<span class="pln">$ </span><span class="kwd">echo</span><span class="str">"This is an email body."</span><span class="pun">|</span><span class="pln"> mutt </span><span class="pun">-</span><span class="pln">s </span><span class="str">"This is an email subject"</span><span class="pln"> alice@yahoo</span><span class="pun">.</span><span class="pln">com</span>
想在一封邮件中添加附件,使用 "-a" 选项
<span class="pln">$ </span><span class="kwd">echo</span><span class="str">"This is an email body."</span><span class="pun">|</span><span class="pln"> mutt </span><span class="pun">-</span><span class="pln">s </span><span class="str">"This is an email subject"</span><span class="pln"> alice@yahoo</span><span class="pun">.</span><span class="pln">com </span><span class="pun">-</span><span class="pln">a </span><span class="pun">~/</span><span class="pln">test_attachment</span><span class="pun">.</span><span class="pln">jpg</span>
使用 Gmail SMTP 服务器意味着邮件将显示是从你 Gmail 账户发出的。换句话说,收件人将视你的 Gmail 地址为发件人地址。如果你想要使用自己的域名作为邮件发送方,你需要使用 Gmail SMTP 转发服务。
如果你在 虚拟专用服务器(VPS) 上跑了些重要的网站,建议监控 VPS 的重启行为。作为一个更为实用的例子,让我们研究如何在你的 VPS 上为每一次重启事件建立邮件通知。这里假设你的 VPS 上使用的是 systemd,并向你展示如何为自动邮件通知创建一个自定义的 systemd 启动服务。
首先创建下面的脚本 reboot_notify.sh,用于负责邮件通知。
<span class="pln">$ </span><span class="kwd">sudo</span><span class="kwd">vi</span><span class="pun">/</span><span class="pln">usr</span><span class="pun">/</span><span class="kwd">local</span><span class="pun">/</span><span class="pln">bin</span><span class="pun">/</span><span class="pln">reboot_notify</span><span class="pun">.</span><span class="pln">sh</span>
<span class="com">#</span><span class="pun">!</span><span class="str">/bin/</span><span class="pln">sh</span>
<span class="kwd">echo</span><span class="str">"`hostname` was rebooted on `date`"</span><span class="pun">|</span><span class="pln"> mutt </span><span class="pun">-</span><span class="pln">F </span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">muttrc </span><span class="pun">-</span><span class="pln">s </span><span class="str">"Notification on `hostname`"</span><span class="pln"> alice@yahoo</span><span class="pun">.</span><span class="pln">com</span>
<span class="pln">$ </span><span class="kwd">sudo</span><span class="kwd">chmod</span><span class="pun">+</span><span class="pln">x </span><span class="pun">/</span><span class="pln">usr</span><span class="pun">/</span><span class="kwd">local</span><span class="pun">/</span><span class="pln">bin</span><span class="pun">/</span><span class="pln">reboot_notify</span><span class="pun">.</span><span class="pln">sh</span>
在这个脚本中,我使用 "-F" 选项,用于指定系统级的 mutt 配置文件位置。因此不要忘了创建 /etc/muttrc 文件,并如前面描述的那样填入 Gmail SMTP 信息。
现在让我们创建如下一个自定义的 systemd 服务。
<span class="pln">$ </span><span class="kwd">sudo</span><span class="kwd">mkdir</span><span class="pun">-</span><span class="pln">p </span><span class="pun">/</span><span class="pln">usr</span><span class="pun">/</span><span class="kwd">local</span><span class="pun">/</span><span class="pln">lib</span><span class="pun">/</span><span class="kwd">systemd</span><span class="pun">/</span><span class="pln">system</span>
<span class="pln">$ </span><span class="kwd">sudo</span><span class="kwd">vi</span><span class="pun">/</span><span class="pln">usr</span><span class="pun">/</span><span class="kwd">local</span><span class="pun">/</span><span class="pln">lib</span><span class="pun">/</span><span class="kwd">systemd</span><span class="pun">/</span><span class="pln">system</span><span class="pun">/</span><span class="kwd">reboot</span><span class="pun">-</span><span class="pln">task</span><span class="pun">.</span><span class="pln">service</span>
<span class="pun">[</span><span class="typ">Unit</span><span class="pun">]</span>
<span class="typ">Description</span><span class="pun">=</span><span class="typ">Send</span><span class="pln"> a notification email </span><span class="kwd">when</span><span class="pln"> the server gets rebooted</span>
<span class="typ">DefaultDependencies</span><span class="pun">=</span><span class="kwd">no</span>
<span class="typ">Before</span><span class="pun">=</span><span class="kwd">reboot</span><span class="pun">.</span><span class="pln">target</span>
<span class="pun">[</span><span class="typ">Service</span><span class="pun">]</span>
<span class="typ">Type</span><span class="pun">=</span><span class="pln">oneshot</span>
<span class="typ">ExecStart</span><span class="pun">=</span><span class="str">/usr/</span><span class="kwd">local</span><span class="pun">/</span><span class="pln">bin</span><span class="pun">/</span><span class="pln">reboot_notify</span><span class="pun">.</span><span class="pln">sh</span>
<span class="pun">[</span><span class="typ">Install</span><span class="pun">]</span>
<span class="typ">WantedBy</span><span class="pun">=</span><span class="kwd">reboot</span><span class="pun">.</span><span class="pln">target</span>
在创建服务后,添加并启动该服务。
<span class="pln">$ </span><span class="kwd">sudo</span><span class="kwd">systemctl</span><span class="pln"> enable </span><span class="kwd">reboot</span><span class="pun">-</span><span class="pln">task</span>
<span class="pln">$ </span><span class="kwd">sudo</span><span class="kwd">systemctl</span><span class="pln"> start </span><span class="kwd">reboot</span><span class="pun">-</span><span class="pln">task</span>
从现在起,在每次 VPS 重启时,你将会收到一封通知邮件。
作为最后一个例子,让我展示一个现实生活中的应用程序,Monit,这是一款极其有用的服务器监控应用程序。它带有全面的 VPS 监控能力(比如 CPU、内存、进程、文件系统)和邮件通知功能。
如果你想要接收 VPS 上由 Monit 产生的任何事件的邮件通知,你可以在 Monit 配置文件中添加以下 SMTP 信息。
<span class="kwd">set</span><span class="pln"> mailserver smtp</span><span class="pun">.</span><span class="pln">gmail</span><span class="pun">.</span><span class="pln">com port </span><span class="lit">587</span>
<span class="pln">username </span><span class="str">"<your-gmail-ID>"</span><span class="pln"> password </span><span class="str">"<gmail-password>"</span>
<span class="kwd">using</span><span class="pln"> tlsv12</span>
<span class="kwd">set</span><span class="pln"> mail</span><span class="pun">-</span><span class="pln">format </span><span class="pun">{</span>
<span class="kwd">from</span><span class="pun">:</span><span class="pun"><</span><span class="pln">your</span><span class="pun">-</span><span class="pln">gmail</span><span class="pun">-</span><span class="pln">ID</span><span class="pun">></span><span class="lit">@gmail</span><span class="pun">.</span><span class="pln">com</span>
<span class="pln">subject</span><span class="pun">:</span><span class="pln"> $SERVICE $EVENT at $DATE on $HOST</span>
<span class="pln">message</span><span class="pun">:</span><span class="typ">Monit</span><span class="pln"> $ACTION $SERVICE $EVENT at $DATE on $HOST </span><span class="pun">:</span><span class="pln"> $DESCRIPTION</span><span class="pun">.</span>
<span class="typ">Yours</span><span class="pln"> sincerely</span><span class="pun">,</span>
<span class="typ">Monit</span>
<span class="pun">}</span>
<span class="com">#</span><span class="pln"> the person </span><span class="kwd">who</span><span class="pln"> will receive notification emails</span>
<span class="kwd">set</span><span class="pln"> alert alice@yahoo</span><span class="pun">.</span><span class="pln">com</span>
这是一个因为 CPU 负载超载而由 Monit 发送的邮件通知的例子。
如你所见,类似 Gmail 这样免费的 SMTP 服务器有着这么多不同的运用方式 。但再次重申,请牢记免费的 SMTP 服务器不适用于商业用途,仅仅适用于个人项目。无论你正在哪款应用中使用 Gmail SMTP 服务器,欢迎自由分享你的用例。
via: http://xmodulo.com/send-email-notifications-gmail-smtp-server-linux.html
作者:Dan Nanni 译者:cposture 校对:martin2011qi, wxy