如何使用shell脚本发送HTML电子邮件?
答案 0 :(得分:57)
首先,您需要撰写邮件。最低限度由这两个标题组成:
MIME-Version: 1.0
Content-Type: text/html
...以及相应的邮件正文:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head><title></title>
</head>
<body>
<p>Hello, world!</p>
</body>
</html>
获得后,您可以将适当的信息传递给mail命令:
body = '...'
echo $body | mail \
-a "From: me@example.com" \
-a "MIME-Version: 1.0" \
-a "Content-Type: text/html" \
-s "This is the subject" \
you@example.com
这是一个过于简单的例子,因为你还需要处理字符集,编码,最大行长......但这基本上就是这个想法。
或者,您可以使用Perl或PHP而不是纯shell编写脚本。
shell脚本基本上是一个带有Unix行结尾的文本文件,它以一个名为shebang的行开头,告诉shell它必须将文件传递给哪个解释器,按照解释器理解的语言执行一些命令执行权限(在Unix中是文件属性)。例如,假设您将以下内容保存为hello-world
:
#!/bin/sh
echo Hello, world!
然后分配执行权限:
chmod +x hello-world
你最终可以运行它:
./hello-world
无论如何,这与原始问题无关。在使用它执行高级任务之前,您应该熟悉基本的shell脚本。这里有一些关于 bash 的链接,这是一个受欢迎的shell:
http://www.gnu.org/software/bash/manual/html_node/index.html
答案 1 :(得分:45)
答案 2 :(得分:37)
到目前为止,我已经在cmd linux中找到了两种快速方法
mail -s "$(echo -e "This is Subject\nContent-Type: text/html")" test@yahoo.com < mytest.html
mutt -e "my_hdr Content-Type: text/html" test@yahoo.com -s "subject" < mytest.html
答案 3 :(得分:2)
另一个选项是sendEmail脚本http://caspian.dotconf.net/menu/Software/SendEmail/,它还允许您将消息类型设置为html并包含一个文件作为消息正文。有关详细信息,请参阅链接。
答案 4 :(得分:2)
另一种选择是使用msmtp。
你需要的是用这样的东西设置.msmtprc(例如使用gmail):
account default
host smtp.gmail.com
port 587
from example@gmail.com
tls on
tls_starttls on
tls_trust_file ~/.certs/equifax.pem
auth on
user example@gmail.com
password <password>
logfile ~/.msmtp.log
然后打电话:
(echo "Subject: <subject>"; echo; echo "<message>") | msmtp <email@domain.tld>
你脚本中的
更新:对于HTML邮件,你也必须放置标题,所以你可能想要制作这样的文件:
From: sender@domain.tld
To: email@domain.tld
Subject: Important message
Mime-Version: 1.0
Content-Type: text/html
<h1>Mail body will be here</h1>
The mail body <b>should</b> start after one blank line from the header.
并将其邮寄为
cat email-template | msmtp email@domain.tld
同样可以通过命令行完成,但使用文件可能更容易。
答案 5 :(得分:0)
cat > mail.txt <<EOL
To: <email>
Subject: <subject>
Content-Type: text/html
<html>
$(cat <report-table-*.html>)
This report in <a href="<url>">SVN</a>
</html>
EOL
然后:
sendmail -t < mail.txt
答案 6 :(得分:0)
Mime标题和来自,地址也可以自己包含在html文件中。
命令
cat cpu_alert.html | /usr/lib/sendmail -t
cpu_alert.html文件样本。
From: donotreply@example.com
To: admin@example.com
Subject: CPU utilization heigh
Mime-Version: 1.0
Content-Type: text/html
<h1>Mail body will be here</h1>
The mail body should start after one blank line from the header.
答案 7 :(得分:0)
这里是我的(鉴于“邮件”已正确配置):
scanuser @ owncloud:〜$ vi sendMailAboutNewDocuments.sh
mail -s "You have new mail" -a "Content-type: text/html" -a "From: sender@xxx.com" $1 << EOF
<html>
<body>
Neues Dokument: $2<br>
<a href="https://xxx/index.php/apps/files/?dir=/Post">Hier anschauen</a>
</body>
</html>
EOF
使可执行文件:
chmod +x sendMailAboutNewDocuments.sh
然后致电:
./sendMailAboutNewDocuments.sh recipient@xxx.com test.doc
答案 8 :(得分:0)
使用CentOS 7的默认mailx(显示为传家宝mailx),我已将其简化为仅使用带有所需标题的文本文件以及用于多部分/混合和多部分/替代设置的静态边界。
如果您要使用相同的设置,我确定您可以弄清楚多部分/相关部分。
test.txt:
--000000000000f3b2150570186a0e
Content-Type: multipart/alternative; boundary="000000000000f3b2130570186a0c"
--000000000000f3b2130570186a0c
Content-Type: text/plain; charset="UTF-8"
This is my plain text stuff here, in case the email client does not support HTML or is blocking it purposely
My Link Here <http://www.example.com>
--000000000000f3b2130570186a0c
Content-Type: text/html; charset="UTF-8"
<div dir="ltr">
<div>This is my HTML version of the email</div>
<div><br></div>
<div><a href="http://www.example.com">My Link Here</a><br></div>
</div>
--000000000000f3b2130570186a0c--
--000000000000f3b2150570186a0e
Content-Type: text/csv; charset="US-ASCII"; name="test.csv"
Content-Disposition: attachment; filename="test.csv"
Content-Transfer-Encoding: base64
X-Attachment-Id: f_jj5qmzqz0
边界定义了多部分分段。
结尾处没有破折号的边界ID是线段的起点。
最后带有两个破折号的是终点。
在此示例中,multipart / mixed主要部分中有一个subpart,用于multipart / alternative。
multipart / alternative方法基本上说“回退,如果优先级部分不成功”-在此示例中,电子邮件客户端通常将HTML作为优先级。如果电子邮件客户端不会显示HTML,则会退回到纯文本。
封装整个消息的multipart / mixed方法,基本上是说这里有不同的内容,同时显示两者。
在此示例中,我在电子邮件中放置了CSV文件附件。您将在下面的命令中看到使用base64插入附件。
我以附件为例,您必须为附件适当地设置内容类型,并指定是否内联。
某些供应商需要X-Attachment-Id,请随机设置您设置的ID。
发送此命令的命令是:
echo -e "`cat test.txt; openssl base64 -e < test.csv`\n--000000000000f3b2150570186a0e--\n" | mailx -s "Test 2 $( echo -e "\nContent-Type: multipart/mixed; boundary=\"000000000000f3b2150570186a0e\"" )" -r fromaddress@example.com toaddress@example.com
正如您在mailx主题行中看到的那样,我静态插入了多部分边界,这是电子邮件客户端将看到的第一个标头。
然后是转储的test.txt内容。
关于附件,我使用openssl(在系统上是非常标准的)将文件附件转换为base64。
此外,我在回显的末尾添加了边界关闭语句,以表示消息的结尾。
这可以解决传家宝邮件问题,并且几乎不需要脚本。
回声可以是提要,也可以是任何其他方法。
答案 9 :(得分:0)
您可以在sendEmail中使用-o选项来发送HTML电子邮件。
-o message-content-type = html以指定电子邮件的内容类型。
-o消息文件,用于将html文件添加到电子邮件内容中。
我已经在shell脚本中尝试过此选项,并且可以正常工作。
这是完整的命令:
/usr/local/bin/sendEmail -f sender@test.com -t "reciever@test.com" -s \
smtp.test.com -u "Title" -xu sender@test.com -xp password \
-o message-charset=UTF-8 \
-o message-content-type=html \
-o message-file=test.html
答案 10 :(得分:0)
我一直在尝试制作一个简单的bash脚本,通过电子邮件发送html格式的内容类型,这些都是很棒的,但是我不想在文件系统上创建本地文件以传递到脚本中,并且在我们的mailx(12.5+)版本中,-a邮件参数不再起作用,因为它添加了附件,而且我找不到其他标头的替换参数,所以对我来说最简单的方法是使用sendmail。>
下面是我创建的最简单的1个衬纸,它在对我们有用的bash脚本中运行。它基本上只是传递Content-Type:text / html,主题和正文并起作用。
printf "Content-Type: text/html\nSubject: Test Email\nHTML BODY<b>test bold</b>" | sendmail <Email Address To>
如果您想通过变量创建整个html页面,我在bash脚本中使用的另一种方法是按如下方式传递变量。
emailBody="From: <Email Address From>
Subject: Test
Content-Type: text/html; charset=\"us-ascii\"
<html>
<body>
body
<b> test bold</b>
</body>
</html>
"
echo "$emailBody" | sendmail <Email Address To>
答案 11 :(得分:0)
Heirloom-mailx 12.4面临类似的问题。感谢这篇文章,我非常接近解决方案:
https://unix.stackexchange.com/questions/384536/html-email-from-heirloom-mailx-on-linux
现在它以html格式发送,但是不幸的是,标题被压入了正文中。当-a
版本中的头文件不使用mailx
时,以下代码几乎可以解决问题。
mailx -v -S smtp=SERVER -s "$(echo -e "This is the subject v1\nContent-Type: text/html\nMIME-Version: 1.0\nContent-Disposition: inline")" -r FROM TO < htmlmail.txt
很高兴听到有人提出建议,以帮助摆脱目前被压入体内的头部。
答案 12 :(得分:0)
在shell script
上专门询问的问题和仅提及sendmail
包裹的问号。因此,如果有人在寻找这个,这是具有sendmail使用情况的简单脚本,它在CentOS 8上对我有用:
#!/bin/sh
TOEMAIL="youremail@server.com"
REPORT_FILE_HTML="$REPORT_FILE.html"
echo "Subject: EMAIL SUBJECT" >> "${REPORT_FILE_HTML}"
echo "MIME-Version: 1.0" >> "${REPORT_FILE_HTML}"
echo "Content-Type: text/html" >> "${REPORT_FILE_HTML}"
echo "<html>" >> "${REPORT_FILE_HTML}"
echo "<head>" >> "${REPORT_FILE_HTML}"
echo "<title>Best practice to include title to view online email</title>" >> "${REPORT_FILE_HTML}"
echo "</head>" >> "${REPORT_FILE_HTML}"
echo "<body>" >> "${REPORT_FILE_HTML}"
echo "<p>Hello there, you can put email html body here</p>" >> "${REPORT_FILE_HTML}"
echo "</body>" >> "${REPORT_FILE_HTML}"
echo "</html>" >> "${REPORT_FILE_HTML}"
sendmail $TOEMAIL < $REPORT_FILE_HTML
答案 13 :(得分:-1)
除了mdma的正确答案之外,您还可以使用mail命令,如下所示:
mail person@email.com -s"Subject Here" -a"Content-Type: text/html; charset=\"us-ascii\""
你会得到你想要的东西。不要忘记将<HTML>
和</HTML>
放入电子邮件中。这是我用来通过HTML发送日常报告的快速脚本:
#!/bin/sh
(cat /path/to/tomorrow.txt mysql -h mysqlserver -u user -pPassword Database -H -e "select statement;" echo "</HTML>") | mail email@email.com -s"Tomorrow's orders as of now" -a"Content-Type: text/html; charset=\"us-ascii\""