Details about the python mail sending instance code, and the python mail sending

Source: Internet
Author: User

Details about the python mail sending instance code, and the python mail sending

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

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('<b>Some <i>HTML</i> text</b> and an image<br><br>good!','html','utf-8') msgRootattach(msgText)  fp = open('h:\\python\\jpg', 'rb') msgImage = MIMEImage(fpread()) fpclose()  msgImageadd_header('Content-ID', '<image1>') 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: \ nhttp: // wwwpythonorg "html =" "\ 

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 be helpful for your learning and support for helping customers.

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.