Automatic Mail sending should be a common requirement. Python provides corresponding module support. Let's look at the code for automatic mail sending:
- # Coding = UTF-8
- Import smtplib
- From email. Mime. Text import mimetext
- From email. Mime. multipart import mimemultipart
- From email. Mime. Application import mimeapplication
- Class basemail:
- Def _ init _ (self, SMTP, bsmtpauth, sender, PWD ):
- Self. bsmtpauth = bsmtpauth
- Self. SMTP = SMTP
- Self. Sender = sender
- Self. Pwd = pwd
- Def _ parsersend (self, ssubject, scontent, lsplugin ):
- Return ssubject, scontent, lsplugin
- Def send (self, ssubject, scontent, lsto, LSCC = [], lsplugin = []):
- MIT = mimemultipart ()
- MIT ['from'] = self. Sender
- MIT ['to'] = ','. Join (lsto)
- If LSCC: MIT ['cc'] = ','. Join (LSCC)
- Codesubject, codecontent, codeplugin = self. _ parsersend (ssubject, scontent, lsplugin)
- MIT. Attach (mimetext (codecontent, 'html', 'utf-8 '))
- MIT ['subobject'] = codesubject
- For Plugin in codeplugin:
- Mitfile = mimeapplication (plugin ['content'],)
- Mitfile. add_header ('content-disposition', 'attachment', filename = plugin ['subobject'])
- MIT. Attach (mitfile)
- Server = smtplib. SMTP (self. SMTP)
- # Server. set_debuglevel (smtplib. SMTP. debuglevel)
- If self. bsmtpauth: server.doc MD ("ehlo server ")
- Server. starttls ()
- Server. login (self. Sender, self. pwd)
- Server. Sendmail (self. Sender, lsto, MIT. as_string ())
- Server. Close ()
- Class Gmail (basemail ):
- Def _ init _ (self, sender, PWD ):
- Basemail. _ init _ (self, 'smtp .gmail.com ', true, sender, PWD)
- Self. _ strcode = 'utf-8'
- Def _ parsersend (self, ssubject, scontent, lsplugin ):
- For I in lsplugin:
- I ['subobject'] = I ['subobject']. encode (self. _ strcode)
- Return ssubject. encode (self. _ strcode), scontent. encode (self. _ strcode), lsplugin
- Class com63mail (basemail ):
- Def _ init _ (self, sender, PWD ):
- Basemail. _ init _ (self, 'smtp .163.com ', false, sender, PWD)
- Self. _ strcode = 'utf-8'
- Def _ parsersend (self, ssubject, scontent, lsplugin ):
- For I in lsplugin:
- I ['subobject'] = I ['subobject']. encode ('gbk ')
- Return ssubject, scontent. encode (self. _ strcode), lsplugin
- If _ name __= = "_ main __":
- Ssubject = u'python3000 mail sending Test'
- Scontent = U' <font color = "# ff0066"> popular comments </color>'
- Lsplugin = [{'subobject': u' 1abc.txt ', 'content': u'content 1abc'}, {'subobject': u' 2abc.txt', 'content ': u'content 2abc'}]
- Gmail = Gmail ('x @ gmail.com ', 'Password ')
- Lsto = ['xxx @ gmail.com ']
- LSCC = []
- Gmail. Send (ssubject, scontent, lsto, LSCC, lsplugin)
- Print 'gmail send'
- Ssubject = u'python3000 mail sending Test'
- Scontent = U' <font color = "# ff0066"> popular comments </color>'
- Lsplugin = [{'subobject': u' 1abc.txt ', 'content': u'content 1abc'}, {'subobject': u' 2abc.txt', 'content ': u'content 2abc'}]
- Com163 = com63mail ('x @ 163.com ', 'Password ')
- Lsto = ['xxx @ 163.com ']
- LSCC = []
- Com163.send (ssubject, scontent, lsto, LSCC, lsplugin)
- 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.