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.