如何将 HTML 文件作为邮件内容(shell 脚本)

如何将 HTML 文件作为邮件内容(shell 脚本)

我是 shell 脚本的新手,我有一个包含表格数据的 HTML 文件。现在我需要将此文件发送到邮件。

mail -s "test" abc.com <test.html

目前我正在获取邮件中表格的原始 HTML 代码。

我需要在 Internet Explorer 中收到邮件中的表格内容。

答案1

尝试添加-a "Content-type: text/html;"如下内容:

mail -a "Content-type: text/html;" -s "test" abc.com <test.html

您可以通过运行以下命令轻松地测试这一点,该命令echo是简单的 HTML 命令mail

echo "<html><b>Test</b></html>" | mail -a "Content-type: text/html;" -s "test" abc.com

这个想法来自Unix & Linux Stack Exchange 上的此问答主题Stack Overflow 上也有类似的帖子

答案2

如果您需要将 html 作为附件发送,那么您可以使用名为 mpack 的 shell 工具。

http://linux.die.net/man/1/mpack

编辑示例:

我作为一个简单用户向 root 发送了一封附加了 /etc/hosts 的虚拟邮件:

$ mpack -s 'trx of /etc/hosts' /etc/hosts root@localhost

并且作为 root,我正在检查我的盒子:

# mail
Mail version 8.1.2 01/15/2001.  Type ? for help.
"/var/mail/root": 1 message 1 new
& p
Message 1:
From sgombai@localhost  Thu Feb 11 01:54:27 2016
Date: Thu, 11 Feb 2016 01:54:27 +0100
From: sgombai <sgombai@localhost>
Mime-Version: 1.0
To: root@localhost
Subject: trx of /etc/hosts
Content-Type: multipart/mixed; boundary="-"

This is a MIME encoded message.  Decode it with "munpack"
or any other MIME reading software.  Mpack/munpack is available
via anonymous FTP in ftp.andrew.cmu.edu:pub/mpack/
---
Content-Type: application/octet-stream; name="hosts"
Content-Transfer-Encoding: base64
Content-Disposition: inline; filename="hosts"
Content-MD5: 9WRRh8Yr7YPb7zo1AsgwcA==

MTI3LjAuMC4xCWxvY2FsaG9zdAo5LjE1Ny4yMTQuMTc4CXZhY21mcy52YWMuaHUuaWJtLmNv
bQl2YWNtZnMKCiMgVGhlIGZvbGxvd2luZyBsaW5lcyBhcmUgZGVzaXJhYmxlIGZvciBJUHY2
IGNhcGFibGUgaG9zdHMKOjoxICAgICBsb2NhbGhvc3QgaXA2LWxvY2FsaG9zdCBpcDYtbG9v
cGJhY2sKZmUwMDo6MCBpcDYtbG9jYWxuZXQKZmYwMDo6MCBpcDYtbWNhc3RwcmVmaXgKZmYw
Mjo6MSBpcDYtYWxsbm9kZXMKZmYwMjo6MiBpcDYtYWxscm91dGVycwpmZjAyOjozIGlwNi1h
bGxob3N0cwo=

-----

因此,它带有附件,可以通过任何现代邮件程序保存/解码。

相关内容