python发送各类邮件

来源:互联网 发布:手机pdf转软件 编辑:程序博客网 时间:2024/03/29 19:49
来源:http://www.cnblogs.com/xiaowuyi/archive/2012/03/17/2404015.html
python中email模块使得处理邮件变得比较简单,今天着重学习了一下发送邮件的具体做法,这里写写自己的的心得,也请高手给些指点。
一、相关模块介绍
发送邮件主要用到了smtplib和email两个模块,这里首先就两个模块进行一下简单的介绍:
1、smtplib模块
smtplib.SMTP([host[, port[, local_hostname[, timeout]]]])
  SMTP类构造函数,表示与SMTP服务器之间的连接,通过这个连接可以向smtp服务器发送指令,执行相关操作(如:登陆、发送邮件)。所有参数都是可选的。
host:smtp服务器主机名
port:smtp服务的端口,默认是25;如果在创建SMTP对象的时候提供了这两个参数,在初始化的时候会自动调用connect方法去连接服务器。
  smtplib模块还提供了SMTP_SSL类和LMTP类,对它们的操作与SMTP基本一致。
  smtplib.SMTP提供的方法:
SMTP.set_debuglevel(level):设置是否为调试模式。默认为False,即非调试模式,表示不输出任何调试信息。
SMTP.connect([host[, port]]):连接到指定的smtp服务器。参数分别表示smpt主机和端口。注意: 也可以在host参数中指定端口号(如:smpt.yeah.net:25),这样就没必要给出port参数。
SMTP.docmd(cmd[, argstring]):向smtp服务器发送指令。可选参数argstring表示指令的参数。
SMTP.helo([hostname]) :使用"helo"指令向服务器确认身份。相当于告诉smtp服务器“我是谁”。
SMTP.has_extn(name):判断指定名称在服务器邮件列表中是否存在。出于安全考虑,smtp服务器往往屏蔽了该指令。
SMTP.verify(address) :判断指定邮件地址是否在服务器中存在。出于安全考虑,smtp服务器往

[Python]代码片段

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# -*- coding: UTF-8 -*-
'''
发送txt文本邮件
小五义:http://www.cnblogs.com/xiaowuyi
'''
import smtplib 
from email.mime.text import MIMEText 
mailto_list=[YYY@YYY.com]
mail_host="smtp.XXX.com" #设置服务器
mail_user="XXXX"   #用户名
mail_pass="XXXXXX"  #口令
mail_postfix="XXX.com" #发件箱的后缀
   
def send_mail(to_list,sub,content): 
    me="hello"+"<"+mail_user+"@"+mail_postfix+">" 
    msg= MIMEText(content,_subtype='plain',_charset='gb2312'
    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() 
        returnTrue 
    exceptException, e: 
        printstr(e) 
        returnFalse 
if __name__ =='__main__'
    ifsend_mail(mailto_list,"hello","hello world!"): 
        print"发送成功" 
    else
        print"发送失败"

[Python]代码片段

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# -*- coding: utf-8 -*-
'''
发送html文本邮件
小五义:http://www.cnblogs.com/xiaowuyi
'''
import smtplib 
from email.mime.text import MIMEText 
mailto_list=["YYY@YYY.com"]
mail_host="smtp.XXX.com" #设置服务器
mail_user="XXX"   #用户名
mail_pass="XXXX"  #口令
mail_postfix="XXX.com" #发件箱的后缀
   
def send_mail(to_list,sub,content):  #to_list:收件人;sub:主题;content:邮件内容
    me="hello"+"<"+mail_user+"@"+mail_postfix+">"  #这里的hello可以任意设置,收到信后,将按照设置显示
    msg= MIMEText(content,_subtype='html',_charset='gb2312')   #创建一个实例,这里设置为html格式邮件
    msg['Subject']= sub    #设置主题
    msg['From']= me 
    msg['To']= ";".join(to_list) 
    try
        s= smtplib.SMTP() 
        s.connect(mail_host) #连接smtp服务器
        s.login(mail_user,mail_pass) #登陆服务器
        s.sendmail(me, to_list, msg.as_string()) #发送邮件
        s.close() 
        returnTrue 
    exceptException, e: 
        printstr(e) 
        returnFalse 
if __name__ =='__main__'
    ifsend_mail(mailto_list,"hello","<a href='http://www.cnblogs.com/xiaowuyi'>小五义</a>"): 
        print"发送成功" 
    else
        print"发送失败"

[Python]代码片段

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# -*- coding: cp936 -*-
'''
发送带附件邮件
小五义:http://www.cnblogs.com/xiaowuyi
'''
 
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import smtplib
 
#创建一个带附件的实例
msg =MIMEMultipart()
 
#构造附件1
att1 =MIMEText(open('d:\\123.rar','rb').read(),'base64', 'gb2312')
att1["Content-Type"]= 'application/octet-stream'
att1["Content-Disposition"]= 'attachment; filename="123.doc"'#这里的filename可以任意写,写什么名字,邮件中显示什么名字
msg.attach(att1)
 
#构造附件2
att2 =MIMEText(open('d:\\123.txt','rb').read(),'base64', 'gb2312')
att2["Content-Type"]= 'application/octet-stream'
att2["Content-Disposition"]= 'attachment; filename="123.txt"'
msg.attach(att2)
 
#加邮件头
msg['to']= 'YYY@YYY.com'
msg['from']= 'XXX@XXX.com'
msg['subject']= 'hello world'
#发送邮件
try:
    server= smtplib.SMTP()
    server.connect('smtp.XXX.com')
    server.login('XXX','XXXXX')#XXX为用户名,XXXXX为密码
    server.sendmail(msg['from'], msg['to'],msg.as_string())
    server.quit()
    print'发送成功'
exceptException, e: 
    printstr(e)

[Python]代码片段

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# -*- coding: cp936 -*-
import smtplib
import mimetypes
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage
 
def AutoSendMail():
    msg= MIMEMultipart()
    msg['From']= "XXX@XXX.com"
    msg['To']= "YYY@YYY.com"
    msg['Subject']= "hello world"
 
 
    txt= MIMEText("这是中文的邮件内容哦",'plain','gb2312')    
    msg.attach(txt)
     
 
    file1= "C:\\hello.jpg"
    image= MIMEImage(open(file1,'rb').read())
    image.add_header('Content-ID','<image1>')
    msg.attach(image)
     
     
    server= smtplib.SMTP()
    server.connect('smtp.XXX.com')
    server.login('XXX','XXXXXX')
    server.sendmail(msg['From'],msg['To'],msg.as_string())
    server.quit()
     
if __name__ =="__main__":
    AutoSendMail()