[python]发送邮件(可带附件+转中文)

支持中文邮件标题和中文邮件内容。

支持多附件。

根据用户名推测邮件服务器提供商。


[python]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. def sendmail(login={},mail={}):  
  2.     '''''\ 
  3.     @param login login['user'] login['passwd'] 
  4.     @param mail mail['to_addr'] mail['subject'] mail['content'] mail['attach'] 
  5.     '''  
  6.     from datetime import datetime  
  7.     from base64 import b64encode  
  8.     import smtplib, mimetypes  
  9.     from email.mime.text import MIMEText  
  10.     from email.mime.multipart import MIMEMultipart  
  11.     from email.mime.image import MIMEImage  
  12.   
  13.     user_info = login['user'].split('@')  
  14.     mail_configure = {}  
  15.     mail_configure['mail_encoding'] = 'utf-8'  
  16.     mail_configure['mail_supplier'] = user_info[1]  
  17.     mail_configure['from_addr'] = login['user']  
  18.     mail_configure['server_host'] = 'smtp.%s' % mail_configure['mail_supplier']  
  19.     error = None  
  20.   
  21.     try:  
  22.         email = MIMEMultipart()  
  23.         email['from'] = mail_configure['from_addr']  
  24.         email['to'] = mail['to_addr']  
  25.         email['subject'] = '=?%s?B?%s?=' % (mail_configure['mail_encoding'],b64encode(mail['subject']))  
  26.         email_content = MIMEText(mail['content'], _charset=mail_configure['mail_encoding'])  
  27.         email.attach(email_content)  
  28.   
  29.         if 'attach' in mail:  
  30.             for i in mail['attach']:  
  31.                 ctype, encoding = mimetypes.guess_type(i)  
  32.                 if ctype is None or not encoding is None:  
  33.                     ctype = 'application/octet-stream'  
  34.                 maintype, subtype = ctype.split('/'1)  
  35.                 att = MIMEImage((lambda f: (f.read(), f.close()))(open(i, 'rb'))[0], _subtype = subtype)  
  36.                 att.add_header('Content-Disposition''attachment', filename = i)  
  37.                 email.attach(att)  
  38.   
  39.         smtp = smtplib.SMTP()  
  40.         smtp.connect(mail_configure['server_host'])  
  41.         smtp.login(user_info[0], login['passwd'])  
  42.         smtp.sendmail(mail_configure['from_addr'], mail['to_addr'], email.as_string())  
  43.         smtp.quit()  
  44.     except Exception as e:  
  45.         error = e  
  46.   
  47.     return (mail_configure['from_addr'], mail['to_addr'], error)  

[python]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. def t21():  
  2.     login = {  
  3.         'user':'ak43@sina.com',  
  4.         'passwd':'hellod@'  
  5.     }  
  6.     mail = {  
  7.         'to_addr':'ak32@sina.com;ak32@21cn.com',  
  8.         'subject':'不带附件的测试邮件',  
  9.         'content':'''''\ 
  10.         sz002718,友邦吊顶 
  11.         sz002719,麦趣尔 
  12.         sz002722,金轮股份 
  13.         ''',          
  14.     }  
  15.     print sendmail(login, mail)  
  16.       
  17.     login = {  
  18.         'user':'hellot@sina.com',  
  19.         'passwd':'hello#world'  
  20.     }  
  21.     mail = {  
  22.         'to_addr':'tom12@sina.com;tom12@21cn.com',  
  23.         'subject':'带附件的测试邮件',  
  24.         'content':'''''\ 
  25.         sz002718,友邦吊顶 
  26.         sz002719,麦趣尔 
  27.         sz002722,金轮股份 
  28.         ''',  
  29.         'attach':['e:/a/a.txt']  
  30.     }  
  31.     print sendmail(login, mail)  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值