File-form emails
Link: http://blog.csdn.net/wyuan8913/article/details/6917873
[Python]
View plaincopy
- #! /Usr/bin/ENV python3
- # Coding: UTF-8
- Import smtplib
- From email. Mime. Text import mimetext
- From email. header import Header
- Sender = '***'
- Else ER = '***'
- Subject = 'python email Test'
- Smtpserver = 'smtp .163.com'
- Username = '***'
- Password = '***'
- MSG = mimetext ('hello', 'plain ', 'utf-8') # The parameter 'utf-8' is required for Chinese characters and is not required for single-byte characters.
- MSG ['subobject'] = header (subject, 'utf-8 ')
- SMTP = smtplib. SMTP ()
- SMTP. Connect ('smtp .163.com ')
- SMTP. login (username, password)
- SMTP. Sendmail (sender, explorer, MSG. as_string ())
- SMTP. Quit ()
HTML emails
[Python]
View plaincopy
- #! /Usr/bin/ENV python3
- # Coding: UTF-8
- Import smtplib
- From email. Mime. Text import mimetext
- Sender = '***'
- Else ER = '***'
- Subject = 'python email Test'
- Smtpserver = 'smtp .163.com'
- Username = '***'
- Password = '***'
- MSG = mimetext ('<HTML>
- MSG ['subobject'] = subject
- SMTP = smtplib. SMTP ()
- SMTP. Connect ('smtp .163.com ')
- SMTP. login (username, password)
- SMTP. Sendmail (sender, explorer, MSG. as_string ())
- SMTP. Quit ()
HTML emails with images
[Python]
View plaincopy
- #! /Usr/bin/ENV python3
- # Coding: UTF-8
- Import smtplib
- From email. Mime. multipart import mimemultipart
- From email. Mime. Text import mimetext
- From email. Mime. Image import mimeimage
- Sender = '***'
- Else ER = '***'
- Subject = 'python email Test'
- Smtpserver = 'smtp .163.com'
- Username = '***'
- Password = '***'
- Msgroot = mimemultipart ('related ')
- Msgroot ['subobject'] = 'test message'
- Msgtext = mimetext ('<B> some <I> HTML </I> text </B> and an image. <br> <br> good! ', 'Html', 'utf-8 ')
- Msgroot. Attach (msgtext)
- Fp = open ('H: \ Python \ 1.jpg ', 'rb ')
- Msgimage = mimeimage (FP. Read ())
- FP. Close ()
- Msgimage. add_header ('content-id', '<image1> ')
- Msgroot. Attach (msgimage)
- SMTP = smtplib. SMTP ()
- SMTP. Connect ('smtp .163.com ')
- SMTP. login (username, password)
- SMTP. Sendmail (sender, explorer, msgroot. as_string ())
- SMTP. Quit ()
Emails with attachments
[Python]
View plaincopy
- #! /Usr/bin/ENV python3
- # Coding: UTF-8
- Import smtplib
- From email. Mime. multipart import mimemultipart
- From email. Mime. Text import mimetext
- From email. Mime. Image import mimeimage
- Sender = '***'
- Else ER = '***'
- Subject = 'python email Test'
- Smtpserver = 'smtp .163.com'
- Username = '***'
- Password = '***'
- Msgroot = mimemultipart ('related ')
- Msgroot ['subobject'] = 'test message'
- # Construct an attachment
- ATT = mimetext (open ('H: \ Python \ 1.jpg ', 'rb'). Read (), 'base64', 'utf-8 ')
- ATT ["Content-Type"] = 'application/octet-stream'
- ATT ["content-disposition"] = 'attachment; filename = "1.jpg "'
- Msgroot. Attach (ATT)
- SMTP = smtplib. SMTP ()
- SMTP. Connect ('smtp .163.com ')
- SMTP. login (username, password)
- SMTP. Sendmail (sender, explorer, msgroot. as_string ())
- SMTP. Quit ()
Group email
[Python]
View plaincopy
- #! /Usr/bin/ENV python3
- # Coding: UTF-8
- Import smtplib
- From email. Mime. Text import mimetext
- Sender = '***'
- Else ER = ['***', '*****',…]
- Subject = 'python email Test'
- Smtpserver = 'smtp .163.com'
- Username = '***'
- Password = '***'
- MSG = mimetext ('hello', 'plain ', 'utf-8 ')
- MSG ['subobject'] = subject
- SMTP = smtplib. SMTP ()
- SMTP. Connect ('smtp .163.com ')
- SMTP. login (username, password)
- SMTP. Sendmail (sender, explorer, MSG. as_string ())
- SMTP. Quit ()
Emails containing various elements
[Python]
View plaincopy
- #! /Usr/bin/ENV python3
- # Coding: UTF-8
- Import smtplib
- From email. Mime. multipart import mimemultipart
- From email. Mime. Text import mimetext
- From email. Mime. Image import mimeimage
- Sender = '***'
- Else ER = '***'
- Subject = 'python email Test'
- Smtpserver = 'smtp .163.com'
- Username = '***'
- Password = '***'
- # Create message container-the correct MIME type is multipart/alternative.
- MSG = mimemultipart ('alternative ')
- MSG ['subobject'] = "Link"
- # Create the body of the message (a plain-text and an HTML Version ).
- TEXT = "Hi! \ Nhow are you? \ Nhere is the link you wanted: \ nhttp: // www.python.org"
- Html = """\
- <HTML>
- <Head>
- <Body>
- <P> Hi! <Br>
- How are you? <Br>
- Here is the <a href = "http://www.python.org"> link </a> you wanted.
- </P>
- </Body>
- </Html>
- """
- # Record the MIME types of both parts-text/plain and text/html.
- Part1 = mimetext (text, 'plain ')
- Part2 = mimetext (HTML, 'html ')
- # Attach parts into message container.
- # According to RFC 2046, the last part of a multipart message, in this case
- # The HTML message, is best and preferred.
- MSG. Attach (Part1)
- MSG. Attach (Part2)
- # Construct an attachment
- ATT = mimetext (open ('H: \ Python \ 1.jpg ', 'rb'). Read (), 'base64', 'utf-8 ')
- ATT ["Content-Type"] = 'application/octet-stream'
- ATT ["content-disposition"] = 'attachment; filename = "1.jpg "'
- MSG. Attach (ATT)
- SMTP = smtplib. SMTP ()
- SMTP. Connect ('smtp .163.com ')
- SMTP. login (username, password)
- SMTP. Sendmail (sender, explorer, MSG. as_string ())
- SMTP. Quit ()
SSL-based Mail
[Python]
View plaincopy
- #! /Usr/bin/ENV python3
- # Coding: UTF-8
- Import smtplib
- From email. Mime. Text import mimetext
- From email. header import Header
- Sender = '***'
- Else ER = '***'
- Subject = 'python email Test'
- Smtpserver = 'smtp .163.com'
- Username = '***'
- Password = '***'
- MSG = mimetext ('hello', 'plain ', 'utf-8') # The parameter 'utf-8' is required for Chinese characters and is not required for single-byte characters.
- MSG ['subobject'] = header (subject, 'utf-8 ')
- SMTP = smtplib. SMTP ()
- SMTP. Connect ('smtp .163.com ')
- SMTP. EHLO ()
- SMTP. starttls ()
- SMTP. EHLO ()
- SMTP. set_debuglevel (1)
- SMTP. login (username, password)
- SMTP. Sendmail (sender, explorer, MSG. as_string ())
- SMTP. Quit ()