如何使用 Linux 命令行发送 HTML 电子邮件

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

How to send HTML email using linux command line

htmllinuxemailsend

提问by Oleh Herych

I need to send email with html format. I have only linux command line and command "mail".

我需要发送 html 格式的电子邮件。我只有 linux 命令行和命令“mail”。

Currently have used:

目前使用过:

echo "To: [email protected]" > /var/www/report.csv
echo "Subject: Subject" >> /var/www/report.csv
echo "Content-Type: text/html; charset=\"us-ascii\"" >> /var/www/report.csv

echo "<html>" >> /var/www/report.csv
mysql -u ***** -p***** -H -e "select * from users LIMIT 20" dev >> /var/www/report.csv
echo "</html>" >> /var/www/report.csv

mail -s "Built notification" [email protected] < /var/www/report.csv

But in my mail-agent i get only plain/text.

但是在我的邮件代理中,我只得到纯文本/文本。

alt text

替代文字

回答by ghostdog74

you should use "append" mode redirection >>instead of >

您应该使用“附加”模式重定向>>而不是>

回答by Simone Margaritelli

Try with :

尝试:

echo "To: [email protected]" > /var/www/report.csv
echo "Subject: Subject" >> /var/www/report.csv
echo "MIME-Version: 1.0" >> /var/www/report.csv
echo "Content-Type: text/html; charset=\"us-ascii\"" >> /var/www/report.csv
echo "Content-Disposition: inline" >> /var/www/report.csv

echo "<html>" >> /var/www/report.csv
mysql -u ***** -p***** -H -e "select * from users LIMIT 20" dev >> /var/www/report.csv
echo "</html>" >> /var/www/report.csv

mail -s "Built notification" [email protected] < /var/www/report.csv

回答by Marc B

The problem is that when redirecting a file into 'mail' like that, it's used for the message body only. Any headers you embed in the file will go into the body instead.

问题是,当像这样将文件重定向到“邮件”时,它仅用于邮件正文。您嵌入文件中的任何标题都将进入正文。

Try:

尝试:

mail --append="Content-type: text/html" -s "Built notification" [email protected] < /var/www/report.csv

--append lets you add arbitrary headers to the mail, which is where you should specify the content-type and content-disposition. There's no need to embed the Toand Subjectheaders in your file, or specify them with --append, since you're implicitly setting them on the command line already (-s is the subject, and [email protected] automatically becomes the To).

--append 允许您向邮件添加任意标题,您应该在此处指定内容类型和内容处置。无需在文件中嵌入ToSubject标头,或使用 --append 指定它们,因为您已经在命令行中隐式设置了它们(-s 是主题,[email protected] 自动变为To)。

回答by Dude

This worked for me:

这对我有用:

echo "<b>HTML Message goes here</b>" | mail -s "$(echo -e "This is the subject\nContent-Type: text/html")" [email protected]

回答by Magge

Very old question, however it ranked high when I googled a question about this.

很老的问题,但是当我用谷歌搜索一个关于这个的问题时,它排名很高。

Find the answer here:

在这里找到答案:

Sending HTML mail using a shell script

使用 shell 脚本发送 HTML 邮件

回答by Ole Tange

My version of mail does not have --appendand it too smart for the echo -e \n-trick (it simply replaces \n with space). It does, however, have -a:

我的邮件版本没有,--append而且它对于echo -e \n-trick来说太聪明了(它只是用空格替换了 \n)。然而,它确实有-a

mail -a "Content-type: text/html" -s "Built notification" [email protected] < /var/www/report.html

回答by Eric Leschinski

Command Line

命令行

Create a file named tmp.htmlwith the following contents:

创建一个以tmp.html以下内容命名的文件:

<b>my bold message</b>

Next, paste the following into the command line (parentheses and all):

接下来,将以下内容粘贴到命令行(括号和全部)中:

(
  echo To: [email protected]
  echo From: [email protected]
  echo "Content-Type: text/html; "
  echo Subject: a logfile
  echo
  cat tmp.html
) | sendmail -t

The mail will be dispatched including a bold message due to the <b>element.

由于该<b>元素,将发送包含粗体消息的邮件。

Shell Script

外壳脚本

As a script, save the following as email.sh:

作为脚本,将以下内容另存为email.sh

ARG_EMAIL_TO="[email protected]"
ARG_EMAIL_FROM="Your Name <[email protected]>"
ARG_EMAIL_SUBJECT="Subject Line"

