We usually need to use Python to send all kinds of mail, how is this requirement implemented? The answer is simple, and smtplib and email libraries can help with this requirement. Smtplib and email combinations can be used to send various types of mail: Plain text, HTML form, with attachments, mass mailing, email with pictures and more. We will explain the function of sending the mail in a few sections here.
Smtplib is the module that Python uses to send mail, and email is used to process mail messages.
Send messages in HTML form
Sending an HTML form of a message requires the _subtype of Mimetext in Email.mime.text to be set to HTML, and the contents of _text should be in HTML form.
Import smtplibfrom email.mime.text Import mimetextsender = ' * * * receiver = ' * * * ' subject = ' python email test ' smtpserver = ' smtp.163.com ' username = ' * * * ' password = ' * * * ' msg = mimetext (U 'How are you doing
"', ' html ', ' Utf-8 ') msg[' Subject ') = SUBJECTSMTP = Smtplib. SMTP () smtp.connect (smtpserver) smtp.login (username, password) smtp.sendmail (sender, receiver, msg.as_string ()) Smtp.quit ()
Note: The code here does not include exception handling and requires the reader to handle the exception itself.
Send a message with a picture
Messages with pictures are sent using Email.mime.multipart's Mimemultipart and Email.mime.image's mimeimage:
Import smtplibfrom email.mime.multipart import mimemultipartfrom email.mime.text import Mimetextfrom email.mime.image Import Mimeimagesender = ' * * * ' receiver = ' * * * ' subject = ' python email test ' smtpserver = ' smtp.163.com ' username = ' * * ' pass Word = ' * * * ' msgroot = Mimemultipart (' related ') msgroot[' Subject '] = ' test message ' Msgtext = Mimetext ( " ' and an image.good! ', ' html ', ' Utf-8 ') Msgroot.attach (msgtext) fp = open ('/users/1.jpg ', ' RB ') Msgimage = Mimeimage ( Fp.read ()) Fp.close () msgimage.add_header (' Content-id ', '
) Msgroot.attach (msgimage) SMTP = Smtplib. SMTP () smtp.connect (smtpserver) smtp.login (username, password) smtp.sendmail (sender, receiver, msgroot.as_string ()) Smtp.quit ()
Send a message with an attachment
Sending messages with attachments is the use of Email.mime.multipart's Mimemultipart and Email.mime.image mimeimage, with emphasis on constructing message header information:
Import smtplibfrom email.mime.multipart import mimemultipartfrom email.mime.text Import mimetextsender = ' * * * ' receiver = ' * * * * ' subject = ' python email test ' smtpserver = ' smtp.163.com ' username = ' * * * ' password = ' * * * ' msgroot = Mimemultipart (' Mixe d ') msgroot[' Subject ' = ' test message ' # construct attachment att = mimetext (open ('/users/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 (smtpserver) smtp.login (username, password) smtp.sendmail (sender, receiver, msgroot.as_string ()) Smtp.quit ()