python 发送邮件
来源:互联网 发布:网络刷单兼职可信吗 编辑:程序博客网 时间:2024/04/27 18:47
前阵子改了下邮件系统一些BUG,顺便了解下python的邮件模块
#coding=utf8import smtplibimport mimetypesfrom email.header import Headerfrom email.mime.text import MIMETextfrom email.mime.multipart import MIMEMultipartfrom email.mime.image import MIMEImagedef SendMail(): msg = MIMEMultipart() '''不使用Header转换邮件头部会乱码''' msg['From'] = ("%s<%s>") % (Header('管理员','utf-8'),'mailfrom@163.com') To = ["123456788@qq.com","123456789@qq.com"] msg['To'] = ','.join(To) msg['Subject'] = Header('附件邮件','utf-8') #text: 文本邮件会与html邮件冲突,导致覆盖(二选一) txt = MIMEText("啦啦啦德玛西亚",'plain','utf-8') msg.attach(txt) #html + 图片 html邮件会与文本邮件冲突(二选一) '''html 也可单独发送html''' content = '''<!DOCTYPE html><html><font size="5" color="red">红色的</font> <p><font size="3" color="red">This is test Email ~_~!</font></p> <img src="cid:image1"></html>''' html = MIMEText(content, 'html', 'utf-8') msg.attach(html) '''也可以不嵌入到上面的html中,单独以附件的方式发送图片''' file1 = "2407531.jpg" with open(file1,'rb') as f: content = f.read() image = MIMEImage(content) image.add_header('Content-ID','<image1>') msg.attach(image) #附件 file2 = 'APScheduler-2.0.3.tar.gz' with open(file2,'rb') as f: content = f.read() Attachments = MIMEText(content, 'base64', 'utf-8') Attachments["Content-Type"] = 'application/octet-stream' Attachments["Content-Disposition"] = 'attachment; filename="APScheduler-2.0.3.tar.gz"' msg.attach(Attachments) server = smtplib.SMTP()#也可以在上一辈初始化的时候连接 server.connect('smtp.163.com') server.login('xxx@163.com','password')#msg['From']:可以为发件邮箱,也可为发件邮箱+别名 #To: 可以为字符串或列表,为字符串时默认为单个邮箱,群发时为列表 server.sendmail(msg['From'],To,msg.as_string()) print msg.as_string() server.quit()if __name__ == "__main__": SendMail()
0 0
- 用Python发送邮件
- Python 发送邮件
- python 邮件发送
- 使用PYTHON发送邮件
- python邮件发送
- python发送邮件程序
- python: 发送邮件
- python发送邮件
- python smtp发送邮件
- python 发送邮件实例
- python发送各类邮件
- 用Python发送邮件
- 用Python发送邮件
- python 发送邮件实例
- Python发送邮件测试
- python发送邮件
- python发送邮件
- python发送邮件
- Linux学习笔记-----挂载光盘
- 玩转Bootstrap(基础) -- (7.其他内置组件)
- ligerUI实现grid同一列根据不同的值编辑时呈现不同类型的编辑器
- 安卓开发小网站
- centos6.5下安装mysql 5.6
- python 发送邮件
- Cocoapods 安装与使用
- JAVA array,map 转 json 字符串
- JVM内存管理:深入Java内存区域与OOM
- 使用SVG中的Symbol元素制作Icon(链接地址)
- tomcat文件夹与文件解析
- 数据库查询不同关键字的执行顺序
- HDOJ1253 胜利大逃亡 BFS
- 环境变量