Details about the python mail sending instance code

Source: Internet
Author: User
This article mainly introduces the python mail sending example code. it describes in detail the various methods of sending emails, including file emails, HTML emails, HTML emails with images, etc, for more information, see. This article mainly introduces the python mail sending example code. it describes in detail the various methods of sending emails, including file emails, HTML emails, HTML emails with images, etc, for more information, see.

Python email sending instance

FILE-form emails

#! /Usr/bin/env python3 # coding: UTF-8 import smtplib from emailmimetext import MIMEText from emailheader import Header sender = '*** 'receiver =' *** 'subject = 'Python email test' smtpserver = 'smtpcom 'username =' * ** 'password = '*** 'MSG = MIMEText (' ', 'text', 'utf-8') # The parameter 'utf-8' is required for Chinese characters. single-byte characters do not require msg ['subobject'] = Header (Subject, 'utf-8') smtp = smtplibSMTP () smtpconnect ('smtpcom') smtplogin (username, password) smtpsendmail (sender, receiver, msgas_string () smtpquit ()

HTML emails

#! /Usr/bin/env python3 # coding: UTF-8 import smtplib from emailmimetext import MIMEText sender = '*** 'receiver =' *** 'subject = 'Python email test' smtpserver = 'smtpcom 'username = '***' password = '*** 'MSG = MIMEText ('Hi!', 'Html', 'utf-8') msg ['subobject'] = Subject smtp = smtplibSMTP () smtpconnect ('smtpcom') smtplogin (username, password) smtpsendmail (sender, explorer, msgas_string () smtpquit ()

HTML emails with images

#!/usr/bin/env python3 #coding: utf-8 import smtplib from emailmimemultipart import MIMEMultipart from emailmimetext import MIMEText from emailmimeimage import MIMEImage  sender = '***' receiver = '***' subject = 'python email test' smtpserver = 'smtpcom' username = '***' password = '***'  msgRoot = MIMEMultipart('related') msgRoot['Subject'] = 'test message'  msgText = MIMEText('Some HTML text and an image

good!','html','utf-8') msgRootattach(msgText) fp = open('h:\\python\\jpg', 'rb') msgImage = MIMEImage(fpread()) fpclose() msgImageadd_header('Content-ID', ' ') msgRootattach(msgImage) smtp = smtplibSMTP() smtpconnect('smtpcom') smtplogin(username, password) smtpsendmail(sender, receiver, msgRootas_string()) smtpquit()

Emails with attachments

#! /Usr/bin/env python3 # coding: UTF-8 import smtplib from mirror import MIMEMultipart from emailmimetext import MIMEText from emailmimeimage import MIMEImage sender = '*** 'receiver =' *** 'subject = 'Python email test' smtpserver = 'smtpcom 'username = '*** 'password =' *** 'msgroot = MIMEMultipart ('related ') msgRoot ['subobject'] = 'Test message' # construct the Attachment att = MIMEText (open ('H: \ python \ jpg ', 'RB') read (), 'base64', 'utf-8') att ["Content-Type"] = 'application/octet-stream' att ["Content-Disposition"] = 'attachment; filename = "jpg" 'msgrootattach (att) smtp = smtplibSMTP () smtpconnect ('smtpcom') smtplogin (username, password) smtpsendmail (sender, explorer, msgRootas_string () smtpquit ()

Group email

#! /Usr/bin/env python3 # coding: UTF-8 import smtplib from emailmimetext import MIMEText sender = '*** 'receiver = ['***','****', ……] Subject = 'Python email test' smtpserver = 'smtpcom' username = '*** 'password =' *** 'MSG = MIMEText (' ', 'text ', 'utf-8') msg ['subobject'] = Subject smtp = smtplibSMTP () smtpconnect ('smtpcom') smtplogin (username, password) smtpsendmail (sender, receiver er, msgas_string () smtpquit ()

Emails containing various elements

#! /Usr/bin/env python3 # coding: UTF-8 import smtplib from mirror import MIMEMultipart from emailmimetext import MIMEText from emailmimeimage import MIMEImage sender = '*** 'receiver =' *** 'subject = 'Python email test' smtpserver = 'smtpcom 'username = '*** 'password =' *** '# Create message container-the correct MIME type is multipart/alternative msg = MIMEMultipart ('alternative ') msg ['Subobject'] = "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: \ n # "html = """\       

Hi!
How are you?
Here is 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, in this case # the HTML message, is best and preferred msgattach (part1) msgattach (part2) # construct the Attachment att = MIMEText (open ('H: \ python \ jpg ', 'RB') read (), 'base64', 'utf-8 ') att ["Content-Type"] = 'application/octet-stream' att ["Content-Disposition"] = 'attachment; filename = "jpg" 'msgattach (att) smtp = smtplibSMTP () smtpconnect ('smtpcom') smtplogin (username, password) smtpsendmail (sender, explorer, msgas_string () smtpquit ()

SSL-based mail

#! /Usr/bin/env python3 # coding: UTF-8 import smtplib from emailmimetext import MIMEText from emailheader import Header sender = '*** 'receiver =' *** 'subject = 'Python email test' smtpserver = 'smtpcom 'username =' * ** 'password = '*** 'MSG = MIMEText (' ', 'text', 'utf-8') # The parameter 'utf-8' is required for Chinese characters. single-byte characters do not require msg ['subobject'] = Header (Subject, 'utf-8') smtp = smtplibSMTP () smtpconnect ('smtpcom') smtpehlo () smtpstarttls () smtpehlo () login (1) smtplogin (username, password) smtpsendmail (sender, receiver, msgas_string () smtpquit ()

The above is all the content of this article. I hope it will help you learn and support PHP.

For more details about the python mail instance code, please follow the PHP Chinese network!

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.