Python sending Email method example-Python tutorial

Source: Internet
Author: User
This article mainly introduces the method of sending emails in Python, which has good practical value and some comments in the code for readers to understand, for more information about how to implement the Python Email sending function, see the following example. 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.

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.