I. file-based Emails
Copy codeThe Code is as follows :#! /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 (' ', 'text', '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 ()
Ii. HTML emails
Copy codeThe Code is as follows:
#! /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 ('</pre>
<H1> Hello <Pre> ', 'html', '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 ()
3. HTML emails with images
Copy codeThe Code is as follows:
#! /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.
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 ','')
MsgRoot. attach (msgImage)
Smtp = smtplib. SMTP ()
Smtp. connect ('smtp .163.com ')
Smtp. login (username, password)
Smtp. sendmail (sender, explorer, msgRoot. as_string ())
Smtp. quit ()
4. emails with attachments
Copy codeThe Code is as follows :#! /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 ()
5. group email
Copy codeThe Code is as follows :#! /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 (' ', 'text', '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 ()
6. emails containing various elements
Copy codeThe Code is as follows :#! /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 = """\
Hi!
How are you?
Here is the <a href = "http://www.python.org"> link </a> you wanted.
"""
# 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 ()
7. SSL-based Mail
Copy codeThe Code is as follows :#! /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 (' ', 'text', '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 ()