dyok 发表于 2018-8-8 12:03:46

Python+sendEmail发邮件

  Linux用户常用sendmail发送电子邮件,当您看了本文后可能会改用sendEmail去发送邮件了,呵呵。
  1 下载sendEmail
  sendEmail有Linux和windows版本软件包,依据自己的平台选择下载好了
  http://caspian.dotconf.net/menu/Software/SendEmail/
  登录上边的网址找到:
  Download
Official>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目录下。
  http://blog.csdn.net/pythonedu/article/details/10826279
  3 sendEmail命令帮助
  sendEmail软件是命令行软件,需要在Dos(shell)下执行使用。
view plaincopy
[*]  [智普教育 @ www.jeapedu.com]# sendEmail --help
[*]
[*]  sendEmail-1.56 by Brandon Zehm <caspian@dotconf.net>
[*]
[*]  Synopsis:sendEmail -f ADDRESS
[*]
[*]  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    to email address(es)
[*]  -u SUBJECT                message subject
[*]  -m MESSAGE                message body
[*]
  -s SERVER[:PORT]          smtp mail>
[*]
[*]  Optional:
[*]  -a   FILE       file attachment(s)
[*]  -ccADDRESS    ccemail address(es)
[*]  -bcc ADDRESS    bcc email address(es)
[*]  -xuUSERNAME             username for SMTP authentication
[*]  -xpPASSWORD             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-
[*]  -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>
[*]
  --help message            explain message body input and>
[*]  --help networking         explain -s, -b, etc
[*]  --help output             explain logging and other output options
[*]  --help misc               explain -o options, TLS, SMTP auth, and more
  测试用例
  我(sender@163.com)要给他/她(receiver@sina.com)发电子邮件,邮件的主题是&quot;subjectTitle&quot;,邮件的内容是说句“hello”,带了附件attach.txt文件,我邮箱sender@163.com的密码是“123456”,则使用sendEmail的命令如下:
view plaincopy
[*]  sendEmail -f sender@163.com -t receiver@sina.com -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服务器上的授权帐号(sender@163.com),
  实际上就是用参数xu后边的邮箱来真正发送邮件
  -xp 后则是smtp服务器上的授权帐号(sender@163.com)所对应的密码,
  smtp服务器要检查合法性
  -u   邮件主题
  -m邮件正文内容
  -a   邮件携带附件(attach.txt)
  各位网友在自我测试时,需对应替换。比如想用“se@163.com(密码abcdef)”发邮件给&quot;re@sina.com&quot;,邮件主题“hello”,邮件内容&quot;world&quot;,携带附件&quot;b.txt&quot;,我来替换一下如下:
view plaincopy
[*]  sendEmail -f se@163.com -t re@sina.com -s smtp.163 -xu se@163.com -xp abcdef -u hello -m world -a b.txt
  4 编写发送邮件程序
  写个Python程序来用用吧:发几百封邮件给他/她/Ta.程序名sm.py,请保存在sendEmail.exe同一目录下。
view plaincopy
[*]  import os
[*]
[*]  from = &quot;sender@163.com&quot;
[*]  to = &quot;reveiver@sina.com&quot;
[*]  subject = &quot;say hello&quot;
[*]  msg = &quot;I like you&quot;
[*]  attachfile = &quot;a.txt&quot;
[*]  c = 0
[*]  while c < 498:
[*]  os.system(&quot;sendEmail.exe -f &quot;+from+&quot; -t &quot;+to+&quot; -xu &quot;+from+&quot; -xp 123456 -u &quot;+subject+&quot; -s smtp.163.com -a &quot;+attachfile+&quot; -m &quot;+msg)
[*]  c = c + 1
    据说163邮件一天最多发500封信,之后。。。呵呵,何时解封时间不确定,我看可以多申请几个163邮箱。  在sendEmail目录下执行python sm.py即可发499封邮件给他/她/Ta.
  5. 怎样使发送邮件内容为Html的呢?
  首先在sm.py文件所在目录下创建一个html文件,例如a.html,代码如下:
view plaincopy
[*]  <html>
[*]  <body>
[*]  <B>Python培训专家</B><br>
[*]  <ahref= &quot;http://www.jeapedu.com&quot;><fontcolor = 'blue'size = '7'><u>智普教育 www.jeapedu.com</u></font></a>
[*]  </body>
[*]  </html>
  接着,需要在sendEmail命令行里增加几个可以发送html文本作为邮件正文内容的参数选项。
view plaincopy
[*]  -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网页文件作为邮件正文, 这样邮件正文里就可以有超级链接了。
  最后运行测试。
http://blog.csdn.net/pythonedu/article/details/10826279http://img.blog.csdn.net/20130901172703375?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcHl0aG9uZWR1/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast
http://blog.csdn.net/pythonedu/article/details/10826279
  测试代码如下:
view plaincopy
[*]  # coding:utf-8
[*]  # Created on 2013.8.28
[*]  # Copyright @ 智普教育
[*]  # www.jeapedu.com
[*]  # Author 智普教育博客
[*]  import os
[*]
[*]  f = &quot;jeapedu009@163.com&quot;
[*]  t=&quot;jeapedu@sina.cn&quot;
[*]  file = &quot;a.html&quot;
[*]  subject = &quot;say hello&quot;
[*]  os.system(&quot;sendEmail.exe -f &quot;+f+&quot; -t &quot;+t+&quot; -xu &quot;+f+&quot; -xp Yourpassword -u &quot;+subject+&quot; -s smtp.163.com -o message-content-type=html-o message-header=HEADER -o message-charset=CHARSET -a README.txt -o message-file=&quot;+file)
[*]  print&quot;ok&quot;
  邮箱收到邮件正文是html格式的邮件,如下所示。
http://img.blog.csdn.net/20130901172805640?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcHl0aG9uZWR1/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast
页: [1]
查看完整版本: Python+sendEmail发邮件