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

问题描述 投票:56回答:11

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

目前已使用:

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

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

html linux email send
11个回答
49
投票

这对我有用:

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

1
投票

试试:

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

1
投票

我在我的一个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

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


42
投票

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

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

26
投票

创建一个名为tmp.html的文件,并在其中添加以下行:

<b>my bold message</b>

然后将所有这些粘贴到命令行中:(括号和全部)。

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

邮件将被发送。并且消息显示为粗体而不是<b>标签。

资源: How to send a html email with the bash command "sendmail"?


8
投票

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

尝试:

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

--append允许您向邮件添加任意标头,您应该在其中指定内容类型和内容处置。没有必要在你的文件中嵌入ToSubject头文件,或者用--append指定它们,因为你已经在命令行上隐式设置了它们(-s是主题,而[email protected]自动成为To)。


7
投票

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

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

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" [email protected] < /var/www/report.csv

2
投票

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


2
投票

很老的问题,但当我搜索有关此问题的问题时它排名很高。

在这里找到答案:

Sending HTML mail using a shell script


2
投票

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

在你的情况下将是:

mail -aContent-Type:text/html -s "Built notification" [email protected] < /var/www/report.csv
© www.soinside.com 2019 - 2024. All rights reserved.