您的位置:首页 > 编程语言 > Python开发

Python发邮件代码

2013-04-17 16:05 190 查看
class BaseMail:
def __init__(self,smtp,bSmtpAuth, sender, pwd):
self.smtp = smtp;
self.bSmtpAuth = bSmtpAuth;
self.sender = sender;
self.pwd = pwd;
def _parserSend(self, sSubject, sContent, lsPlugin):
return sSubject, sContent, lsPlugin;
def send ( self, sSubject, sContent, lsTo, lsCc = [], lsPlugin = [] ):
mit = MIMEMultipart();
mit['from'] = self.sender;
mit['to'] = ','.join( lsTo );
if lsCc:
mit['cc'] = ','.join( lsCc );
codeSubject, codeContent, codePlugin = self._parserSend(sSubject, sContent, lsPlugin);
mit.attach( MIMEText( codeContent, 'html', 'utf-8' ) );
mit['subject'] = codeSubject;
for plugin in codePlugin:
mitFile = MIMEApplication( plugin['content'], );
mitFile.add_header( 'content-disposition', 'attachment', filename=plugin['subject'] );
mit.attach( mitFile );
server = smtplib.SMTP( self.smtp );
if self.bSmtpAuth:
server.docmd( "EHLO server" );
server.ehlo();
server.esmtp_features['auth'] = 'LOGIN DIGEST-MD5 PLAIN';
#server.starttls();
server.login( self.sender, self.pwd );
server.sendmail( self.sender, lsTo , mit.as_string() );
server.close();

class VanclMail(BaseMail):
def __init__( self, sender, pwd ):
BaseMail.__init__( self, 'SMTP', True, sender, pwd );
self.__strcode = 'utf-8';
def _parserSend(self, sSubject, sContent, lsPlugin):
for i in lsPlugin:
i['subject'] = i['subject'].encode(self.__strcode);
return sSubject.encode(self.__strcode), sContent.encode(self.__strcode), lsPlugin;

class NetEase(BaseMail):
def __init__( self, sender, pwd ):
BaseMail.__init__( self, 'smtp.163.com', True, sender, pwd );
self.__strcode = 'utf-8';
def _parserSend(self, sSubject, sContent, lsPlugin):
'''for i in lsPlugin:
i['subject'] = i['subject'].encode(self.__strcode);'''
return sSubject.encode(self.__strcode), sContent.encode(self.__strcode), lsPlugin;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: