# -*- coding: utf-8 -*- # ================================================= # # 描述: KEmail 发送邮件类 # # 作者: hechangmin # # 时间: 2008-05-30 # # ================================================= import email import mimetypes from email.MIMEMultipart import MIMEMultipart from email.MIMEText import MIMEText from email.MIMEImage import MIMEImage import smtplib from logger import klog def sendmail(paramMap): smtp = smtplib.SMTP() msgRoot = MIMEMultipart( ' related ' ) msgAlternative = MIMEMultipart( ' alternative ' ) if paramMap.has_key( " server " ) and paramMap.has_key( " user " ) and paramMap.has_key( " password " ): try : smtp.set_debuglevel( 1 ) smtp.connect(paramMap[ " server " ]) smtp.login(paramMap[ " user " ], paramMap[ " password " ]) except : klog.error( " smtp login exception! " ) return False else : klog.error( " Parameters incomplete! " ) return False # 测试发现 邮件其头部信息 比如标题 发送地址 还有抄送之类 和stmp发送的具体信息脱离。 if (paramMap.has_key( " subject " ) and paramMap.has_key( " from " ) and paramMap.has_key( " to " )) == False: klog.error( " Parameters incomplete! " ) return False msgRoot[ ' subject ' ] = paramMap[ " subject " ] msgRoot[ ' from ' ] = paramMap[ " from " ] if paramMap.has_key( " cc " ): msgRoot[ ' cc ' ] = paramMap[ " cc " ] msgRoot[ ' to ' ] = paramMap[ " to " ] msgRoot.preamble = ' This is a multi-part message in MIME format. ' msgRoot.attach(msgAlternative) TempAddTo = paramMap[ " to " ] if paramMap.has_key( " text " ): msgText = MIMEText(paramMap[ " text " ] , ' plain ' , ' utf-8 ' ) msgAlternative.attach(msgText) if paramMap.has_key( " html " ): msgText = MIMEText(paramMap[ " html " ], ' html ' , ' utf-8 ' ) msgAlternative.attach(msgText) if paramMap.has_key( " image " ): fp = open(paramMap[ " image " ], ' rb ' ) msgImage = MIMEImage(fp.read()) fp.close() msgImage.add_header( ' Content-ID ' , ' <image1> ' ) msgRoot.attach(msgImage) if paramMap.has_key( " cc " ): TempAddTo = paramMap[ " to " ] + " , " + paramMap[ " cc " ] if TempAddTo.find( " , " ) != - 1 : FinallyAdd = TempAddTo.split( " , " ) else : FinallyAdd = TempAddTo # stmp 发送邮件. 发现群发地址用‘,’相隔的字符串,只有一个成功,所以改用列表。 smtp.sendmail(paramMap[ " from " ], FinallyAdd, msgRoot.as_string()) smtp.quit() return True # ===Test ========================================= if __name__ == ' __main__ ' : from mailconf import mailmap sendmail(mailmap)
发信信息是可以乱写的。而真正和stmp有密切关系的是 :smtp.sendmail(paramMap["from"], FinallyAdd, msgRoot.as_string())
也就是上面的什么发信人地址 ,收信人地址 ,抄送人地址都可以是假的。
您可能感兴趣的文章:
分享:python发邮件的综合实例
python邮件发送模块smtplib的实例详解
python smtplib发送邮件的例子 python使用126邮箱发送邮件
python smtplib模块发邮件(带附件)的例子
Python发送带附件的邮件的实现代码
python 发送邮件乱码的解决方法
python从文件读取邮件地址输出的例子
python使用gmail发送邮件的实例代码
python smtplib模块发送邮件的实例详解
python smtp模块发送邮件的代码
python发送邮件的脚本一例
python结合php解决发送邮件乱码的问题
python发送邮件的例子
python发送邮件的实例代码