First paragraph of code:
Copy CodeThe 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 ' 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 ', ' )
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 = ' username@somehost.com '
Toadd = [' someone@somehost.com ', ' other@somehost.com ']
Subject = ' Mail Subject '
plaintext = ' Here is plain text '
HTMLText = 'html text '
SendEmail (AuthInfo, Fromadd, Toadd, subject, plaintext, htmltext)
Mail in file Form
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.login (username, password)
Smtp.sendmail (sender, receiver, msg.as_string ())
Smtp.quit ()
Messages 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 ()
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 text 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 ()
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 ()
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 ', ' 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
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 = ' * * * '
# 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 ()
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 ', ' 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 ()