Python instance code for sending messages (supports HTML, pictures, attachments)

Source: Internet
Author: User
Tags base64 html form

Transfer from http://www.jb51.net/article/34498.htm

First paragraph of code

#!/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 was a multi-part message in MIME format. '

# encapsulate the plain and HTML versions of the message body in an
# ' alternative ' part, so message agents can decide which they want 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 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 = ' [email protected] '
Toadd = [' [email protected] ', ' [email protected] '
Subject = ' Mail Subject '
plaintext = ' Here is plain text '
HTMLText = ' <b>html text </B> '
SendEmail (AuthInfo, Fromadd, Toadd, subject, plaintext, htmltext)

Mail in file Form

#!/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

#!/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

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

Mail with Attachments

#!/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

#!/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 by various elements

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

SSL-based Mail

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

Python instance code for sending messages (supports HTML, pictures, attachments)

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.