Python+sendEmail發郵件

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)下執行使用。


  1. [智普教育 @ www.jeapedu.com]# sendEmail --help

  2. sendEmail-1.56 by Brandon Zehm <[email protected]>  

  3. Synopsis:  sendEmail -f ADDRESS [options]  

  4. Required:  

  5. -f ADDRESS                from (sender) email address  

  6. * At least one recipient required via -t, -cc, or -bcc  

  7. * Message body required via -m, STDIN, or -o message-file=FILE  

  8. Common:  

  9. -t ADDRESS [ADDR ...]     to email address(es)  

  10. -u SUBJECT                message subject  

  11. -m MESSAGE                message body  

  12. -s SERVER[:PORT]          smtp mail relay, default is localhost:25

  13. Optional:  

  14. -a   FILE [FILE ...]      file attachment(s)  

  15. -cc  ADDRESS [ADDR ...]   cc  email address(es)  

  16. -bcc ADDRESS [ADDR ...]   bcc email address(es)  

  17. -xu  USERNAME             username for SMTP authentication  

  18. -xp  PASSWORD             password for SMTP authentication  

  19. Paranormal:  

  20. -b BINDADDR[:PORT]        local host bind address  

  21. -l LOGFILE                log to the specified file  

  22. -v                        verbosity, use multiple times for greater effect  

  23. -q                        be quiet (i.e. no STDOUT output)  

  24. -o NAME=VALUE             advanced options, for details try: --help misc  

  25. -o message-content-type=<auto|text|html>  

  26. -o message-file=FILE         -o message-format=raw  

  27. -o message-header=HEADER     -o message-charset=CHARSET  

  28. -o reply-to=ADDRESS          -o timeout=SECONDS  

  29. -o username=USERNAME         -o password=PASSWORD  

  30. -o tls=<auto|yes|no>         -o fqdn=FQDN  

  31. Help:  

  32. --help                    the helpful overview you're reading now  

  33. --help addressing         explain addressing and related options  

  34. --help message            explain message body input and related options  

  35. --help networking         explain -s, -b, etc  

  36. --help output             explain logging and other output options  

  37. --help misc               explain -o options, TLS, SMTP auth, and more  


     測試用例

我([email protected])要給他/她([email protected])發電子郵件,郵件的主題是"subjectTitle",郵件的內容是說句“hello”,帶了附件attach.txt文件,我郵箱[email protected]的密碼是“123456”,則使用sendEmail的命令如下:


  1. 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目錄(假設下載文件解壓到本目錄)


  -f    後指定在郵箱裏顯示誰發的郵件


  -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",我來替換一下如下:


  1. 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同一目錄下。

  1. import os  

  2. from = "[email protected]"

  3. to = "[email protected]"

  4. subject = "say hello"

  5. msg = "I like you"

  6. attachfile = "a.txt"

  7. c = 0

  8. while c < 498:  

  9.    os.system("sendEmail.exe -f "+from+" -t "+to+" -xu "+from+" -xp 123456 -u "+subject+" -s smtp.163.com -a "+attachfile+" -m "+msg)  

  10.    c = c + 1

   據說163郵件一天最多發500封信,之後。。。呵呵,何時解封時間不確定,我看可以多申請幾個163郵箱。


   在sendEmail目錄下執行python sm.py即可發499封郵件給他/她/Ta.


5. 怎樣使發送郵件內容爲Html的呢?

   首先在sm.py文件所在目錄下創建一個html文件,例如a.html,代碼如下:

  1. <html>

  2. <body>

  3. <B>Python培訓專家</B><br>

  4. <ahref= "http://www.jeapedu.com"><fontcolor = 'blue'size = '7'><u>智普教育 www.jeapedu.com</u></font></a>

  5. </body>

  6. </html>


   接着,需要在sendEmail命令行裏增加幾個可以發送html文本作爲郵件正文內容的參數選項。


  1. -o message-content-type=html  

  2. -o message-charset=CHARSET  

  3. -o message-header=HEADER    

  4. -o message-file=afile  


    -o message-content-type = html

                告訴郵件服務器發送的是html格式郵件內容

    -o message-charset = CHARSET

               這裏不能用utf-8否則郵件內容顯示亂碼

    -o message-file=某文件  

               是用來指定攜帶的那個html網頁文件作爲郵件正文, 這樣郵件正文裏就可以有超級鏈接了。

    最後運行測試。



   測試代碼如下:

  1. # coding:utf-8

  2. # Created on 2013.8.28

  3. # Copyright @ 智普教育

  4. # www.jeapedu.com

  5. # Author 智普教育博客

  6. import os  

  7. f = "[email protected]"

  8. t="[email protected]"

  9. file = "a.html"

  10. subject = "say hello"

  11. 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)  

  12. print"ok"


   郵箱收到郵件正文是html格式的郵件,如下所示。



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章