bash 使用 sendmail 强制内容类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18233696/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 06:15:03  来源:igfitidea点击:

Force Content Type with sendmail

bashgmailsendmailcontent-type

提问by Labynocle

I got a little problem when sending mail with sendmail: each mail are sent with Content-Type: multipart/alternativebut I wonder to send my mail only in Content-Type: text/plain. The reason is because of GMAIL web interface respect the RFC, my message is displayed with the latest Content-typebut because my message is not in HTML, the display is awful.

从sendmail发送邮件时我有一个小问题:每封邮件与发送Content-Type: multipart/alternative,但我不知道送我的邮箱只是在Content-Type: text/plain。原因是因为 GMAIL 网络界面尊重 RFC,我的消息显示为最新Content-type但因为我的消息不是 HTML,所以显示很糟糕。

My bash script is as following:

我的 bash 脚本如下:

#!/bin/bash

SENDMAIL_BIN='/usr/sbin/sendmail'
FROM_MAIL_ADDRESS='[email protected]'
FROM_MAIL_DISLAY='Test format mail'
RECIPIENT_ADDRESSES='[email protected]'

MAIL_CMD="$SENDMAIL_BIN -f $FROM_MAIL_ADDRESS -F \"$FROM_MAIL_DISLAY\" $RECIPIENT_ADDRESSES"
(echo "Subject: Test format";echo -e "MIME-Version: 1.0\nContent-Type: text/plain;\n\n" && cat output.txt) | eval $MAIL_CMD

But my script doesn't seem to rewrite the Content-Typeand it's still Content-type: multipart/alternative(according to the show originalof my mail).

但是我的脚本似乎没有重写Content-Type,它仍然是Content-type: multipart/alternative(根据我邮件的显示原件)。

nota:

注意

  • There is nothing special in my output.txt (only log lines from my app).
  • I tried a gruik hack: put a <pre>and </pre>but the display is still awful with &lt;pre&gt;in the source of the mail in the Content-Type: text/htmlpart...
  • 我的 output.txt 没有什么特别之处(只有我的应用程序中的日志行)。
  • 我尝试了一个 gruik hack:放一个<pre>and</pre>&lt;pre&gt;Content-Type: text/html部分邮件的来源中显示仍然很糟糕......

If you have any clue or if you know how to change the order of the Content-Typewith sendmail let me know.

如果您有任何线索,或者您知道如何更改Content-Typewith sendmail的顺序,请告诉我。

Thanks in advance

提前致谢

回答by Labynocle

I have a non solution, instead of to force my mail to be in text/plain, I will send a mail in text/htmlbut I will add the <pre>tag to open and close my output file... And because it's now in text/html, the <pre>tag is not displayed as &lt;pre&gt;

我有一个非解决方案,而不是强制我的邮件进入text/plain,我将发送一封邮件,text/html但我将添加<pre>标签以打开和关闭我的输出文件......而且因为它现在在text/html<pre>标签不会显示为&lt;pre&gt;

It's not what I excepted but it works. So my previous script simply become:

这不是我所排除的,但它有效。所以我之前的脚本简单地变成了:

#!/bin/bash

SENDMAIL_BIN='/usr/sbin/sendmail'
FROM_MAIL_ADDRESS='[email protected]'
FROM_MAIL_DISLAY='Test format mail'
RECIPIENT_ADDRESSES='[email protected]'

MAIL_CMD="$SENDMAIL_BIN -f $FROM_MAIL_ADDRESS -F \"$FROM_MAIL_DISLAY\" $RECIPIENT_ADDRESSES"
(echo "Subject: Test format";echo -e "MIME-Version: 1.0\nContent-Type: text/html;\n" && echo '<pre>' && cat output.txt && echo '</pre>') | eval $MAIL_CMD

回答by konsolebox

Try removing the extra semicolon on the content type:

尝试删除内容类型上的额外分号:

echo -e "MIME-Version: 1.0\nContent-Type: text/plain\n\n"

echo -e "MIME-Version: 1.0\nContent-Type: text/plain\n\n"

Also it's better to use arrays than parse a string with eval:

此外,使用数组比使用 eval 解析字符串更好:

#!/bin/bash

SENDMAIL_BIN='/usr/sbin/sendmail'
FROM_MAIL_ADDRESS='[email protected]'
FROM_MAIL_DISLAY='Test format mail'
RECIPIENT_ADDRESSES='[email protected]'

MAIL_CMD=("$SENDMAIL_BIN" -f "$FROM_MAIL_ADDRESS" -F "$FROM_MAIL_DISLAY" "$RECIPIENT_ADDRESSES")
(echo "Subject: Test format";echo -e "MIME-Version: 1.0\nContent-Type: text/plain\n\n" && cat output.txt) | "${MAIL_CMD[@]}"