This article shows an example of how Python sends the email function, has a good practical value of skills, and the function is more perfect. The implementation method is as follows:
The main function code is as follows:
#/usr/bin/env python#-*-encoding=utf-8-*-import base64import smtplibfrom email.mime.multipart Import Mimemultipartfrom email.mime.text Import mimetextclass ccsendmail:def __init__ (self,host= "your.mailhost.com", Username= ' fromuser@xxx.com ', password= ' passwd '): Self.__smtp=smtplib. SMTP (host) Self.__subject=none Self.__content=none self.__from=none self.__to=[] self.__style= ' HTML ' sel f.__charset= ' gb2312 ' self.username = username Self.password = password self.fromalias= ' fromuser ' #发件人别名 self.f Rom2= ' def close (self): Try:self.__smtp.quit () except Exception, E:pass def setfromalias (self,a Lias): Self.fromalias=alias def setStyle (self,style): Self.__style = Style def setFrom2 (self,from2): Self.from2 =from2 def setsubject (self,subject): Self.__subject=subject def setcontent (self,content): Self.__content=c Ontent def setfrom (self,address): Self.__from=address def addto (self,address): self.__to. Append (address) def setcharset (Self,charset): Self.__charset=charset def send (self): TRY:SELF.__SMT P.set_debuglevel (1) #login if necessary if Self.username and self.password:self.__smtp.login (self. Username,self.password) Msgroot = Mimemultipart (' related ') msgroot[' Subject '] = self.__subject Alia Sb6=base64.encodestring (Self.fromAlias.encode (self.__charset)) If Len (self.from2) ==0:msgroot[' from '] = "=?%s?" b?%s?=%s "% (Self.__charset,aliasb6.strip (), Self.__from) else:msgroot[' from '] ="%s "% (SELF.FROM2) msgroot[ ' to '] = ";". Join (self.__to) msgalternative = Mimemultipart (' alternative ') Msgroot.attach (msgalternative) MS Gtext = Mimetext (self.__content, Self.__style,self.__charset) Msgalternative.attach (msgText) Self.__smtp.sendmai L (self.__from,self.__to,msgroot.as_string ()) return True except Exception,e:print "Error", E return Fal Se defClearrecipient (self): self.__to = [] #给单个人发送邮件 def sendhtml (self,address,title,content): Self.setstyle (' HTML ') Self.setfrom ("<%s>"%self.username) if not isinstance (CONTENT,STR): content = Content.encode (' GB18030 ') Self.addto (address) Self.setsubject (title) self.setcontent (content) ret = Self.send () self.close () return R ET #群发邮件 def sendmorehtml (self,addresslist,title,content): Self.setstyle (' HTML ') self.setfrom ("<%s>"%self. username) if not isinstance (CONTENT,STR): content = Content.encode (' GB18030 ') for address in AddressList: Self.addto (address) Self.setsubject (title) self.setcontent (content) ret = Self.send () self.close () return R et# Test def main (): Send=ccsendmail () send.sendhtml (' touser@xxx.com ', u ' message header ', U ' message content ') #send. sendmorehtml ([ Touser1@xx.com,touser2@xx.com],u ' message header ', U ' message content ') if __name__== ' __main__ ': Main ()
It is hoped that the examples described in this paper will help you with Python programming.