免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2809 | 回复: 0
打印 上一主题 下一主题

关于用python来发email,用smtplib [复制链接]

论坛徽章:
0
1 [报告]
发表于 2006-05-16 09:14 |显示全部楼层
给你参考一下,这个可以发送附件。

其中 MIMEText 类型可以解决你的问题。


[code]
msg=MIMEMultipart()
msg['From'] = '''pppp@sina.com.cn'''
msg['To'] = '''happy@gmail.com'''
msg['Subject'] =  '''corp backup files: ''' + target_bakfile
msg['Reply-To'] = '''pppp@sina.com.cn'''
msg['Date'] = time.ctime(time.time())
msg['X-Priority'] =  '''3'''
msg['X-MSMail-Priority'] =  '''Normal'''
msg['X-Mailer'] =  '''Microsoft Outlook Express 6.00.2900.2180'''
msg['X-MimeOLE'] =  '''Produced By Microsoft MimeOLE V6.00.2900.2180'''
body=MIMEText(target,_subtype='plain',_charset='utf-8')
msg.attach(body)

fp = open(target,'rb')
ctype,encoding = mimetypes.guess_type(target)
if ctype is None or encoding is not None:
   ctype = 'application/octet-stream'
maintype,subtype = ctype.split('/',1)
m = MIMEBase(maintype,subtype)
m.set_payload(fp.read())
fp.close()
Encoders.encode_base64(m)                                             
m.add_header('Content-disposition','attachment; filename="%s"' % os.path.basename(target))
msg.attach(m)

smtpServer = smtplib.SMTP('smtp.sina.com.cn')
smtpServer.login('username','pass')
smtpServer.sendmail(msg['From'],msg['To'],msg.as_string())
smtpServer.close()
[/code]
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP