Summarize methods for sending various forms of mail using Python

Source: Internet
Author: User
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 ()
  • 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.