设为首页 收藏本站
查看: 861|回复: 0

[经验分享] Python+sendEmail发邮件

[复制链接]

尚未签到

发表于 2018-8-8 12:03:46 | 显示全部楼层 |阅读模式
  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目录下。
  
  3 sendEmail命令帮助
  sendEmail软件是命令行软件,需要在Dos(shell)下执行使用。
[python]view plaincopy

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

  •   sendEmail-1.56 by Brandon Zehm <caspian@dotconf.net>

  •   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>

  •   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-
  •   -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的命令如下:
[python]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;,我来替换一下如下:
[python]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同一目录下。
[python]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,代码如下:
[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文本作为邮件正文内容的参数选项。
[python]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网页文件作为邮件正文, 这样邮件正文里就可以有超级链接了。
  最后运行测试。


  测试代码如下:
[python]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格式的邮件,如下所示。

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-548645-1-1.html 上篇帖子: python django web项目的构建步骤(二) 下篇帖子: python写的系统常用命令(二)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表