This paper shows the implementation method of the Python sending email function with the example form, has the good practical value skill, and the function is more perfect. The implementation methods are as follows:
The main functional code is as follows:
#/usr/bin/env python #-*-encoding=utf-8-*-import base64 import smtplib from Email.mime.multipart import Mimemultipar T from Email.mime.text import Mimetext class 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 '
self.__charset= ' gb2312 ' self.username = username Self.password = password self.fromalias= ' fromuser ' #发件人别名 Self.from2= ' def close (self): Try:self.__smtp.quit () except Exception, E:pass def SE Tfromalias (Self,alias): 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,co
ntent): Self.__content=content def setfrom (self,address): self.__from=address
def addto (self,address): Self.__to.append (address) def setcharset (self,charset): Self.__charset=char Set def send (self): Try:self.__smtp.set_debuglevel (1) #login if necessary if self.use Rname and Self.password:self.__smtp.login (self.username,self.password) msgroot = Mimemultipart (' re
lated ') msgroot[' Subject '] = Self.__subject aliasb6=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) Msgro Ot[' to '] = ";".
Join (self.__to) msgalternative = Mimemultipart (' alternative ') Msgroot.attach (msgalternative) Msgtext = Mimetext (self.__content, Self.__style,self.__charset) Msgalternative.attach (msgText) self.__smtp . SendMail (Self.__from,self.__to,msgroot.as_string ()) return True except Exception,e:print "Error", E return False def clearrecipient (self): self.__to = [] #给单个人发送邮件 def sendhtml (self,address,title,content): Self.setstyle (' HTML ') self.setfrom ("<%s>"%self.user Name if not isinstance (CONTENT,STR): content = Content.encode (' gb18030 ') self.addto (address) Self.setsu Bject (title) self.setcontent (content) ret = Self.send () self.close () return ret #群发邮件 def Sendmoreh Tml (self,addresslist,title,content): Self.setstyle (' HTML ') self.setfrom ("<%s>"%self.username) if not ISI
Nstance (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 ret #测试 def mai N (): Send=ccsendmail () send.sendhtml (' touser@xxx.com ', u ' mail title ', u ' mail content ') #send. sendmorehtml ([Touser1@xx.com,
Touser2@xx.com],u ' mail title ', u ' mail content ')
If __name__== ' __main__ ': Main ()
I hope the examples described in this article will help you with your Python programming.