Use Python to implement a simple tutorial on sending email programs with attachments

Source: Internet
Author: User
This article mainly introduces how to use Python to implement a simple tutorial that can send email programs with attachments, and use the MIMEApplication module to send various types of files, the basic idea for a friend is to use MIMEMultipart to indicate that the email is composed of multiple parts and then attach each part. If it is an attachment, add_header is added to the declaration of the attachment.
In python, the inheritance relationships of MIME objects are as follows.
MIMEBase
| -- MIMENonMultipart
| -- MIMEApplication
| -- MIMEAudio
| -- MIMEImage
| -- MIMEMessage
| -- MIMEText
| -- MIMEMultipart
Generally, MIMEBase is not used, but its inheritance class is directly used. The MIMEMultipart has the attach method, while the MIMENonMultipart does not. it can only be attach.
MIME has many types. this is a little tricky. if the attachment is in the image format, I want to use MIMEImage. if it is audio, I want to use MIMEAudio. if it is word or excel, I don't know which MIME type to use. google should check it.
The lazy method is to use MIMEApplication for any type of attachments. the default subtype of MIMEApplication is application/octet-stream.
Application/octet-stream indicates "this is a binary file and you want to know how to handle it". then, the client, such as QQ mail, guesses based on the file extension after receiving this declaration.

The code below.
Assume that there are four files, foo.xlsx/foo.jpg/foo.pdf.

Import smtplib from email. mime. multipart import MIMEMultipart from email. mime. text import MIMEText from email. mime. application import MIMEApplication _ user = "sigeken@qq.com" _ pwd = "***" _ to = "402363522@qq.com" # as shown in the name, Multipart is divided into multiple parts msg = MIMEMultipart () msg ["Subject"] = "don't panic" msg ["From"] = _ user msg ["To"] = _ to # --- this is the text part --- part = MIMEText ("dressing up, ") msg. attach (part) # --- this is the attachment part --- # xlsx type attachment part = MIMEApplication(open('foo.xlsx ', 'RB '). read () part. add_header ('content-disposition', 'attachment', filename = "foo.xlsx") msg. attach (part) # jpg attachment part = MIMEApplication(open('foo.jpg ', 'RB '). read () part. add_header ('content-disposition', 'attachment ', filename = "foo.jpg") msg. attach (part) # pdf attachment part = mimeapplication(open('fooket', 'RB '). read () part. add_header ('content-disposition', 'attachment', filename = "footings") msg. attach (part) # mp3 attachment part = mimeapplication(open('fooket', 'RB '). read () part. add_header ('content-disposition', 'attachment', filename = "footings") msg. attach (part) s = smtplib. SMTP ("smtp.qq.com", timeout = 30) # connect to the smtp mail server. the default port is 25 s. login (_ user, _ pwd) # log on to the server s. sendmail (_ user, _ to, msg. as_string () # send email s. close ()

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.