Example code for sending emails in python (html, images, and attachments are supported)

Source: Internet
Author: User
Some examples of sending emails using python are as follows:

The code is as follows:


#! /Usr/bin/python
#-*-Coding: UTF-8 -*-

Import email
Import mimetypes
From email. MIMEMultipart import MIMEMultipart
From email. MIMEText import MIMEText
From email. MIMEImage import MIMEImage
Import smtplib

Def sendEmail (authInfo, fromAdd, toAdd, subject, plainText, htmlText ):

StrFrom = fromAdd
StrTo = ','. join (toAdd)

Server = authInfo. get ('server ')
User = authInfo. get ('user ')
Passwd = authInfo. get ('password ')

If not (server and user and passwd ):
Print 'complete login info, exit now'
Return

# Set root information
MsgRoot = MIMEMultipart ('related ')
MsgRoot ['subobject'] = Subject
MsgRoot ['from'] = strFrom
MsgRoot ['to'] = strTo
MsgRoot. preamble = 'This is a multi-part message in MIME format .'

# Encapsulate the plain and HTML versions of the message body in
# 'Alternative 'part, so message agents can decide which they want to display.
MsgAlternative = MIMEMultipart ('alternative ')
MsgRoot. attach (msgAlternative)

# Set plain text information
MsgText = MIMEText (plainText, 'plain ', 'utf-8 ')
MsgAlternative. attach (msgText)

# Set HTML information
MsgText = MIMEText (htmlText, 'html', 'utf-8 ')
MsgAlternative. attach (msgText)

# Set built-in image information
Fp = open('test.jpg ', 'RB ')
MsgImage = MIMEImage (fp. read ())
Fp. close ()
MsgImage. add_header ('content-id ',' ')
MsgRoot. attach (msgImage)

# Send an email
Smtp = smtplib. SMTP ()
# Set the debugging level, depending on the situation
Smtp. set_debuglevel (1)
Smtp. connect (server)
Smtp. login (user, passwd)
Smtp. sendmail (strFrom, strTo, msgRoot. as_string ())
Smtp. quit ()
Return

If _ name _ = '_ main __':
AuthInfo = {}
AuthInfo ['server'] = 'smtp .somehost.com'
AuthInfo ['user'] = 'username'
AuthInfo ['password'] = 'password'
FromAdd = 'username @ somehost.com'
ToAdd = ['someone @ somehost.com ', 'Oss @ somehost.com']
Subject = 'Email topic'
PlainText = 'here is plain text'
HtmlText ='HTML text'
SendEmail (authInfo, fromAdd, toAdd, subject, plainText, htmlText)



FILE-form 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 ()

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 ()

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 ()

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 ()

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 ('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

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 ()

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 ('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 ()

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.