Python sends an instance code for a message (HTML, pictures, attachments) _python

Source: Internet
Author: User
Tags base64 html form

First paragraph code:

Copy Code code 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 ' Incomplete login info, exit now '
Return

# Set Root information
Msgroot = Mimemultipart (' related ')
msgroot[' Subject '] = 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 '
# ' alternative ' part, such as agents can decide which they to display.
msgalternative = Mimemultipart (' alternative ')
Msgroot.attach (msgalternative)

#设定纯文本信息
Msgtext = Mimetext (plaintext, ' plain ', ' utf-8 ')
Msgalternative.attach (Msgtext)

#设定HTML信息
Msgtext = Mimetext (htmltext, ' html ', ' Utf-8 ')
Msgalternative.attach (Msgtext)

       #设定内置图片信息
        fp = open (' Test.jpg ', ' RB ')
        msgimage = Mimeimage (Fp.read ())
         fp.close ()
        msgimage.add_header (' Content-id ', ' <image1> ')
        Msgroot.attach (msgimage)

       #发送邮件
        smtp = Smtplib. SMTP ()
       #设定调试级别, depending on the circumstances
        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 ', ' other@somehost.com ']
Subject = ' Mail Subject '
plaintext = ' Here is plain text '
HTMLText = ' <b>html text </B> '
SendEmail (AuthInfo, Fromadd, Toadd, subject, plaintext, htmltext)



Messages in the form of a file

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

Messages in HTML form

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

msg[' Subject '] = Subject

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

HTML messages with pictures

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.<br><br>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 ', ' <image1> ')
Msgroot.attach (Msgimage)

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

Messages with Attachments

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

Messages that are contained in various elements

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 = "" "\
<body>
<p>Hi!<br>
How are you?<br>
This is the <a href= "http://www.python.org" >link</a> your wanted.
</p>
</body>
"""

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

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 ', ' plain ', ' 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.