Python seven ways to send a message content instance

Source: Internet
Author: User
Tags base64 html form

I. Mail in the form of a file

#!/usr/bin/env python3#coding:utf-8import smtplibfrom email.mime.text import mimetextfrom email.header Import Headersender = ' * * * ' 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

#!/usr/bin/env python3#coding:utf-8import smtplibfrom email.mime.text Import mimetextsender = ' * * * receiver = ' * * * ' Subject = ' python email test ' smtpserver = ' smtp.163.com ' username = ' * * * ' password = ' * * * ' msg = mimetext (' </pre>


Three, HTML messages with pictures

#!/usr/bin/env python3#coding: utf-8import smtplibfrom email.mime.multipart import  mimemultipartfrom email.mime.text import mimetextfrom 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) &NBSP;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

#!/usr/bin/env python3#coding:utf-8import smtplibfrom email.mime.multipart import Mimemultipartfrom Email.mime.text Import mimetextfrom email.mime.image Import mimeimage sender = ' * * * ' receiver = ' * * * ' subject = ' python email test ' smtpserve r = ' smtp.163.com ' username = ' * * * ' password = ' * * * ' msgroot = Mimemultipart (' related ') msgroot[' Subject '] = ' test message ' # Construct Attachment 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

#!/usr/bin/env python3#coding:utf-8import smtplibfrom 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

#!/usr/bin/env python3#coding: utf-8import smtplibfrom email.mime.multipart import  mimemultipartfrom email.mime.text import mimetextfrom 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 are you?\nhere is the link you  wanted:\nhttp://www.python.org "html = " "" " Hi!        How are you?       here is the <a href= "http://www.python.org" >link</a>  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,  in this case# 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

#!/usr/bin/env python3#coding:utf-8import smtplibfrom email.mime.text import mimetextfrom email.header Import Headersender = ' * * * ' 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 ()


Python seven ways to send a message content instance

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.