Python sending an instance of an email method

Source: Internet
Author: User
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.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.