用于通过电子邮件发送输出或日志的Shell脚本

我想知道是否有可能通过使用谷歌(或任何其他SMTP服务器)通过电子邮件得到我的备份脚本输出或其任何错误。 我不想在本地机器上安装邮件服务器。 我应该使用什么脚本或工具来提供此function?

还有其他选项可以使用mutt通过外部SMTP服务器发送电子邮件。

指甲将做你想要的,只是没有更多。

Esmtp和sSMTP都用一个简单的邮件中继代替了sendmail(或者Postfix或者Exim或者其他),它将通过一个外部的SMTP服务器来中继所有的东西 。

安装适当的MTA有好处。 指甲,Mutt和sSMTP没有排队的概念。 如果SMTP服务器closures,他们都会向您发送一条错误消息,并忘记刚才给他们发送的电子邮件。

ESMTP 确实有一个队列,但它不是一个守护进程,所以它不主动pipe理队列。 当您尝试发送新电子邮件时,它会重试队列中的每封邮件。 这可能会导致我喜欢称之为“伦敦公共汽车综合症”:您等待一整天的电子邮件,然后一万来。

有很多使用电子邮件的好工具,我强烈build议您重新考虑本地邮件服务器的想法。 如果您担心安全问题,请限制服务器仅接受来自本地主机的消息。

现在,如果您同意,cron将会向您发送包含脚本输出的电子邮件。 如果您只想在出现问题时接收消息,请将退出代码添加到脚本中,并在crontab中检查它们: if /backup/script >/tmp/bkup.txt; then cat /tmp/bkup.txt; fi if /backup/script >/tmp/bkup.txt; then cat /tmp/bkup.txt; fi if /backup/script >/tmp/bkup.txt; then cat /tmp/bkup.txt; fi 。 或者,如果您想将输出发送到其他邮箱,请写下...then mail me@somewhere </tmp/bkup; fi ...then mail me@somewhere </tmp/bkup; fi

即使你仍然不喜欢这个想法,你可以使用ssmtp以类似的方式发送邮件:)

我发现另一个很好的工具MSMTP为我的要求使用外部smtp或gmail smtp发送邮件。 正如上面所提到的,当我configurationssmtp我得到所有的邮件,像我一样cron邮件&munin-cron和其他应用程序生成的邮件。

configuration步骤MSMTP MSMTP可以从命令行读取所需的每个参数。

 touch ~/.msmtprc chmod 0600 ~/.msmtprc vim ~/.msmtprc # Use an external SMTP server with insecure authentication. # (manually choose an insecure authentication method.) # Note that the password contains blanks. defaults ###################################################################### # A sample configuration using Gmail ###################################################################### # account name is "gmail". # You can select this account by using "-a gmail" in your command line. account gmail host smtp.gmail.com tls on tls_certcheck off port 587 auth login from [email protected] user somebody password somesecret ###################################################################### # A sample configuration using other normal ESMTP account ###################################################################### # account name is "someplace". # You can select this account by using "-a someplace" in your command line. account someplace host smtp.someplace.com from [email protected] auth login user someone password somesecret # If you don't use any "-a" parameter in your command line, # the default account "someplace" will be used. account default: someplace Test cat <<EOF | msmtp -a gmail [email protected] Subject: test This is a test! EOF