Python的SMTP发送邮件学习记录
2015-11-01 21:01
741 查看
这是我的第一个用markdown写的博客
首先上代码,这是我在学习了用Python邮箱编程后模仿写的测试代码这是版本一的代码:
from email.mime.text import MIMEText msg=MIMEText('hello,send by python...','plain','utf-8') from_addr=raw_input('From: ') password=raw_input('Password: ') smtp_server=raw_input('SMTP server: ') to_addr=raw_input('To: ') import smtplib server=smtplib.SMTP(smtp_server,25) server.set_debuglevel(1) server.starttls() server.login(from_addr,password) server.sendmail(from_addr,[to_addr],msg.as_string()) server.quit()
此版本的特点是:
邮件没有主题
收件人的名字没有显示为友好的名字,比如Mr Green green@example.com
明明收到了邮件,却提示不在收件人中
于是做了测试,用qq邮箱发给outlook与gmail,试了很多次,邮件都被回退了;相反,用qq给自家(另外一个邮箱)发,确成功了。
QXdeMacBook-Pro:workspace qx$ python smtp.py From: xqconverse@foxmail.com Password: ********(for secret!) SMTP server: smtp.qq.com To: 2453694798@qq.com send: 'ehlo QXdeMacBook-Pro.local\r\n' reply: '250-smtp.qq.com\r\n' reply: '250-PIPELINING\r\n' reply: '250-SIZE 73400320\r\n' reply: '250-STARTTLS\r\n' reply: '250-AUTH LOGIN PLAIN\r\n' reply: '250-AUTH=LOGIN\r\n' reply: '250-MAILCOMPRESS\r\n' reply: '250 8BITMIME\r\n' reply: retcode (250); Msg: smtp.qq.com PIPELINING SIZE 73400320 STARTTLS AUTH LOGIN PLAIN AUTH=LOGIN MAILCOMPRESS 8BITMIME send: 'STARTTLS\r\n' reply: '220 Ready to start TLS\r\n' reply: retcode (220); Msg: Ready to start TLS send: 'ehlo QXdeMacBook-Pro.local\r\n' reply: '250-smtp.qq.com\r\n' reply: '250-PIPELINING\r\n' reply: '250-SIZE 73400320\r\n' reply: '250-AUTH LOGIN PLAIN\r\n' reply: '250-AUTH=LOGIN\r\n' reply: '250-MAILCOMPRESS\r\n' reply: '250 8BITMIME\r\n' reply: retcode (250); Msg: smtp.qq.com PIPELINING SIZE 73400320 AUTH LOGIN PLAIN AUTH=LOGIN MAILCOMPRESS 8BITMIME send: 'AUTH PLAIN AHhxY29udmVyc2VAZm94bWFpbC5jb20AMTExMjIwcXg=\r\n' reply: '235 Authentication successful\r\n' reply: retcode (235); Msg: Authentication successful send: 'mail FROM:<xqconverse@foxmail.com> size=128\r\n' reply: '250 Ok\r\n' reply: retcode (250); Msg: Ok send: 'rcpt TO:<2453694798@qq.com>\r\n' reply: '250 Ok\r\n' reply: retcode (250); Msg: Ok send: 'data\r\n' reply: '354 End data with <CR><LF>.<CR><LF>\r\n' reply: retcode (354); Msg: End data with <CR><LF>.<CR><LF> data: (354, 'End data with <CR><LF>.<CR><LF>') send: 'Content-Type: text/plain; charset="utf-8"\r\nMIME-Version: 1.0\r\nContent-Transfer-Encoding: base64\r\n\r\naGVsbG8sc2VuZCBieSBweXRob24uLi4=\r\n.\r\n' reply: '250 Ok: queued as \r\n' reply: retcode (250); Msg: Ok: queued as data: (250, 'Ok: queued as') send: 'quit\r\n' reply: '221 Bye\r\n' reply: retcode (221); Msg: Bye
版本一的缺点,是因为邮件主题、如何显示发件人、收件人等信息并不是通过SMTP协议发给MTA,而是包含在发给MTA的文本中的,所以,我们必须把From、To和Subject添加到MIMEText中,才是一封完整的邮件:
以下就是版本二:
from email import encoders from email.header import Header from email.mime.text import MIMEText from email.utils import parseaddr, formataddr import smtplib def _format_addr(s): name, addr=parseaddr(s) return formataddr((\ Header(name,'utf-8').encode(),\ addr.encode('utf-8') if isinstance(addr,unicode) else addr)) from_addr=raw_input('From: ') password=raw_input('Password: ') to_addr=raw_input('To: ') smtp_server=raw_input('SMTP Server: ') msg=MIMEText('hello, send by python...','plain','utf-8') msg['From']=_format_addr(u'Python Lover <%s>' % from_addr) msg['To']=_format_addr(u'Admin <%s>' % to_addr) msg['Subject']=Header(u'Hello sent from SMTP....','utf-8').encode() server=smtplib.SMTP(smtp_server,25) server.set_debuglevel(1) server.login(from_addr,password) server.sendmail(from_addr,[to_addr],msg.as_string()) server.quit()
点击参照原文
这次我再次用了自己的qq邮箱做实验,发给outlook、gmail与qq邮箱,都成功了!原来当时邮件被回退是应为,缺少了一些信息,比如邮件主题、发件人、收件人。
相关文章推荐
- python的属性和方法
- Python3 Internet Protocols an Support
- LIBSVM在python下的使用
- 解析python中:if __name__=='__main__':
- 用Python实现一个1加到n求和功能的函数
- LeetCode----Word Pattern
- Python_List对象内置方法详解
- Python学习笔记(二)
- Python_序列对象内置方法详解_String
- python基础 之 装饰器详解
- Python yield 使用浅析
- python正则表达式
- python 随机产生多维高斯分布点
- python配置mysqldb环境
- Python使用urllib2 POST数组
- Eclipse搭建Python和Django环境
- python常用模块
- python装饰器
- python-time模块
- python正则表达式