脚本发送电子邮件

脚本发送电子邮件

本文介绍了从 bash 脚本发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Ubuntu 18.04 LTS、GNU Mailutils 3.4 和 MSMTP 1.6.6 从 Bash 脚本(和/或从命令行测试)发送包含附件的电子邮件.我在服务器运行 16.04 时使用 BSD-Mailx,但升级到 18.04 导致 Mailx 无法发送附件.

I am using Ubuntu 18.04 LTS, GNU Mailutils 3.4 and MSMTP 1.6.6 to send an e-mail, containing an attachment, from a Bash script (and/or testing from the command line). I was using BSD-Mailx when the server was running 16.04, but upgrading to 18.04 caused Mailx to not be able to send attachments.

我尝试了多种格式的 mail 命令以将文本传递到电子邮件正文,但它们似乎都失败了.一些例子:

I have tried multiple formats of the mail command in order to pass text to the body of the e-mail, yet they all seem to fail. Some examples:

echo "This is the body of the e-mail" | mail [email protected] -s "This is the subject" -A /file/path/file.txt

我得到的只是带有空电子邮件的附件.

All I get is the attached file with an empty e-mail.

mail [email protected] -s "This is the subject" -A /file/path/file.txt <<< echo "This is the body of the e-mail"

再次,带附件的空电子邮件.

Again, empty e-mail with the attachment.

我也尝试过使用命令末尾的电子邮件地址,它仍然只给出带有附件的空电子邮件.

I have also tried it with the e-mail address at the end of the command, which still just gives an empty e-mail with the attachment.

我已经尝试了上面的几个其他迭代,例如单个 < 重定向,| 命令末尾的文本,当然失败了,但只是想猜测正确的格式.

I have tried several other iterations of the above, such as a single < redirect, | the text at the end of the command, which of course fail, but just trying to guess at the correct format.

有没有其他人知道这个?

Does anyone else have this figured this out?

推荐答案

using mailutils

我认为问题在于,如果您指定 -A,则会忽略标准输入:https://savannah.gnu.org/bugs/?54992

您可以包含正文作为附加附件:

You can include the body text as an additional attachment:

echo "This is the body of the e-mail" |\
mail [email protected] \
    -s "This is the subject" \
    --skip-empty-attachments \
    --content-type text/plain -A - \
    -A /file/path/file.txt

使用mutt

虽然我不认为 mutt 真的是为脚本编写的,但看起来这应该可行:

using mutt

Although I don't think mutt is really intended for scripting, it looks like this should work:

echo "this is the body" |\
mutt \
  -s "this is the subject" \
  -a /file/path/file.txt -- \
  [email protected]

这篇关于从 bash 脚本发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 22:32