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: 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
But in my mail-agent i get only plain/text.
但是在我的邮件代理中,我只能收到纯文本。
11 个解决方案
#1
45
This worked for me:
这工作对我来说:
echo "<b>HTML Message goes here</b>" | mail -s "$(echo -e "This is the subject\nContent-Type: text/html")" foo@example.com
#2
36
My version of mail does not have --append
and it too smart for the echo -e \n
-trick (it simply replaces \n with space). It does, however, have -a
:
我的邮件版本没有——附加,它对于echo -e \n-trick来说太智能了(它只是用空格替换\n)。但是,它有-a:
mail -a "Content-type: text/html" -s "Built notification" address@example.com < /var/www/report.html
#3
19
Make a file called tmp.html and put the following line in it:
创建一个名为tmp的文件。html并在其中加入如下一行:
<b>my bold message</b>
Then paste all this into the commandline: (with the parenthesis and all).
然后将所有这些粘贴到命令行中:(用括号和所有)。
(
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
The mail will be dispatched. And the message appeared as bold instead of with the <b>
tags.
邮件将被发出。消息显示为粗体,而不是标记。
Source:
How to send a html email with the bash command "sendmail"?
源:如何发送html电子邮件与bash命令“sendmail”?
#4
6
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.
问题是,当将文件重定向到类似的“mail”时,它只用于消息主体。文件中嵌入的任何标头都将进入正文。
Try:
试一试:
mail --append="Content-type: text/html" -s "Built notification" address@example.com < /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 To
and Subject
headers in your file, or specify them with --append, since you're implicitly setting them on the command line already (-s is the subject, and address@example.com automatically becomes the To
).
-append允许向邮件添加任意的头,在这里应该指定内容类型和内容配置。无需在文件中嵌入to和Subject头,也无需使用-append指定它们,因为您已经在命令行中隐式地设置了它们(-s是主题,address@example.com自动成为to)。
#5
4
On OS X (10.9.4), cat
works, 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")" karl@marx.com
#6
2
you should use "append" mode redirection >>
instead of >
您应该使用“append”模式重定向>>而不是>。
#7
2
Try with :
试一试:
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
#8
2
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邮件
#9
2
With heirloom-mailx you can change sendmail program to your hook script, replace headers there and then use sendmail.
使用heirloom-mailx,您可以将sendmail程序更改为hook脚本,在那里替换header,然后使用sendmail。
The script I use (~/bin/sendmail-hook
):
我使用的脚本(~/bin/sendmail-hook):
#!/bin/bash
sed '1,/^$/{
s,^\(Content-Type: \).*$,\1text/html; charset=utf-8,g
s,^\(Content-Transfer-Encoding: \).*$,\18bit,g
}' | sendmail $@
This script changes the values in the mail header as follows:
此脚本更改邮件头中的值如下:
-
Content-Type:
totext/html; charset=utf-8
- 内容类型:text / html;utf - 8字符集=
-
Content-Transfer-Encoding:
to8bit
(not sure if this is really needed). - 内容传递-编码:到8bit(不确定是否真的需要)。
To send HTML email:
发送HTML电子邮件:
mail -Ssendmail='~/bin/sendmail-hook' \
-s "Built notification" address@example.com < /var/www/report.csv
#10
0
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=(
'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
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/bash
does not always work on every system.
要生成一些输出,请注意邮件部分的反勾号,请记住,<<- operator只从行开头删除制表符(而不是空格),因此在这种情况下,复制粘贴将不起作用(需要用适当的制表符替换缩进)。或使用< <运算符,并没有缩进。希望这能帮助某人。当然,您可以在o邮件部分之外使用回签,并将输出保存到某个变量中,稍后您可以在邮件部分中使用这个变量—品味和可读性问题。我知道,# ! bin bash并不总是适用于每个系统。< p>
#11
0
I found a really easy solution: add to the mail command the modifier -aContent-Type:text/html.
我找到了一个非常简单的解决方案:向邮件命令添加修饰符-aContent-Type:text/html。
In your case would be:
你的情况是:
mail -aContent-Type:text/html -s "Built notification" address@example.com < /var/www/report.csv
#1
45
This worked for me:
这工作对我来说:
echo "<b>HTML Message goes here</b>" | mail -s "$(echo -e "This is the subject\nContent-Type: text/html")" foo@example.com
#2
36
My version of mail does not have --append
and it too smart for the echo -e \n
-trick (it simply replaces \n with space). It does, however, have -a
:
我的邮件版本没有——附加,它对于echo -e \n-trick来说太智能了(它只是用空格替换\n)。但是,它有-a:
mail -a "Content-type: text/html" -s "Built notification" address@example.com < /var/www/report.html
#3
19
Make a file called tmp.html and put the following line in it:
创建一个名为tmp的文件。html并在其中加入如下一行:
<b>my bold message</b>
Then paste all this into the commandline: (with the parenthesis and all).
然后将所有这些粘贴到命令行中:(用括号和所有)。
(
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
The mail will be dispatched. And the message appeared as bold instead of with the <b>
tags.
邮件将被发出。消息显示为粗体,而不是标记。
Source:
How to send a html email with the bash command "sendmail"?
源:如何发送html电子邮件与bash命令“sendmail”?
#4
6
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.
问题是,当将文件重定向到类似的“mail”时,它只用于消息主体。文件中嵌入的任何标头都将进入正文。
Try:
试一试:
mail --append="Content-type: text/html" -s "Built notification" address@example.com < /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 To
and Subject
headers in your file, or specify them with --append, since you're implicitly setting them on the command line already (-s is the subject, and address@example.com automatically becomes the To
).
-append允许向邮件添加任意的头,在这里应该指定内容类型和内容配置。无需在文件中嵌入to和Subject头,也无需使用-append指定它们,因为您已经在命令行中隐式地设置了它们(-s是主题,address@example.com自动成为to)。
#5
4
On OS X (10.9.4), cat
works, 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")" karl@marx.com
#6
2
you should use "append" mode redirection >>
instead of >
您应该使用“append”模式重定向>>而不是>。
#7
2
Try with :
试一试:
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
#8
2
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邮件
#9
2
With heirloom-mailx you can change sendmail program to your hook script, replace headers there and then use sendmail.
使用heirloom-mailx,您可以将sendmail程序更改为hook脚本,在那里替换header,然后使用sendmail。
The script I use (~/bin/sendmail-hook
):
我使用的脚本(~/bin/sendmail-hook):
#!/bin/bash
sed '1,/^$/{
s,^\(Content-Type: \).*$,\1text/html; charset=utf-8,g
s,^\(Content-Transfer-Encoding: \).*$,\18bit,g
}' | sendmail $@
This script changes the values in the mail header as follows:
此脚本更改邮件头中的值如下:
-
Content-Type:
totext/html; charset=utf-8
- 内容类型:text / html;utf - 8字符集=
-
Content-Transfer-Encoding:
to8bit
(not sure if this is really needed). - 内容传递-编码:到8bit(不确定是否真的需要)。
To send HTML email:
发送HTML电子邮件:
mail -Ssendmail='~/bin/sendmail-hook' \
-s "Built notification" address@example.com < /var/www/report.csv
#10
0
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=(
'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
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/bash
does not always work on every system.
要生成一些输出,请注意邮件部分的反勾号,请记住,<<- operator只从行开头删除制表符(而不是空格),因此在这种情况下,复制粘贴将不起作用(需要用适当的制表符替换缩进)。或使用< <运算符,并没有缩进。希望这能帮助某人。当然,您可以在o邮件部分之外使用回签,并将输出保存到某个变量中,稍后您可以在邮件部分中使用这个变量—品味和可读性问题。我知道,# ! bin bash并不总是适用于每个系统。< p>
#11
0
I found a really easy solution: add to the mail command the modifier -aContent-Type:text/html.
我找到了一个非常简单的解决方案:向邮件命令添加修饰符-aContent-Type:text/html。
In your case would be:
你的情况是:
mail -aContent-Type:text/html -s "Built notification" address@example.com < /var/www/report.csv