@rickyChen
2017-01-12T11:37:29.000000Z
字数 913
阅读 3227
Python
# encoding: utf-8
from email.mime.image import MIMEImage
from email.mime.multipart import MiMEMultipart
import stmplib
class SendMail:
def __init__(self, host, user, passwd):
self._user = user
self._account = user.split("@")[0]
self._me = "{}<{}>".format(self._account, self._user)
server = stmplib.SMTP()
server.connect(host)
server.login(self._account, passwd)
self._server = server
def sendImageMail(self, to_list, cc_list, sub, content, pic_dict,
subtype='html'):
# 创建一个带附件的实例
msg = MIMEMultipart('related')
# 添加邮件内容
msg.attach(MIMEText(Content, _subtype=subtype, _charset='utf-8'))
for pic in pic_dict.keys():
image = MIMEImage(open(pic_dict[pic], 'rb').read())
image.add_header('Content-ID', pic)
msg.attach(image)
msg['Subject'] = sub
msg['From'] = self._me
# 收件人与抄送人
msg['To'] = ";".join(to_list)
msg['Cc'] = ";".join(cc_list)
try:
# 抄送人与收件人须在同一个列表中
self._server.sendmail(self._me, to_list + cc_list, msg.as_string())
return True
except Exception, e:
print str(e)
return False