来自 Linux 上传家宝 mailx 的 HTML 电子邮件

FzZ*_*eaU 7 linux html mail-command mailx

我一直在尝试通过在 Linux 服务器上从 mailx 发送 html 电子邮件来工作。

几点注意事项:

  • 我必须指定一个 smtp 服务器,因此我不能使用 sendmail(这不是我可以改变的)
  • 我无法安装 3rd 方的东西,例如 mutt。我将不得不使用 mail 或 mailx
  • 由于我的邮件/x 版本是传家宝,我没有 --append 或 -a(附加标题选项)
  • 不确定这是否有帮助,但我的 linux 发行版是 7.3 (Maipo)

对于我的案例,我在关于 stackoverflow 的大多数帖子中看到的内容:

mailx -v -S smtp=SERVER -s "$(echo -e "This is the subject\nContent-Type: text/html")" -r FROM TO < htmlmail.txt
Run Code Online (Sandbox Code Playgroud)

在我的情况下,这只是返回一个纯文本电子邮件。

所以这是我迄今为止尝试过的:

尝试 1:

我在帖子中看到添加 Content-Disposition: inline。

mailx -v -S smtp=SERVER -s "$(echo -e "This is the subject v1\nContent-Type: text/html\nMIME-Version: 1.0\nContent-Disposition: inline")" -r FROM TO < htmlmail.txt
Run Code Online (Sandbox Code Playgroud)

这最终会发送一封 html 电子邮件,但由于标题包含在正文中,因此它输出如下:

内容处置:内联消息 ID:用户代理:Heirloom mailx 12.5 7/5/10 MIME-Version:1.0 Content-Type:text/plain;charset=us-ascii Content-Transfer-Encoding: 7bit Hello World

尝试2:

所以我不想在正文中打印标题。所以我试图删除 Content-Disposition: inline

mailx -v -S smtp=SERVER -s "$(echo -e "This is the subject v2\nContent-Type: text/html\nMIME-Version: 1.0")" -r FROM TO < htmlmail.txt
Run Code Online (Sandbox Code Playgroud)

这最终会发送一封简单的测试电子邮件,如下所示:

<html> <b>Hello World</b> </html>

尝试3:

尝试触发器内容类型和 MIME 版本

mailx -v -S SERVER -s "$(echo -e "This is the subject v3\nMIME-Version: 1.0\nContent-Type: text/html")" -r FROM TO < htmlmail.txt
Run Code Online (Sandbox Code Playgroud)

我最终没有收到来自此代码的电子邮件

尝试4:

我在网上看到尝试另一个标题来帮助找到问题所在。所以我添加了 header 选项来设置邮件优先级。

mailx -v -S smtp=SERVER -s "$(echo -e "This is a subject v4\nContent-Type: text/html\nX-Priority: 1 (Highest)")" -r FROM TO < htmlmail.txt
Run Code Online (Sandbox Code Playgroud)

这最终发送了一封高优先级的电子邮件,但都是纯文本的。

尝试5:

我将 MIME 标头添加到上一次尝试中

mailx -v -S smtp=SERVER -s "$(echo -e "This is a subject v5\nMIME-Version: 1.0\nContent-Type: text/html\nX-Priority: 1 (Highest)")" -r FROM TO < htmlmail.txt
Run Code Online (Sandbox Code Playgroud)

这最终发送了一封带有正文标题的电子邮件,并且优先级没有设置为高......很奇怪

X-Priority: 1 (Highest) Message-ID: User-Agent: Heirloom mailx 12.5 7/5/10 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Hello World

毕竟,我已经尝试了上述尝试的许多其他改编,但它们没有导致新的输出。

因此,我们很乐意接受任何建议或想法!请记住我上面列出的限制...我知道它们限制了我的选择,但这超出了我的控制范围。

谢谢你的时间!

小智 3

首先一些上下文:我使用 heirloom-mailx 版本作为以下线程上的版本: https ://serverfault.com/questions/136106/what-package-to-install-for-sending-emails-from-localhost-ubuntu

我使用的是 Ubuntu 16.04 Xenial。也在 Ubuntu Server 16.04 上尝试过。

为了发送电子邮件,我使用以下函数通过 mailx(bash 中的传家宝 mailx)发送邮件:

sendmail() {
#Sending Report Email
heirloom-mailx -a $2 -v -s ""$(echo -e "subject 3\nContent-Type: text/html")"" \
-S smtp-use-starttls \
-S ssl-verify=ignore \
-S smtp-auth=login \
-S smtp=smtp://mail.mymailserver:port \
-S from="example@mymailserver.com" \
-S smtp-auth-user=example@mymailserver.com \
-S smtp-auth-password='password' \
-S ssl-verify=ignore \
$1 < body.html
}
Run Code Online (Sandbox Code Playgroud)

其中 $2 是附件,$1 是目的地。注意: 1. 附加文件也会打印在正文内,但如果您只想发送不带附件的 html 文件,这可能对您有用。2. 使用“-v”选项会打印详细信息,因此您可能会遇到可以忽略的 .mime.types 问题。如果您不想在 mailx 上出现详细信息,请删除该选项。3. 如果使用“-a”选项,您仍会在正文中看到以下内容: 这是一条 MIME 格式的多部分消息。--=-=fFPa7dLqoSF1TGj-YDc2k8bdvmjpix_4sKFT=-= 内容类型:文本/纯文本;字符集=US-ASCII 内容处置:内联

在本例中,我附加了一个纯文本文件。从命令中删除“-a $2”,您就可以打印 html 消息了。所以最终结果将是:

sendmail() {
    #Sending Report Email
    heirloom-mailx -s ""$(echo -e "subject 3\nContent-Type: text/html")"" \
    -S smtp-use-starttls \
    -S ssl-verify=ignore \
    -S smtp-auth=login \
    -S smtp=smtp://mail.mymailserver:port \
    -S from="example@mymailserver.com" \
    -S smtp-auth-user=example@mymailserver.com \
    -S smtp-auth-password='password' \
    -S ssl-verify=ignore \
    $1 < body.html
    }
Run Code Online (Sandbox Code Playgroud)

尝试一下并告诉我。我已经在我的终端上进行了测试并且它有效。