-
转自http://jingyan.baidu.com/article/b24f6c822784b886bfe5dabe.html
-
smtplib模块:
smtplib.SMTP([host[, port[, local_hostname[, timeout]]]])
此为SMTP类构造函数,表示与SMTP服务器之间的连接,并根据这个连接向smtp服务器发送指令,执行相关操作(如:登陆、发送邮件),且每个参数都是可选的。
其中最重要的参数:
host:smtp服务器主机名
port:smtp服务的端口,默认是25;
如果在创建SMTP对象的时候提供了这两个参数,在初始化的时候会自动调用connect方法去连接服务器。
smtplib.SMTP还提供了如下方法:
SMTP.set_debuglevel(level):设置是否为调试模式
SMTP.connect([host[, port]]):连接到指定的smtp服务器。参数分别表示 smpt主机和端口。
SMTP.docmd(cmd[, argstring]):向smtp服务器发送指令。
SMTP.helo([hostname]) :使用"helo"指令向服务器确认身份。
SMTP.login(user, password):登陆到smtp服务器。现在几乎所有smtp服务器,都必须在验证用户信息合法之后才允许发送邮件。(重要!)
SMTP.sendmail(from_addr,to_addrs,msg[,mail_options,rcpt_options]):发送邮件。这里要注意一下第三个参数,msg是字符串,表示邮件。我们知道邮件一般由标题,发信人,收件人,邮件内容,附件等构成,发送邮件的时候,要注意msg的格式。这个格式就是smtp协议中定义的格式。SMTP.quit() :断开与smtp服务器的连接,相当于发送"quit"指令。(重要!)
常用的函数方法:
-
-
-
email模块
1.class email.message.Message
__getitem__,__setitem__实现obj[key]形式的访问。
Msg.attach(playload): 向当前Msg添加playload。
Msg.set_playload(playload):
Msg.add_header(_name, _value, **_params): 添加邮件头字段。
2.class email.mime.base.MIMEBase(_maintype, _subtype, **_params)
所有MIME类的基类,是email.message.Message类的子类。
3.class email.mime.multipart.MIMEMultipart()
在3.0版本的email模块 (Python 2.3-Python 2.5) 中,这个类位于email.MIMEMultipart.MIMEMult ipart。这个类是MIMEBase的直接子类,用来生成包含多个部分的邮件体的MIME对象。
4.class email.mime.text.MIMEText(_text)
使用字符串_text来生成MIME对象的主体文本。
-
-
编写程序如下:
#! /usr/bin/env python
import smtpli
from email.mime.text import MIMEText
mailto_list=['xxxx@xxx.com'] #收件人(列表)
mail_host="smtp.163.com" #使用的邮箱的smtp服务器地址
mail_user="name" #用户名
mail_pass="pwd" #密码
mail_postfix="postfix" #邮箱的后缀
def send_mail(to_list,sub,content):
me="hello"+"<"+mail_user+"@"+mail_postfix+">"
msg = MIMEText(content,_subtype='plain')
msg['Subject'] = sub
msg['From'] = me
msg['To'] = ";".join(to_list) #将收件人列表以‘;’分隔
try:
server = smtplib.SMTP()
server.connect(mail_host) #连接服务器
server.login(mail_user,mail_pass) #登录操作
server.sendmail(me, to_list, msg.as_string())
server.close()
return True
except Exception, e:
print str(e)
return False
for i in range(5): #发送五封,不过会被拦截的。。。
if send_mail(mailto_list,"hello","haha!"): #邮件主题和邮件内容
print "done!"
else:
print "failed!"
----------------------------------------------------------------------
-
发送文本
-
#!/usr/bin/env python3 #coding: utf-8 import smtplib from email.mime.text import MIMEText from email.header import Header sender = '***' receiver = '***' subject = 'python email test' smtpserver = 'smtp.163.com' username = '***' password = '***' msg = MIMEText('你好','text','utf-8')#中文需参数‘utf-8’,单字节字符不需要 msg['Subject'] = Header(subject, 'utf-8') smtp = smtplib.SMTP() smtp.connect('smtp.163.com') smtp.login(username, password) smtp.sendmail(sender, receiver, msg.as_string()) smtp.quit()
- 发送附件
-
#!/usr/bin/env python3 #coding: utf-8 import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from email.mime.image import MIMEImage sender = '***' receiver = '***' subject = 'python email test' smtpserver = 'smtp.163.com' username = '***' password = '***' msgRoot = MIMEMultipart('related') msgRoot['Subject'] = 'test message' #构造附件 att = MIMEText(open('h:\\python\\1.jpg', 'rb').read(), 'base64', 'utf-8') att["Content-Type"] = 'application/octet-stream' att["Content-Disposition"] = 'attachment; filename="1.jpg"' msgRoot.attach(att) smtp = smtplib.SMTP() smtp.connect('smtp.163.com') smtp.login(username, password) smtp.sendmail(sender, receiver, msgRoot.as_string()) smtp.quit()