68

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

目前使用过:

echo "To: address@example.com" > /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" address@example.com < /var/www/report.csv

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

替代文字

4

11 回答 11

64

这对我有用:

echo "<b>HTML Message goes here</b>" | mail -s "$(echo -e "This is the subject\nContent-Type: text/html")" foo@example.com
于 2011-04-28T16:51:21.760 回答
46

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

mail -a "Content-type: text/html" -s "Built notification" address@example.com < /var/www/report.html
于 2012-09-04T11:18:06.040 回答
29

命令行

创建一个包含tmp.html以下内容的文件:

<b>my bold message</b>

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

(
  echo To: youremail@blah.com
  echo From: el@defiant.com
  echo "Content-Type: text/html; "
  echo Subject: a logfile
  echo
  cat tmp.html
) | sendmail -t

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

外壳脚本

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

ARG_EMAIL_TO="recipient@domain.com"
ARG_EMAIL_FROM="Your Name <you@host.com>"
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

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

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

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

有关的

于 2012-12-13T04:37:45.160 回答
10

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

cat email_template.html  | mail -s "$(echo -e "Test\nContent-Type: text/html")" karl@marx.com
于 2014-08-19T19:21:19.000 回答
6

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

尝试:

mail --append="Content-type: text/html" -s "Built notification" address@example.com < /var/www/report.csv

--append 允许您向邮件添加任意标题,您应该在其中指定内容类型和内容处置。无需在文件中嵌入ToSubject标头,或使用 --append 指定它们,因为您已经在命令行上隐式设置它们(-s 是主题,而 address@example.com 自动成为To)。

于 2010-04-07T22:17:47.043 回答
3

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

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

#!/bin/bash

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

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

  • Content-Type:text/html; charset=utf-8
  • Content-Transfer-Encoding:8bit(不确定这是否真的需要)。

要发送 HTML 电子邮件:

mail -Ssendmail='~/bin/sendmail-hook' \
    -s "Built notification" address@example.com < /var/www/report.csv
于 2014-09-17T07:01:45.163 回答
2

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

在这里找到答案:

使用 shell 脚本发送 HTML 邮件

于 2012-03-27T07:57:47.043 回答
2

我找到了一个非常简单的解决方案:在邮件命令中添加修饰符 -aContent-Type:text/html。

你的情况是:

mail -aContent-Type:text/html -s "Built notification" address@example.com < /var/www/report.csv
于 2017-11-17T13:11:06.343 回答
2

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

#!/bin/bash
recipients=(
    'john@example.com'
    'marry@not-so-an.example.com'
#   'naah@not.this.one'
);
sender='highly-automated-reporter@example.com';
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

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

于 2017-02-16T05:51:55.940 回答
1

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

于 2010-04-07T10:56:09.913 回答
1

尝试:

echo "To: address@example.com" > /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" address@example.com < /var/www/report.csv
于 2010-04-07T10:59:01.623 回答