問題描述
如何使用 shell 腳本發送 HTML 電子郵件?
最佳解決方案
首先你需要撰寫消息。最低限度由這兩個頭組成:
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 。
Update
一個 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
http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html
次佳解決方案
標籤包括’sendmail’,所以這裏有一個解決方案:
(
echo "From: me@xyz.com "
echo "To: them@xyz.com "
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"' me@mydomain.com -s "subject" < message.html
第三種解決方案
到目前為止,我已經在 cmd linux 中找到了兩個快捷方式
-
使用舊的郵件
mail -s "$(echo -e "This is SubjectnContent-Type: text/html")" test@yahoo.com < mytest.html
-
使用 mutt
mutt -e "my_hdr Content-Type: text/html" test@yahoo.com -s "subject" < mytest.html
參考文獻
注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。