Python allows you to send emails and attachments.

Source: Internet
Author: User

Python allows you to send emails and attachments.

Today, I want to tell you how to send an email to python. I have to go to Baidu for additional official words. I still have a lot of documents to tell you the truth, so most people will not go to it. I will go back to the topic:

If the mac is not based on the dependent module, install it as follows:

If there is no error in the import module, the installation is successful.

Python sends an unknown MIME-type file attachment. The basic idea is as follows:
1. Construct the MIMEMultipart object as the root container
2. Construct the MIMEText object as the mail display content and attach it to the root container
3. Construct the MIMEBase object as the file attachment content and attach it to the root container
A. Read and format the file content.
B. Set the attachment Header
4. Set root container Properties
5. Obtain the formatted complete text.
6. Send emails using smtp

Instance code:

#! /Usr/bin/env python #-*-coding: UTF-8-*-import smtplib from email. mime. multipart import MIMEMultipart from email. mime. text import MIMEText from email. mime. application import MIMEApplication class Mailer (object): def _ init _ (self, maillist, mailtitle, mailcontent): self. mail_list = maillist self. mail_title = mailtitle self. mail_content = mailcontent self. mail_host = "smtp.163.com" self. mail_user = "Your email name" self. mail_pass = "your email password" self. mail_postfix = "163.com" def sendMail (self): me = self. mail_user + "<" + self. mail_user + "@" + self. mail_postfix + ">" msg = MIMEMultipart () msg ['subobject'] = 'python mail test' msg ['from'] = me msg ['to'] = "; ". join (self. mail_list) # puretext = MIMEText ('

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.