Linux 如何使用 bash 命令“sendmail”发送 html 电子邮件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1333097/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to send a html email with the bash command "sendmail"?
提问by omg
Anyone has a demo available?
任何人都有可用的演示?
Sendmail is said to be not scalable,but it's free,so I decided to use it first for now:)
Sendmail 据说不可扩展,但它是免费的,所以我决定暂时先用它:)
回答by DavidMWilliams
This page should help - http://www.zedwood.com/article/103/bash-send-mail-with-an-attachment
此页面应该有所帮助 - http://www.zedwood.com/article/103/bash-send-mail-with-an-attachment
It includes a script to send e-mail with a MIME attachment, ie with a HTML page and images included.
它包含一个脚本,用于发送带有 MIME 附件的电子邮件,即包含 HTML 页面和图像。
回答by Space
If I understand you correctly, you want to send mail in HTML format using linux sendmail command. This code is working on Unix. Please give it a try.
如果我理解正确,您想使用 linux sendmail 命令以 HTML 格式发送邮件。此代码适用于 Unix。请试一试。
echo "From: [email protected]
To: [email protected]
MIME-Version: 1.0
Content-Type: multipart/alternative;
boundary='PAA08673.1018277622/server.xyz.com'
Subject: Test HTML e-mail.
This is a MIME-encapsulated message
--PAA08673.1018277622/server.xyz.com
Content-Type: text/html
<html>
<head>
<title>HTML E-mail</title>
</head>
<body>
<a href='http://www.google.com'>Click Here</a>
</body>
</html>
--PAA08673.1018277622/server.xyz.com
" | sendmail -t
For the sendmail configuration details, please refer to this link. Hope this helps.
有关sendmail 配置的详细信息,请参阅此链接。希望这可以帮助。
回答by Steven L
回答by Nicholas Anderson
I understand you asked for sendmail but why not use the default mail? It can easily send html emails.
我知道您要求使用 sendmail 但为什么不使用默认邮件?它可以轻松发送 html 电子邮件。
Works on: RHEL 5.10/6.x & CentOS 5.8
适用于:RHEL 5.10/6.x 和 CentOS 5.8
Example:
例子:
cat ~/campaigns/release-status.html | mail -s "$(echo -e "Release Status [Green]\nContent-Type: text/html")" [email protected] -v
CodeShare: http://www.codeshare.io/8udx5
代码共享:http: //www.codeshare.io/8udx5
回答by Mike S
To follow up on the previous answer using mail:
要使用mail跟进上一个答案:
Often times one's html output is interpreted by the client mailer, which may not format things using a fixed-width font. Thus your nicely formatted ascii alignment gets all messed up. To send old-fashioned fixed-width the way the God intended, try this:
通常一个人的 html 输出是由客户端邮件程序解释的,它可能不会使用固定宽度的字体来格式化内容。因此,您格式良好的 ascii 对齐方式变得一团糟。要按照上帝的意图发送老式的固定宽度,请尝试以下操作:
{ echo -e "<pre>"
echo "Descriptive text here."
shell_command_1_here
another_shell_command
cat <<EOF
This is the ending text.
</pre><br>
</div>
EOF
} | mail -s "$(echo -e 'Your subject.\nContent-Type: text/html')" [email protected]
You don't necessarily need the "Descriptive text here." line, but I have found that sometimes the first line may, depending on its contents, cause the mail program to interpret the rest of the file in ways you did not intend. Try the script with simple descriptive text first, before fine tuning the output in the way that you want.
您不一定需要“此处的描述性文本”。行,但我发现有时第一行可能会导致邮件程序以您不希望的方式解释文件的其余部分,具体取决于其内容。首先尝试使用简单描述性文本的脚本,然后再以您想要的方式微调输出。
回答by PhilippeS
It's simpler to use, the -a option :
使用更简单, -a 选项:
cat ~/campaigns/release-status.html | mail -s "Release Status [Green]" -a "Content-Type: text/html" [email protected]
回答by Virole Bridée
-a option?
-一个选项?
Cf. man page:
参见 手册页:
-a file
Attach the given file to the message.
Result:
结果:
Content-Type: text/html: No such file or directory
回答by Abdel
Found solution in http://senthilkl.blogspot.lu/2012/11/how-to-send-html-emails-using-sendemail.html
在http://senthilkl.blogspot.lu/2012/11/how-to-send-html-emails-using-sendemail.html 中找到解决方案
sendEmail -f "oracle@server" -t "[email protected]" -u "Alert: Backup complete" -o message-content-type=html -o message-file=$LOG_FILE? -a $LOG_FILE_ATTACH