如何使用shell脚本发送HTML电子邮件?
发布于 2010-07-23 18:27:21
首先,您需要编写消息。最小值由以下两个标头组成:
MIME-Version: 1.0
Content-Type: text/html..。和适当的消息正文:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head><title></title>
</head>
<body>
<p>Hello, world!</p>
</body>
</html>一旦您有了它,您就可以将适当的信息传递给mail命令:
body = '...'
echo $body | mail \
-a "From: me@example.com" \
-a "MIME-Version: 1.0" \
-a "Content-Type: text/html" \
-s "This is the subject" \
you@example.com这是一个过于简单的示例,因为您还需要注意字符集、编码、最大行长度……但这基本上就是我们的想法。
或者,您可以用Perl或PHP编写脚本,而不是用普通的shell。
更新
shell脚本基本上是一个具有Unix行结尾的文本文件,它以名为shebang的行开头,该行告诉shell它必须将文件传递给哪个解释器,遵循解释器能够理解并具有执行权限的语言中的一些命令(在Unix中,这是一个文件属性)。例如,假设您将以下代码保存为hello-world
#!/bin/sh
echo Hello, world!然后分配执行权限:
chmod +x hello-world你终于可以运行它了:
./hello-world无论如何,这与原来的问题没有什么关系。在使用它执行高级任务之前,您应该熟悉基本的shell脚本。这里有几个关于bash的链接,这是一个流行的shell:
http://www.gnu.org/software/bash/manual/html_node/index.html
http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html
发布于 2010-07-26 21:47:13
标签包括'sendmail‘,所以这里有一个使用它的解决方案:
(
echo "From: me@xyz.com "
echo "To: them@xyz.com "
echo "MIME-Version: 1.0"
echo "Content-Type: multipart/alternative; "
echo ' boundary="some.unique.value.ABC123/server.xyz.com"'
echo "Subject: Test HTML e-mail."
echo ""
echo "This is a MIME-encapsulated message"
echo ""
echo "--some.unique.value.ABC123/server.xyz.com"
echo "Content-Type: text/html"
echo ""
echo "<html>
<head>
<title>HTML E-mail</title>
</head>
<body>
<a href='http://www.google.com'>Click Here</a>
</body>
</html>"
echo "------some.unique.value.ABC123/server.xyz.com--"
) | sendmail -tsendmail的包装器可以使这项工作更容易,例如mutt
mutt -e 'set content_type="text/html"' me@mydomain.com -s "subject" < message.html发布于 2012-09-07 12:04:26
到目前为止,我已经在cmd linux中找到了两种快速方法。
mail -s "$(echo -e "This is Subject\nContent-Type: text/html")" test@yahoo.com < mytest.html
mutt -e "my_hdr Content-Type: text/html" test@yahoo.com -s "subject" < mytest.html
https://stackoverflow.com/questions/3317174
复制相似问题