使用python原生的方法实现发送email
2016-07-06 16:36
781 查看
使用python原生的方法实现发送email
import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart from email.mime.base import MIMEBase from email.utils import COMMASPACE from email import encoders import os # 发送账号信息 sender = 'xxxxx@qq.com' password = 'xxxxxxxxxx' # 授权码 ''' html:内容 subject: 主题 receivers:接收者,类型字符串,例子:xx@xx.xx list ''' def send_email(receivers, subject, html): print(html) if not isinstance(receivers, list): receivers = [receivers] message = MIMEMultipart() content_message = MIMEText(html, 'HTML', 'utf-8') message.attach(content_message) message['From'] = sender message['To'] = COMMASPACE.join(receivers) message['Subject'] = subject # for file in files: # part = MIMEBase('application', 'octet-stream') #'octet-stream': binary data # print(open(file, 'r').read()) # part.set_payload(open(file, 'r').read()) # encoders.encode_base64(part) # part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(file)) # message.attach(part) smtpObj = smtplib.SMTP_SSL(host='smtp.qq.com') smtpObj.login(sender, password) result = smtpObj.sendmail(sender, receivers, message.as_string()) smtpObj.quit() # send_mail('用户激活', 'hello', settings.SERVER_EMAIL, [email]) return result # Note: # 内容最好是动态内容,每次不一样,而且不要太频繁 msg = ''' <h1> Hello</h1><a href="http://www.baidu.com">baidu</a> ''' print(send_email(['xxx@xxx.com', 'xxx@qq.com'], '你好', msg))
相关文章推荐
- python 中转义字符的注释
- 皮尔逊相关系数的计算(python代码)
- python 随机数
- Python的时间模块
- 玩弄python的正则表达式
- python 入门笔记
- NumPy学习笔记3-线性代数
- Python数据爬虫,爬链家的二手房信息
- python命令行解析模块optionparser
- Python 安装matplotlib,six,dateutil,pyparsing 完整过程
- 【执行】robot执行命令
- 边做边学Python Flask Web开发(5)-- 使用Jinjia2模板(中)
- Python 和字符编码
- python备份、删除过期压缩文件
- 11个实用但你可能不知道的Python程序库
- Python中yield的用法
- 第一节:安装Python
- d3.js+json-Python2014大会主题旭日图
- Numpy学习笔记2-numpy进阶
- 【语法】telnet库的使用