Python sorting 17 -- sending emails

Source: Internet
Author: User

Automatic Mail sending should be a common requirement. Python provides corresponding module support. Let's look at the code for automatic mail sending:

 

  1. # Coding = UTF-8
  2. Import smtplib
  3. From email. Mime. Text import mimetext
  4. From email. Mime. multipart import mimemultipart
  5. From email. Mime. Application import mimeapplication
  6. Class basemail:
  7. Def _ init _ (self, SMTP, bsmtpauth, sender, PWD ):
  8. Self. bsmtpauth = bsmtpauth
  9. Self. SMTP = SMTP
  10. Self. Sender = sender
  11. Self. Pwd = pwd
  12. Def _ parsersend (self, ssubject, scontent, lsplugin ):
  13. Return ssubject, scontent, lsplugin
  14. Def send (self, ssubject, scontent, lsto, LSCC = [], lsplugin = []):
  15. MIT = mimemultipart ()
  16. MIT ['from'] = self. Sender
  17. MIT ['to'] = ','. Join (lsto)
  18. If LSCC: MIT ['cc'] = ','. Join (LSCC)
  19. Codesubject, codecontent, codeplugin = self. _ parsersend (ssubject, scontent, lsplugin)
  20. MIT. Attach (mimetext (codecontent, 'html', 'utf-8 '))
  21. MIT ['subobject'] = codesubject
  22. For Plugin in codeplugin:
  23. Mitfile = mimeapplication (plugin ['content'],)
  24. Mitfile. add_header ('content-disposition', 'attachment', filename = plugin ['subobject'])
  25. MIT. Attach (mitfile)
  26. Server = smtplib. SMTP (self. SMTP)
  27. # Server. set_debuglevel (smtplib. SMTP. debuglevel)
  28. If self. bsmtpauth: server.doc MD ("ehlo server ")
  29. Server. starttls ()
  30. Server. login (self. Sender, self. pwd)
  31. Server. Sendmail (self. Sender, lsto, MIT. as_string ())
  32. Server. Close ()
  33. Class Gmail (basemail ):
  34. Def _ init _ (self, sender, PWD ):
  35. Basemail. _ init _ (self, 'smtp .gmail.com ', true, sender, PWD)
  36. Self. _ strcode = 'utf-8'
  37. Def _ parsersend (self, ssubject, scontent, lsplugin ):
  38. For I in lsplugin:
  39. I ['subobject'] = I ['subobject']. encode (self. _ strcode)
  40. Return ssubject. encode (self. _ strcode), scontent. encode (self. _ strcode), lsplugin
  41. Class com63mail (basemail ):
  42. Def _ init _ (self, sender, PWD ):
  43. Basemail. _ init _ (self, 'smtp .163.com ', false, sender, PWD)
  44. Self. _ strcode = 'utf-8'
  45. Def _ parsersend (self, ssubject, scontent, lsplugin ):
  46. For I in lsplugin:
  47. I ['subobject'] = I ['subobject']. encode ('gbk ')
  48. Return ssubject, scontent. encode (self. _ strcode), lsplugin
  49. If _ name __= = "_ main __":
  50. Ssubject = u'python3000 mail sending Test'
  51. Scontent = U' <font color = "# ff0066"> popular comments </color>'
  52. Lsplugin = [{'subobject': u' 1abc.txt ', 'content': u'content 1abc'}, {'subobject': u' 2abc.txt', 'content ': u'content 2abc'}]
  53. Gmail = Gmail ('x @ gmail.com ', 'Password ')
  54. Lsto = ['xxx @ gmail.com ']
  55. LSCC = []
  56. Gmail. Send (ssubject, scontent, lsto, LSCC, lsplugin)
  57. Print 'gmail send'
  58. Ssubject = u'python3000 mail sending Test'
  59. Scontent = U' <font color = "# ff0066"> popular comments </color>'
  60. Lsplugin = [{'subobject': u' 1abc.txt ', 'content': u'content 1abc'}, {'subobject': u' 2abc.txt', 'content ': u'content 2abc'}]
  61. Com163 = com63mail ('x @ 163.com ', 'Password ')
  62. Lsto = ['xxx @ 163.com ']
  63. LSCC = []
  64. Com163.send (ssubject, scontent, lsto, LSCC, lsplugin)
  65. Print 'com163 send'

Because the Information encoding and Verification provided by different email service providers are not the same, the code for sending emails is slightly different.

 

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.