Python seven ways to send a message content instance

Source: Internet
Author: User
I. Mail in the form of a file
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 = ' * * * '
Receiver = ' * * * '
Subject = ' Python email test '
SmtpServer = ' smtp.163.com '
Username = ' * * * '
Password = ' * * * '

msg = mimetext (' Hello ', ' text ', ' utf-8 ') #中文需参数 ' Utf-8 ', single-byte characters do not need
msg[' Subject ' = Header (Subject, ' utf-8 ')

SMTP = Smtplib. SMTP ()
Smtp.connect (' smtp.163.com ')
Smtp.login (username, password)
Smtp.sendmail (sender, receiver, msg.as_string ())
Smtp.quit ()

Ii. e-mail in HTML form
Copy the Code code as follows:


#!/usr/bin/env Python3
#coding: Utf-8
Import Smtplib
From Email.mime.text import Mimetext

Sender = ' * * * '
Receiver = ' * * * '
Subject = ' Python email test '
SmtpServer = ' smtp.163.com '
Username = ' * * * '
Password = ' * * * '

msg = Mimetext ('

How are you doing


', ' HTML ', ' Utf-8 ')

msg[' Subject '] = Subject

SMTP = Smtplib. SMTP ()
Smtp.connect (' smtp.163.com ')
Smtp.login (username, password)
Smtp.sendmail (sender, receiver, msg.as_string ())
Smtp.quit ()

Three, HTML messages with pictures
Copy the Code code 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 = ' * * * '
Receiver = ' * * * '
Subject = ' Python email test '
SmtpServer = ' smtp.163.com '
Username = ' * * * '
Password = ' * * * '

Msgroot = Mimemultipart (' related ')
msgroot[' Subject ' = ' test message '

Msgtext = Mimetext ('Some HTML textand 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, receiver, msgroot.as_string ())
Smtp.quit ()

Iv. e-mail with attachments
Copy the Code code 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 = ' * * * '
Receiver = ' * * * '
Subject = ' Python email test '
SmtpServer = ' smtp.163.com '
Username = ' * * * '
Password = ' * * * '

Msgroot = Mimemultipart (' related ')
msgroot[' Subject ' = ' test message '

#构造附件
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, receiver, msgroot.as_string ())
Smtp.quit ()

Five, Group Mail
Copy the Code code as follows:

#!/usr/bin/env Python3
#coding: Utf-8
Import Smtplib
From Email.mime.text import Mimetext

Sender = ' * * * '
Receiver = [' * * * ', ' * * * ',......]
Subject = ' Python email test '
SmtpServer = ' smtp.163.com '
Username = ' * * * '
Password = ' * * * '

msg = mimetext (' Hello ', ' text ', ' Utf-8 ')

msg[' Subject '] = Subject

SMTP = Smtplib. SMTP ()
Smtp.connect (' smtp.163.com ')
Smtp.login (username, password)
Smtp.sendmail (sender, receiver, msg.as_string ())
Smtp.quit ()


Vi. messages that are contained in 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 = ' * * * '
Receiver = ' * * * '
Subject = ' Python email test '
SmtpServer = ' smtp.163.com '
Username = ' * * * '
Password = ' * * * '

# Create Message container-the correct MIME type is multipart/alternative.
msg = Mimemultipart (' alternative ')
msg[' Subject ' = "Link"

# Create The body of the message (a Plain-text and an HTML version).
Text = "Hi!\nhow is You?\nhere was the link you wanted:\nhttp://www.python.org"
html = "" "\


Hi!

How is it?

Here's 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,
# The HTML message, is best and preferred.
Msg.attach (part1)
Msg.attach (part2)
#构造附件
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, receiver, msg.as_string ())
Smtp.quit ()

Vii. SSL-based mail

Copy the Code code as follows:

#!/usr/bin/env Python3
#coding: Utf-8
Import Smtplib
From Email.mime.text import Mimetext
From Email.header Import Header
Sender = ' * * * '
Receiver = ' * * * '
Subject = ' Python email test '
SmtpServer = ' smtp.163.com '
Username = ' * * * '
Password = ' * * * '

msg = mimetext (' Hello ', ' text ', ' utf-8 ') #中文需参数 ' Utf-8 ', single-byte characters do not need
msg[' Subject ' = 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, receiver, msg.as_string ())
Smtp.quit ()
  • Related Article

    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.