python发送电子邮件
来源:互联网 发布:mac口红怎么辨别真假 编辑:程序博客网 时间:2024/04/29 10:31
# -*- coding: utf-8 -*-
import email
import mimetypes
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from email.MIMEImage import MIMEImage
import smtplib
def sendEmail(authInfo, fromAdd, toAdd, subject, plainText, htmlText):
strFrom = fromAdd
strTo = ', '.join(toAdd)
server = authInfo.get('server')
user = authInfo.get('user')
passwd = authInfo.get('password')
if not (server and user and passwd) :
print 'incomplete login info, exit now'
return
# 设定root信息
msgRoot = MIMEMultipart('related')
msgRoot['Subject'] = subject
msgRoot['From'] = strFrom
msgRoot['To'] = strTo
msgRoot.preamble = 'This is a multi-part message in MIME format.'
# Encapsulate the plain and HTML versions of the message body in an
# 'alternative' part, so message agents can decide which they want to display.
msgAlternative = MIMEMultipart('alternative')
msgRoot.attach(msgAlternative)
#设定纯文本信息
msgText = MIMEText(plainText, 'plain', 'utf-8')
msgAlternative.attach(msgText)
#设定HTML信息
#msgText = MIMEText(htmlText, 'html', 'utf-8')
#msgAlternative.attach(msgText)
#设定内置图片信息
fp = open('1.jpg', 'rb')
msgImage = MIMEImage(fp.read())
fp.close()
msgImage.add_header('Content-ID', '<image1>')
msgRoot.attach(msgImage)
#发送邮件
smtp = smtplib.SMTP()
#设定调试级别,依情况而定
smtp.set_debuglevel(1)
smtp.connect(server)
smtp.login(user, passwd)
smtp.sendmail(strFrom, strTo, msgRoot.as_string())
smtp.quit()
return
if __name__ == '__main__' :
authInfo = {}
authInfo['server'] = 'smtp.163.com'
authInfo['user'] = '******'
authInfo['password'] = '******'
fromAdd = '******@163.com'
toAdd = ['****@163.com','****@126.com']
subject = u'测试邮件'
plainText = '这里是普通文本'
htmlText = "<a href='http://www.baidu.com'>这个世界需要和平!</a>"
sendEmail(authInfo, fromAdd, toAdd, subject, plainText, htmlText)
- 使用python发送电子邮件
- 使用python发送电子邮件
- python发送电子邮件
- python发送电子邮件
- python发送电子邮件范例
- 使用Python发送电子邮件
- 电子邮件发送-python
- Python:发送电子邮件
- 使用PYTHON发送电子邮件
- Python发送电子邮件.
- python实现电子邮件(E-mail)发送
- Python SMTP 发送带附件电子邮件
- Python SMTP 发送带附件电子邮件
- Python次位面——发送电子邮件
- python学习通过smtp发送电子邮件
- Python SMTP 发送带附件电子邮件
- python实现发送电子邮件的功能
- 发送电子邮件
- VC中类信息丢失
- 取文本文件中的任意一行内容
- 我的2010
- linux内核学习(20)设备模型第二层之一
- Win7下安装Oracle10g 问题汇总
- python发送电子邮件
- JS遮照层、拖拽效果
- python判断进程是否存在
- QT中使用windows api
- BlackBerry Tablet OS SDK (操作系统开发工具包) UI指南(二)
- Extjs中ComboBox选中默认值
- PHP时间相减总结
- Linux下服务器心跳集群脚本
- JAVA处理日期(Date)时间(Time)以及相关类的介绍