This article mainly introduces seven python mail content sending methods. For more information, see
I. file-based emails
The 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
The 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 ('
Hi!
', '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
The 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 ('SomeHTMLTextAnd 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
The 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
The 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
The 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 link 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
The 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 ()