Linux用戶常用sendmail發送電子郵件,當您看了本文後可能會改用sendEmail去發送郵件了,呵呵。
1 下載sendEmail
sendEmail有Linux和windows版本軟件包,依據自己的平臺選擇下載好了
http://caspian.dotconf.net/menu/Software/SendEmail/
登錄上邊的網址找到:
Download
- Official Release:sendEmail-v1.56.tar.gz (29kb Sep 29th, 2009) ChangelogScreen Shot
Windows Download:
Free sendEmail.exe for Windows. To use simply run sendEmail.exe from a console / command line.
sendEmail-v156-notls.zip (677kb Sep 29th, 2009) No TLS support
sendEmail-v156.zip (1.4mb Sep 29th, 2009) TLS supported
RPM Package:sendEmail rpm - 選擇適合自己的下載好了
2 安裝sendEmail
sendEmail不需要安裝直接解壓到某目錄即可直接使用。
(Linux用戶) tar -zxvf sendEmail-x-y-z.tar.gz
本文選擇windows平臺sendEmail軟件包,下載後解壓到c:\sendEmail目錄下。
3 sendEmail命令幫助
sendEmail軟件是命令行軟件,需要在Dos(shell)下執行使用。
[智普教育 @ www.jeapedu.com]# sendEmail --help
sendEmail-1.56 by Brandon Zehm <[email protected]>
Synopsis: sendEmail -f ADDRESS [options]
Required:
-f ADDRESS from (sender) email address
* At least one recipient required via -t, -cc, or -bcc
* Message body required via -m, STDIN, or -o message-file=FILE
Common:
-t ADDRESS [ADDR ...] to email address(es)
-u SUBJECT message subject
-m MESSAGE message body
-s SERVER[:PORT] smtp mail relay, default is localhost:25
Optional:
-a FILE [FILE ...] file attachment(s)
-cc ADDRESS [ADDR ...] cc email address(es)
-bcc ADDRESS [ADDR ...] bcc email address(es)
-xu USERNAME username for SMTP authentication
-xp PASSWORD password for SMTP authentication
Paranormal:
-b BINDADDR[:PORT] local host bind address
-l LOGFILE log to the specified file
-v verbosity, use multiple times for greater effect
-q be quiet (i.e. no STDOUT output)
-o NAME=VALUE advanced options, for details try: --help misc
-o message-content-type=<auto|text|html>
-o message-file=FILE -o message-format=raw
-o message-header=HEADER -o message-charset=CHARSET
-o reply-to=ADDRESS -o timeout=SECONDS
-o username=USERNAME -o password=PASSWORD
-o tls=<auto|yes|no> -o fqdn=FQDN
Help:
--help the helpful overview you're reading now
--help addressing explain addressing and related options
--help message explain message body input and related options
--help networking explain -s, -b, etc
--help output explain logging and other output options
--help misc explain -o options, TLS, SMTP auth, and more
測試用例
我([email protected])要給他/她([email protected])發電子郵件,郵件的主題是"subjectTitle",郵件的內容是說句“hello”,帶了附件attach.txt文件,我郵箱[email protected]的密碼是“123456”,則使用sendEmail的命令如下:
sendEmail -f sender@163.com -t [email protected] -s smtp.163.com -xu sender@163.com -xp 123456 -u subjectTitle -m hello -a attach.txt
注:本示例在windows平臺下測試,需手動使用dos即運行->cmd->cd c:/sendEmail目錄(假設下載文件解壓到本目錄)
-t 後指定發給誰
-s 指定發送smtp服務器,本例用163的smtp服務器
-xu 後需指定smtp服務器上的授權帳號([email protected]),
實際上就是用參數xu後邊的郵箱來真正發送郵件
-xp 後則是smtp服務器上的授權帳號([email protected])所對應的密碼,
smtp服務器要檢查合法性
-u 郵件主題
-m 郵件正文內容
-a 郵件攜帶附件(attach.txt)
各位網友在自我測試時,需對應替換。比如想用“[email protected](密碼abcdef)”發郵件給"[email protected]",郵件主題“hello”,郵件內容"world",攜帶附件"b.txt",我來替換一下如下:
sendEmail -f se@163.com -t [email protected] -s smtp.163 -xu se@163.com -xp abcdef -u hello -m world -a b.txt
4 編寫發送郵件程序
寫個Python程序來用用吧:發幾百封郵件給他/她/Ta.程序名sm.py,請保存在sendEmail.exe同一目錄下。
import os
from = "[email protected]"
to = "[email protected]"
subject = "say hello"
msg = "I like you"
attachfile = "a.txt"
c = 0
while c < 498:
os.system("sendEmail.exe -f "+from+" -t "+to+" -xu "+from+" -xp 123456 -u "+subject+" -s smtp.163.com -a "+attachfile+" -m "+msg)
c = c + 1
在sendEmail目錄下執行python sm.py即可發499封郵件給他/她/Ta.
5. 怎樣使發送郵件內容爲Html的呢?
首先在sm.py文件所在目錄下創建一個html文件,例如a.html,代碼如下:
<html>
<body>
<B>Python培訓專家</B><br>
<ahref= "http://www.jeapedu.com"><fontcolor = 'blue'size = '7'><u>智普教育 www.jeapedu.com</u></font></a>
</body>
</html>
接着,需要在sendEmail命令行裏增加幾個可以發送html文本作爲郵件正文內容的參數選項。
-o message-content-type=html
-o message-charset=CHARSET
-o message-header=HEADER
-o message-file=afile
-o message-content-type = html
告訴郵件服務器發送的是html格式郵件內容
-o message-charset = CHARSET
這裏不能用utf-8否則郵件內容顯示亂碼
-o message-file=某文件
是用來指定攜帶的那個html網頁文件作爲郵件正文, 這樣郵件正文裏就可以有超級鏈接了。
最後運行測試。
測試代碼如下:
# coding:utf-8
# Created on 2013.8.28
# Copyright @ 智普教育
# www.jeapedu.com
# Author 智普教育博客
import os
f = "[email protected]"
file = "a.html"
subject = "say hello"
os.system("sendEmail.exe -f "+f+" -t "+t+" -xu "+f+" -xp Yourpassword -u "+subject+" -s smtp.163.com -o message-content-type=html -o message-header=HEADER -o message-charset=CHARSET -a README.txt -o message-file="+file)
print"ok"
郵箱收到郵件正文是html格式的郵件,如下所示。