2010-07-23 125 views
42

如何使用shell腳本發送HTML郵件?使用shell腳本發送HTML郵件

+1

什麼是錯是給出了答案,作爲郵件的命令是從一個shell腳本你最好的選擇?你在尋找什麼,或者他的回答缺乏什麼,你決定給它一個獎勵? – 2010-07-25 16:23:45

+0

因爲我沒有很好地理解答案。但所有其他人都能夠理解,但我無法做到...... – Tree 2010-07-26 13:44:25

+10

然後說出來。如果你不明白,不要安靜。要求澄清答案。 – Anders 2010-07-26 13:50:08

回答

50

首先你需要撰寫郵件。最低限度是由這兩個頭:

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: [email protected]" \ 
-a "MIME-Version: 1.0" \ 
-a "Content-Type: text/html" \ 
-s "This is the subject" \ 
[email protected] 

這是一個過於簡單的例子,因爲你還需要照顧字符集,編碼,行的最大長度的......但是,這是基本的想法。

或者,您可以使用Perl或PHP而不是純shell編寫您的腳本。

更新

shell腳本基本上是Unix行結尾的一個文本文件,用一條線叫shebang告訴殼開始什麼解釋它必須通過文件,按照解釋理解語言中的一些命令並具有執行權限(在Unix中是文件屬性)。例如,假設您保存下列爲hello-world

#!/bin/sh 

echo Hello, world! 

然後分配執行權限:

chmod +x hello-world 

你終於可以運行它:

./hello-world 

無論如何,這是一種與原始問題無關。在進行高級任務之前,您應該熟悉基本的shell腳本。在這裏,你有幾個鏈接對慶典,一個流行的shell:

http://www.gnu.org/software/bash/manual/html_node/index.html

http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html

+0

可以在第二部分中回答..我不知道如何使用它? – Tree 2010-07-26 15:04:28

+0

我不確定你的問題......你熟悉shell腳本嗎?你有哪些信息要發送? – 2010-07-26 19:57:08

+0

不,我從來沒有使用shell腳本..它只是我需要使用它的一部分,我的應用程序開發... – Tree 2010-07-26 21:56:21

39

的標籤包括 'sendmail的' 所以這裏使用的解決方案:

(
echo "From: m[email protected] " 
echo "To: [email protected] " 
echo "MIME-Version: 1.0" 
echo "Content-Type: multipart/alternative; " 
echo ' boundary="some.unique.value.ABC123/server.xyz.com"' 
echo "Subject: Test HTML e-mail." 
echo "" 
echo "This is a MIME-encapsulated message" 
echo "" 
echo "--some.unique.value.ABC123/server.xyz.com" 
echo "Content-Type: text/html" 
echo "" 
echo "<html> 
<head> 
<title>HTML E-mail</title> 
</head> 
<body> 
<a href='http://www.google.com'>Click Here</a> 
</body> 
</html>" 
echo "------some.unique.value.ABC123/server.xyz.com--" 
) | sendmail -t 

sendmail的包裝可以使這項工作更容易,例如,mutt

mutt -e 'set content_type="text/html"' [email protected] -s "subject" < message.html 
+0

echo'boundary =「some.unique.value.ABC123/server.xyz.com」'有什麼用? – Tree 2010-07-27 14:26:33

+0

什麼是回聲「--some.unique.value.ABC123/server.xyz.com」 – Tree 2010-07-27 14:28:50

+0

「--some.unique.value ...」,它對應於'boundary =「some.unique。值...「,是MIME分離多部分消息的方式。當它看到它時,它知道接下來是一個新的部分,並且它應該回到解析標題。這個例子比它更復雜一些,因爲多部分的東西不是絕對必要的,但是如果你發送HTML郵件,這不是一個壞主意。 – cHao 2010-07-30 18:03:06

2

另一種選擇是使用msmtp會。

你需要做的是設置您的.msmtprc像這樣的東西(例如使用Gmail):

account default 
host smtp.gmail.com 
port 587 
from [email protected] 
tls on 
tls_starttls on 
tls_trust_file ~/.certs/equifax.pem 
auth on 
user [email protected] 
password <password> 
logfile ~/.msmtp.log 

然後只要致電:

在腳本
(echo "Subject: <subject>"; echo; echo "<message>") | msmtp <[email protected]> 

更新:對於HTML郵件,您也必須放置標題,因此您可能需要製作如下文件:

From: [email protected] 
To: [email protected] 
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 protected] 

同樣可以通過命令行來進行爲好,但它可能會使用一個文件更容易。

+0

我在這裏沒有看到任何與HTML郵件相關的東西,您如何定義標頭? – eis 2015-10-28 16:15:36

+1

新增我自己。幫助我,也許還有其他人。 – eis 2015-10-28 16:35:23

0

除了由MDMA正確答案,你也可以使用郵件命令如下:

郵件[email protected] -s 「主題這裏的」 -a「的Content-Type:text/html的; charset = \「us-ascii \」「

你會得到你要找的。不要忘記在電子郵件中加入「」和「」。下面是一個簡單的腳本我用電子郵件中的HTML每日報告:

#/ bin/sh的

(貓/path/to/tomorrow.txt

的mysql -h mysqlserver -u用戶-p密碼數據庫-H -e「選擇語句;」

echo「」)|電子郵件@電子郵件。COM -s 「明天的訂單截至目前」 -a 「的Content-Type:text/html的;字符集= \」 US-ASCII \ 「」

33

到目前爲止,我已經發現了兩個在cmd中的Linux快速的方法

  1. 使用舊學校的郵件

mail -s "$(echo -e "This is Subject\nContent-Type: text/html")" [email protected] < mytest.html

  1. 使用狗

mutt -e "my_hdr Content-Type: text/html" [email protected] -s "subject" < mytest.html

+2

那封郵件/回覆技巧是GENIUS! – Nick 2014-05-08 00:01:19

+0

該解決方案非常簡單,但可以直接使用。它絕對值得讚賞。 – 2014-06-20 00:31:32

+0

在MacOSX 10.9上運行良好。非常聰明。不錯的工作。任何添加附件的技巧? – CocoaEv 2014-06-28 05:52:07

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 
+0

可能會也可能不會工作,完全取決於使用的sendmail。 – eis 2015-10-28 16:35:54

0

MIME頭和,也解決可以包含在HTML文件中它的自我。

命令

cat cpu_alert.html | /usr/lib/sendmail -t 

cpu_alert.html文件樣本。

From: [email protected] 
To: [email protected] 
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. 

例子代碼在這裏:http://sugunan.net/git/slides/shell/cpu.php