use*_*716 21 html linux email mailx
我想用Mailx发送一条HTML消息.当我尝试以下命令时
mailx -s "Subject" user@gmail.com < email.html
Run Code Online (Sandbox Code Playgroud)
我以纯文本格式获取了email.html的内容.在消息中,标题Content-Type设置为text/plain.-a选项尝试发送文件,因此我没有找到如何修改标头.这个答案几乎可行,它将Content-Type设置为text/html,但不替换默认的Content-Type,即text/plain.
mailx -s "$(echo -e "This is the subject\nContent-Type: text/html")" user@gmail.com < email.html
Run Code Online (Sandbox Code Playgroud)
给出了这个结果:
From: send@gmail.com
To: user@gmail.com
Subject: This is the subject
Content-Type: text/html
Message-ID: <538d7b66.Xs0x9HsxnJKUFWuI%maikeul06@gmail.com>
User-Agent: Heirloom mailx 12.4 7/29/08
MIME-Version: 1.0
boundary="=_538d7b66.z5gaIQnlwb1f/AOkuuC+GwF1evCaG/XIHQMbMMxbY6satTjK"
This is a multi-part message in MIME format.
--=_538d7b66.z5gaIQnlwb1f/AOkuuC+GwF1evCaG/XIHQMbMMxbY6satTjK
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
<html>
<body>
<p>Helo wolrd</p>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
PS:我也试过用uuencode.当我尝试在网络邮件中显示消息时,我得到一个空白页面...
dog*_*ane 21
如果您的mailx命令支持-a(追加标题)选项,这很容易:
$ mailx -a 'Content-Type: text/html' -s "my subject" user@gmail.com < email.html
Run Code Online (Sandbox Code Playgroud)
如果没有,请尝试使用sendmail:
# create a header file
$ cat mailheader
To: user@gmail.com
Subject: my subject
Content-Type: text/html
# send
$ cat mailheader email.html | sendmail -t
Run Code Online (Sandbox Code Playgroud)
Wak*_*u44 11
有很多不同版本的邮件.当你超越mail -s的主题是1 @ address1到2 @ address2
对于一些mailx实现,例如来自Ubuntu或Debian的bsd-mailx上的mailutils,它很容易,因为有一个选项.
mailx -a 'Content-Type: text/html' -s "Subject" to@address <test.html
Run Code Online (Sandbox Code Playgroud)使用传家宝mailx,没有方便的方法.插入任意标头的一种可能性是设置editheaders = 1并使用外部编辑器(可以是脚本).
## Prepare a temporary script that will serve as an editor.
## This script will be passed to ed.
temp_script=$(mktemp)
cat <<'EOF' >>"$temp_script"
1a
Content-Type: text/html
.
$r test.html
w
q
EOF
## Call mailx, and tell it to invoke the editor script
EDITOR="ed -s $temp_script" heirloom-mailx -S editheaders=1 -s "Subject" to@address <<EOF
~e
.
EOF
rm -f "$temp_script"
Run Code Online (Sandbox Code Playgroud)使用一般的POSIX mailx,我不知道如何获取标题.
如果您打算使用任何邮件或mailx,请记住
即使在给定的Linux发行版中,这也是不可移植的.例如,Ubuntu和Debian都有几种邮件和邮件的替代品.
撰写邮件时,mail和mailx将以〜开头的行视为命令.如果将文本管道传入邮件,则需要安排此文本不包含以〜开头的行.
如果你打算安装软件,你也可以安装比mail/Mail/mailx更可预测的东西.例如,mutt.使用Mutt,您可以使用-H选项提供输入中的大多数标头,但不能通过mutt选项设置Content-Type.
mutt -e 'set content_type=text/html' -s 'hello' 'to@address' <test.html
Run Code Online (Sandbox Code Playgroud)
或者您可以直接调用sendmail.有几个版本的sendmail,但它们都支持sendmail -t以最简单的方式发送邮件,从邮件中读取收件人列表.(我认为他们并不都支持Bcc:.)在大多数系统上,sendmail不在通常的$ PATH中,它位于/ usr/sbin或/ usr/lib中.
cat <<'EOF' - test.html | /usr/sbin/sendmail -t
To: to@address
Subject: hello
Content-Type: text/html
EOF
Run Code Online (Sandbox Code Playgroud)
几年来,我已经在Arch Linux(该-a标志用于附件)上成功使用了以下命令:
mailx -s "The Subject $( echo -e "\nContent-Type: text/html" user@gmail.com < email.html
Run Code Online (Sandbox Code Playgroud)
这将Content-Type标头附加到主题标头,该标头在最近更新之前一直有效。现在,新行已从-s主题中过滤掉。据推测,这样做是为了提高安全性。
现在,我不再依赖于修改主题行,而是使用bash子外壳程序:
(
echo -e "Content-Type: text/html\n"
cat mail.html
) | mail -s "The Subject" -t user@gmail.com
Run Code Online (Sandbox Code Playgroud)
而且由于我们实际上只使用了mailx'subject'标志,所以似乎没有理由不sendmail按照@dogbane的建议切换到:
(
echo "To: user@gmail.com"
echo "Subject: The Subject"
echo "Content-Type: text/html"
echo
cat mail.html
) | sendmail -t
Run Code Online (Sandbox Code Playgroud)
bash子shell的使用避免了创建临时文件的麻烦。
| 归档时间: |
|
| 查看次数: |
88650 次 |
| 最近记录: |