(
  echo "To: ${ARG_EMAIL_TO}"
  echo "From: ${ARG_EMAIL_FROM}"
  echo "Subject: ${ARG_EMAIL_SUBJECT}"
  echo "Mime-Version: 1.0"
  echo "Content-Type: text/html; charset='utf-8'"
  echo
  cat contents.html
) | sendmail -t

Create a file named contents.htmlin the same directory as the email.shscript that resembles:

contents.html在与email.sh脚本相同的目录中创建一个文件,类似于:

<html><head><title>Subject Line</title></head>
<body>
  <p style='color:red'>HTML Content</p>
</body>
</html>

Run email.sh. When the email arrives, the HTML Contenttext will appear red.

运行email.sh。当电子邮件到达时,HTML Content文本将显示为红色。

Related

有关的

回答by jamesnotjim

On OS X (10.9.4), catworks, and is easier if your email is already in a file:

在 OS X (10.9.4) 上,cat如果您的电子邮件已经在一个文件中,则可以工作并且更容易:

cat email_template.html  | mail -s "$(echo -e "Test\nContent-Type: text/html")" [email protected]

回答by loentar

With heirloom-mailx you can change sendmail program to your hook script, replace headers there and then use sendmail.

使用 heirloom-mailx,您可以将 sendmail 程序更改为您的钩子脚本,替换那里的标头,然后使用 sendmail。

The script I use (~/bin/sendmail-hook):

我使用的脚本 ( ~/bin/sendmail-hook):

#!/bin/bash

sed '1,/^$/{
s,^\(Content-Type: \).*$,text/html; charset=utf-8,g
s,^\(Content-Transfer-Encoding: \).*$,bit,g
}' | sendmail $@

This script changes the values in the mail header as follows:

此脚本更改邮件标头中的值,如下所示:

  • Content-Type:to text/html; charset=utf-8
  • Content-Transfer-Encoding:to 8bit(not sure if this is really needed).
  • Content-Type:text/html; charset=utf-8
  • Content-Transfer-Encoding:8bit(不确定这是否真的需要)。

To send HTML email:

发送 HTML 电子邮件:

mail -Ssendmail='~/bin/sendmail-hook' \
    -s "Built notification" [email protected] < /var/www/report.csv

回答by Cromax

I was struggling with similar problem (with mail) in one of my git's post_receive hooks and finally I found out, that sendmail actually works better for that kind of things, especially if you know a bit of how e-mails are constructed (and it seems like you know). I know this answer comes very late, but maybe it will be of some use to others too. I made use of heredoc operator and use of the feature, that it expands variables, so it can also run inlined scripts. Just check this out (bash script):

我在我的一个 git 的 post_receive 钩子中遇到了类似的问题(邮件),最后我发现,sendmail 实际上更适合这类事情,特别是如果你知道一些电子邮件是如何构建的(并且它好像你知道)。我知道这个答案来得很晚,但也许对其他人也有用。我利用了heredoc 操作符并使用了它扩展变量的特性,因此它也可以运行内联脚本。只需检查一下(bash脚本):

#!/bin/bash
recipients=(
    '[email protected]'
    '[email protected]'
#   '[email protected]'
);
sender='[email protected]';
subject='Oh, who really cares, seriously...';
sendmail -t <<-MAIL
    From: ${sender}
    `for r in "${recipients[@]}"; do echo "To: ${r}"; done;`
    Subject: ${subject}
    Content-Type: text/html; charset=UTF-8

    <html><head><meta charset="UTF-8"/></head>
    <body><p>Ladies and gents, here comes the report!</p>
    <pre>`mysql -u ***** -p***** -H -e "SELECT * FROM users LIMIT 20"`</pre>
    </body></html>
MAIL

Note of backticks in the MAIL part to generate some output and remember, that <<-operator strips only tabs (not spaces) from the beginning of lines, so in that case copy-paste will not work (you need to replace indentation with proper tabs). Or use <<operator and make no indentation at all. Hope this will help someone. Of course you can use backticks outside o MAIL part and save the output into some variable, that you can later use in the MAIL part — matter of taste and readability. And I know, #!/bin/bashdoes not always work on every system.

注意 MAIL 部分中的反引号以生成一些输出并记住,该<<-运算符仅从行的开头去除制表符(而不是空格),因此在这种情况下,复制粘贴将不起作用(您需要用适当的制表符替换缩进)。或者使用<<运算符并且根本不进行缩进。希望这会帮助某人。当然,您可以在 o MAIL 部分之外使用反引号并将输出保存到某个变量中,稍后您可以在 MAIL 部分中使用它 - 品味和可读性的问题。而且我知道,#!/bin/bash并不总是适用于每个系统。