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格式的邮件,如下所示。



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