Python Seven message content Send method instance _python

Source: Internet
Author: User

Mail in the form of a document

Copy 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.login (username, password)
Smtp.sendmail (sender, receiver, msg.as_string ())
Smtp.quit ()

Two, HTML form of mail

Copy 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 (' </pre>
<pre> ', ' 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, with pictures of HTML mail

Copy 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 (' <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, receiver, msgroot.as_string ())
Smtp.quit ()

Four, with the attachment of the message

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

Group E-Mail

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

Six, the various elements are included in the mail
Copy 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 = ' * * *

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

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


Hi!

How are?

This is the <a href= "http://www.python.org" >link</a> your wanted.



"""

# Record the MIME types of both Parts-text/plain and text/html.
Part1 = Mimetext (text, ' plain ')
Part2 = mimetext (HTML, ' HTML ')

# Attach parts into the message container.
# According to RFCs 2046, the last part of a multipart
# 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 ()

Seven, SSL based mail

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

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.