如何使用linux命令行发送HTML电子邮件
我需要发送html格式的电子邮件。我只有linux命令行和命令“mail” 目前已使用:如何使用linux命令行发送HTML电子邮件,html,linux,email,send,Html,Linux,Email,Send,我需要发送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 "<htm
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
echo”发送至:address@example.com“>/var/www/report.csv
echo“主题:主题”>>/var/www/report.csv
echo“内容类型:text/html;字符集=\“us ascii\”>/var/www/report.csv
echo”“>>/var/www/report.csv
mysql-u*****-p*****-H-e“从用户限制中选择*20”dev>>/var/www/report.csv
echo”“>>/var/www/report.csv
邮件-s“内置通知”address@example.com
但在我的邮件代理中,我只能得到纯文本
您应该使用“附加”模式重定向>
而不是
尝试:
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
echo”发送至:address@example.com“>/var/www/report.csv
echo“主题:主题”>>/var/www/report.csv
echo“MIME版本:1.0”>>/var/www/report.csv
echo“内容类型:text/html;字符集=\“us ascii\”>/var/www/report.csv
echo“内容处置:内联”>>/var/www/report.csv
echo”“>>/var/www/report.csv
mysql-u*****-p*****-H-e“从用户限制中选择*20”dev>>/var/www/report.csv
echo”“>>/var/www/report.csv
邮件-s“内置通知”address@example.com
问题在于,当将文件重定向到类似的“邮件”中时,它仅用于邮件正文。您在文件中嵌入的任何头都将进入正文
尝试:
mail--append=“内容类型:text/html”-s“内置通知”address@example.com
--append允许您向邮件添加任意标题,您应该在其中指定内容类型和内容处置。无需在文件中嵌入到
和主题
头,也无需使用--append指定它们,因为您已经在命令行上隐式设置了它们(-s是主题,而address@example.com自动成为到)。这对我很有用:
echo "<b>HTML Message goes here</b>" | mail -s "$(echo -e "This is the subject\nContent-Type: text/html")" foo@example.com
echo“HTML消息在此显示”| mail-s“$(echo-e“这是主题内容类型:text/HTML”)”foo@example.com
这是一个很老的问题,但当我在谷歌上搜索一个关于这个问题的问题时,它的排名很高
在这里找到答案:
我的邮件版本没有--append
,而且对于echo-e\n
-技巧来说太聪明了(它只是用空格代替了\n)。但是,它确实有-a
:
mail -a "Content-type: text/html" -s "Built notification" address@example.com < /var/www/report.html
mail-a“内容类型:text/html”-s“内置通知”address@example.com
命令行
创建一个名为tmp.html
的文件,其中包含以下内容:
我的粗体消息
接下来,将以下内容粘贴到命令行(括号和全部):
(
回音至:youremail@blah.com
回音来自:el@defiant.com
echo“内容类型:文本/html
echo主题:日志文件
回响
cat tmp.html
)| sendmail-t
由于
元素,邮件将被发送,包括一条粗体消息
Shell脚本
作为脚本,将以下内容另存为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
运行email.sh
。当电子邮件到达时,HTML内容
文本将显示为红色
相关的
在OS X(10.9.4)上,cat
工作正常,如果您的电子邮件已经存在于以下文件中,则更容易:
cat email_template.html | mail -s "$(echo -e "Test\nContent-Type: text/html")" karl@marx.com
使用Hiherloom mailx,您可以将sendmail程序更改为钩子脚本,替换其中的标题,然后使用sendmail
我使用的脚本(~/bin/sendmail hook
):
此脚本更改邮件标题中的值,如下所示:
内容类型:
至文本/html;字符集=utf-8
内容传输编码:
到8bit
(不确定是否真的需要)
要发送HTML电子邮件:
mail -Ssendmail='~/bin/sendmail-hook' \
-s "Built notification" address@example.com < /var/www/report.csv
mail-Ssendmail='~/bin/sendmail-hook'\
-s“内置通知”address@example.com
我在git的一个post_receive钩子中遇到了类似的问题(邮件),最后我发现sendmail实际上在这类事情上效果更好,特别是如果你知道一点电子邮件是如何构造的(看起来你知道)。我知道这个答案来得很晚,但也许它对其他人也有一些用处。我使用了herdoc操作符和特性,它扩展了变量,所以它也可以运行内联脚本。只需查看以下内容(bash脚本):
#/bin/bash
接受者=(
'john@example.com'
'marry@not-所以一个.example.com'
# 'naah@not.this.one'
);
发送器是高度自动化的-reporter@example.com';
“哦,谁真的在乎呢,说真的……”;
sendmail-t我找到了一个非常简单的解决方案:在mail命令中添加修饰符-a内容类型:text/html
在您的情况下:
mail-a内容类型:text/html-s“内置通知”address@example.com
在我的邮件命令邮件上没有该选项:无效选项--用法:邮件[-iInv][s主题][c抄送地址][b密件抄送地址]到地址。。。[--sendmail选项…]mail[-iInNv]-f[name]mail[-iInNv][-u user]@nylund:mail(GNU Mailutils 2.2)
。这是Ubuntu12.04的版本,但也将是旧版本,因为这个答案现在已经有3年了。这不起作用。$lsb_发行版-发行商ID:Ubuntu描述:Ubuntu 10.04.4 LTS发行版:10.04代码名:lucid@Simone,由于MIME版本和内容类型的顺序,此操作无法完美运行。在大多数unix/linux版本中,您需要将此信息放在主题之前。我认为这应该是一个注释,而不是一个答案。我认为这是OSX邮件实现中的一个bug,尽管它需要与其他bug结合才能被利用。我敢肯定,在AIX7.1.0.0上,会有一些web脚本在插入主题行时没有正确检查回车!哦,对智者说几句话
#!/bin/bash
sed '1,/^$/{
s,^\(Content-Type: \).*$,\1text/html; charset=utf-8,g
s,^\(Content-Transfer-Encoding: \).*$,\18bit,g
}' | sendmail $@
mail -Ssendmail='~/bin/sendmail-hook' \
-s "Built notification" address@example.com < /var/www/report.csv
#!/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 -aContent-Type:text/html -s "Built notification" address@example.com < /var/www/report.csv