Python-send messages with various types of attachments

Source: Internet
Author: User
Tags joins

How to send various types of attachments.
The basic idea is to use Mimemultipart to indicate that the message is made up of multiple parts, and then attach each part. If it is an attachment, Add_header joins the declaration of the attachment.
In Python, the following are the inheritance relationships for MIME objects.
Mimebase
|--Mimenonmultipart
|--mimeapplication
|--Mimeaudio
|--Mimeimage
|--MimeMessage
|--Mimetext
|--Mimemultipart
In general, Mimebase is not used, but it's inherited classes are used directly. Mimemultipart has attach method, and Mimenonmultipart not, can only be attach.
MIME has a lot of types, this slightly troublesome, if the attachment is a picture format, I want to use mimeimage, if it is audio, to use Mimeaudio, if it is Word, Excel, I do not know which MIME type to use, Google to check.
The most lazy way is to use the mimeapplication,mimeapplication default subtype, regardless of the type of attachment, is application/octet-stream.
Application/octet-stream that "This is a binary file, I hope you know how to deal with", and then the client, such as QQ mailbox, after receiving this statement, will be based on the file name extension to guess.

The code below.

Assume that the current directory has Foo.xlsx/foo.jpg/foo.pdf/foo.mp3 4 files.

How to send various types of attachments.
The basic idea is to use Mimemultipart to indicate that the message is made up of multiple parts, and then attach each part. If it is an attachment, Add_header joins the declaration of the attachment.
In Python, the following are the inheritance relationships for MIME objects.
Mimebase
|--Mimenonmultipart
|--mimeapplication
|--Mimeaudio
|--Mimeimage
|--MimeMessage
|--Mimetext
|--Mimemultipart
In general, Mimebase is not used, but it's inherited classes are used directly. Mimemultipart has attach method, and Mimenonmultipart not, can only be attach.
MIME has a lot of types, this slightly troublesome, if the attachment is a picture format, I want to use mimeimage, if it is audio, to use Mimeaudio, if it is Word, Excel, I do not know which MIME type to use, Google to check.
The most lazy way is to, regardless of the type of attachment, the mimeapplication,mimeapplication default subtype is Application/octet-stream.
Application/octet-stream that "This is a binary file, I hope you know how to deal with", and then the client, such as QQ mailbox, after receiving this statement, will be based on the file name extension to guess.

The code below.

Assume that the current directory has Foo.xlsx/foo.jpg/foo.pdf/foo.mp3 4 files.

Import Smtplib
From Email.mime.multipart import Mimemultipart
From Email.mime.text import Mimetext
From email.mime.application import mimeapplication
_user = "[Email protected]"
_PWD = "* * *"
_to = "[Email protected]"

#如名字所示Multipart就是分多个部分
msg = Mimemultipart ()
msg["Subject"] = "don ' t Panic"
Msg["from"] = _user
Msg["to"] = _to

#---This is the text part---
Part = Mimetext ("Disguised, unscrupulous")
Msg.attach (part)

#---This is the attachment section---
#xlsx类型附件
Part = mimeapplication (open (' foo.xlsx ', ' RB '). Read ())
Part.add_header (' content-disposition ', ' attachment ', filename= "foo.xlsx")
Msg.attach (part)

#jpg类型附件
Part = mimeapplication (open (' foo.jpg ', ' RB '). Read ())
Part.add_header (' content-disposition ', ' attachment ', filename= "foo.jpg")
Msg.attach (part)

#pdf类型附件
Part = mimeapplication (open (' foo.pdf ', ' RB '). Read ())
Part.add_header (' content-disposition ', ' attachment ', filename= "Foo.pdf")
Msg.attach (part)

#mp3类型附件
Part = mimeapplication (open (' Foo.mp3 ', ' RB '). Read ())
Part.add_header (' content-disposition ', ' attachment ', filename= "Foo.mp3")
Msg.attach (part)

s = smtplib. SMTP ("smtp.qq.com", timeout=30) #连接smtp邮件服务器, port default is 25
S.login (_user, _pwd) #登陆服务器
S.sendmail (_user, _to, msg.as_string ()) #发送邮件
S.close ()

Python-send messages with various types of attachments

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.