Use Python to send a summary of various forms of mail, python in various forms

Source: Internet
Author: User

Use Python to send a summary of various forms of mail, python in various forms

We usually need to use Python to send all kinds of emails. How can we achieve this? The answer is actually very simple. smtplib and the e-mail library can help to achieve this requirement. The combination of smtplib and email can be used to send various types of Emails: common text, HTML form, emails with attachments, Group emails, and images. Here we will explain the email sending function in several sections.
Smtplib is a Python module used to send mail, and an email is used to process mail messages.

Send HTML emails
To send an email in HTML format, you must set the _ subtype of MIMEText in email. mime. text to html, and the content of _ text should be in HTML format.

Import smtplibfrom email. mime. text import MIMETextsender = '*** 'courier =' *** 'subject = 'python email test' smtpserver = 'smtp .163.com 'username =' *** 'password = '* ** 'msg = MIMEText (u''' <pre> 

Note: The code here does not add Exception Handling. You need to handle the exception yourself.

Send email with image
An email with an image is sent using MIMEMultipart of email. mime. multipart and MIMEImage of email. mime. image:

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 = '***'password = '***'msgRoot = MIMEMultipart('related')msgRoot['Subject'] = 'test message'msgText = MIMEText(  '''<b> Some <i> HTML </i> text </b > 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', '<image1>')msgRoot.attach(msgImage)smtp = smtplib.SMTP()smtp.connect(smtpserver)smtp.login(username, password)smtp.sendmail(sender, receiver, msgRoot.as_string())smtp.quit()

Send email with attachment
Emails with attachments are sent using MIMEMultipart of email. mime. multipart and MIMEImage of email. mime. image. The focus is to construct the mail header information:

Import smtplibfrom email. mime. multipart import MIMEMultipartfrom email. mime. text import MIMETextsender = '*** 'courier =' *** 'subject = 'python email test' smtpserver = 'smtp .163.com 'username =' *** 'password = '* ** 'msgroot = MIMEMultipart ('mixed ') msgRoot ['subobject'] = 'test message' # construct the 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, aggreger, 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.