Python Email sending Method Instance and pythonemail instance
This article demonstrates the implementation of the Python Email sending function in the form of examples. It has good practical and useful skills and has relatively complete functions. The specific 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' self. _ charset = 'gb 2312 'self. username = username self. password = password self. fromAlias = 'fromuser' # sender alias self. from2 = ''def close (self): try: self. _ smtp. quit () doesn t Exception, e: pass def setFromAlias (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, content): self. _ _ Content = content def setFrom (self, address): self. _ from = address def addTo (self, address): self. _. append (address) def setCharset (self, charset): self. _ charset = charset def send (self): try: self. _ smtp. set_debuglevel (1) # login if necessary if self. username and self. password: self. _ smtp. login (self. username, self. password) msgRoot = MIMEMultipart ('related') msgRoot ['subobject'] = 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) msgRoot ['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 t 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. 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 ret # group email 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 ret # test def main (): 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 Python programming.
How does python send emails?
To send an email to python, you can use the python email library.
Here is a python Method for sending emails by calling system commands.
First, you need to call the system command, load the python OS library, and then OS. system () can execute the shell command.
For example
Import OS
OS. system ("mkdir newdoc ")
You can call "mail" to send normal mails, and call "sendmail" to send multimedia files. For more information, see related documents.
Email sending in python
Python sends an email to the QQ mail server. If there is a problem, modify the smtplib. py file.
Specific methods can be found on the Internet.