ITPub博客

首页 > 应用开发 > IT综合 > 通过python发邮件

通过python发邮件

原创 IT综合 作者:suzhouclark 时间:2009-05-26 16:19:23 0 删除 编辑
通过python发邮件[@more@]


email模块提供了把body组织成二进制流的方法,配合 smtplib模块使用可以实现发送附件(attachment)等功能。email模块把所有的内容包括正文、附件、时间、From、To、 Subject作为邮件的各个部分对象attach到邮件对象的根容器中,然后把容器转成成字符串(上例的body),然后通过 smtp.sendmail发送。
# -*- coding: gbk -*-

import smtplib
import email.MIMEMultipart
import email.MIMEText
import email.MIMEBase
import os.path

def sendmail(from_addr, to_addr, subject, content, attachfilelist):
"""
attachfilelist use ;
"""
server = smtplib.SMTP("proxy-in.xxxx.com")
#server.login("username","password");

# 构造MIMEMultipart对象做为根容器
main_msg = email.MIMEMultipart.MIMEMultipart()

# 构造MIMEText对象做为邮件显示内容并附加到根容器
text_msg = email.MIMEText.MIMEText(content)
main_msg.attach(text_msg)

# 构造MIMEBase对象做为文件附件内容并附加到根容器
contype = 'application/octet-stream'
maintype, subtype = contype.split('/', 1)

# 读入文件内容并格式化
file_list=attachfilelist.split(';')
if (len(attachfilelist)>0 and len(file_list)>0):
for file_name in attachfilelist.split(';'):
if(len(file_name)>1):
data = open(file_name, 'rb')
file_msg = email.MIMEBase.MIMEBase(maintype, subtype)
file_msg.set_payload(data.read( ))
data.close( )
email.Encoders.encode_base64(file_msg)

## 设置附件头
basename = os.path.basename(file_name)
file_msg.add_header('Content-Disposition', 'attachment', filename = file_name)
main_msg.attach(file_msg)

# 设置根容器属性
main_msg['From'] = from_addr
main_msg['To'] = to_addr
main_msg['Subject'] = subject
main_msg['Date'] = email.Utils.formatdate( )

# 得到格式化后的完整文本
fullText = main_msg.as_string()
# 用smtp发送邮件
to_list=[]
for addr in to_addr.split(';'):
to_list.append(addr)
try:
server.sendmail(main_msg['From'], tuple(to_list), fullText)
finally:
server.quit()

相关链接:http://hi.baidu.com/terry%5Fsky/blog/item/df3b5fdf85607f154854030f.html
上面的链接从邮件的格式的角度出发分析email的原理,可以加深对发送和接收邮件的理解

相关链接:http://hi.baidu.com/terry_sky/blog/item/22ae71cf10042c3bf9dc6139.html
上面的链接给出了MIMEMultipart和 MIMEImage和MIMEText的带参数的用法,可以在正文里面用html格式带图片,有意思。

相关链接:http://hi.baidu.com/python23/blog/item/a7a918d5cc5e0ec551da4beb.html
http://hi.baidu.com/python23/blog/item/42a5194c87160cf8d72afc1d.html
上面的链接从编解码的角度去看email模块,也挺有意思的,解释得比较透彻

相关链接:http://caokee.com/?p=67



================================

===================================

PYTHON 3.1.1 /* Style Definitions */ table.MsoNormalTable {mso-style-name:"Table Normal"; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-priority:99; mso-style-qformat:yes; mso-style-parent:""; mso-padding-alt:0in 5.4pt 0in 5.4pt; mso-para-margin:0in; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:"Times New Roman","serif"; mso-fareast-font-family:"Times New Roman";} 发HTML格式的,带有中文的邮件。


/* Style Definitions */ table.MsoNormalTable {mso-style-name:"Table Normal"; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-priority:99; mso-style-qformat:yes; mso-style-parent:""; mso-padding-alt:0in 5.4pt 0in 5.4pt; mso-para-margin:0in; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:"Times New Roman","serif"; mso-fareast-font-family:"Times New Roman";}

# -*- encoding:utf-8 -*-

import smtplib

import email.message

import mimetypes

from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from email.mime.image import MIMEImage from email.header import Header

sender = "KesSysReminder@knowles.com"

to = "Eric.Han@knowles.com"

subject = Header('这是一封测试邮件','utf-8')

cc = "Martin.Xu@knowles.com"

htmlText2 = '