This article mainly introduces the python sends the message instance code, detailed introduces each kind of way sends the mail, including the file form mail, the HTML form mail, with the picture HTML mail and so on, has the interest to be possible to understand.
Python Send mail instance
Mail in file Form
#!/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 (' Hello ', ' text ', ' utf-8 ') #中文需参数 ' Utf-8 ', single-byte characters do not need msg[' Subject '] = Header (Subject, ' Utf-8 ') SMTP = SMTPLIBSMTP () smtpconnect (' smtpcom ') smtplogin (username, password) smtpsendmail (sender, receiver, msgas_string ()) Smtpquit ()
Messages in HTML form
#!/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 messages with pictures
#!/usr/bin/env python3 #coding: Utf-8 import smtplib from Emailmimemultipart import Mimemultipart from Emailmimetext Impor T mimetext from emailmimeimage import mimeimage sender = ' * * * ' receiver = ' * * * ' subject = ' python email test ' smtpserv er = ' 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 ()
Mail with Attachments
#!/usr/bin/env python3 #coding: Utf-8 import smtplib from Emailmimemultipart import Mimemultipart from Emailmimetext Impor T mimetext from emailmimeimage import mimeimage sender = ' * * * ' receiver = ' * * * ' subject = ' python email test ' smtpserv er = ' smtpcom ' username = ' * * * ' password = ' * * * ' msgroot = Mimemultipart (' related ') msgroot[' Subject '] = ' test message ' ' #构造附件 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, receiver, msgrootas_string ()) Smtpquit ()
Group Mail
#!/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 (' Hello ', ' text ', ' UTF -8 ') msg[' Subject ' = Subject smtp = SMTPLIBSMTP () smtpconnect (' smtpcom ') smtplogin (username, password) Smtpsendmail (sender, receiver, msgas_string ()) Smtpquit ()
Messages that are contained by various elements
#!/usr/bin/env python3 #coding: Utf-8 import smtplib from Emailmimemultipart import Mimemultipart from Emailmimetext Impor T 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[' Subject '] = "Link" # Create the body of the message (a Plain-text and an HTML ve rsion) Text = "Hi!\nhow is You?\nhere was the link you wanted:\nhttp://www.php.cn/" html = "" \
SSL-based Mail
#!/usr/bin/env python3 #coding: Utf-8 import smtplib from Emailmimetext import mimetext from Emailheader import Header Sen Der = ' * * * ' receiver = ' * * * ' subject = ' python email test ' smtpserver = ' smtpcom ' username = ' * * * ' password = ' * * * ' ms g = mimetext (' Hello ', ' text ', ' utf-8 ') #中文需参数 ' Utf-8 ', single-byte characters do not need msg[' Subject ' = Header (Subject, ' Utf-8 ') SMTP = SMTPLIBSMTP () smtpconnect (' smtpcom ') Smtpehlo () Smtpstarttls () Smtpehlo () smtpset_debuglevel (1) smtplogin (username, Password) smtpsendmail (sender, receiver, msgas_string ()) Smtpquit ()
The above is the whole content of this article, I hope that everyone's learning has helped, but also hope that we support topic.alibabacloud.com.