MIMEMultipart,MIMEText,MIMEBase和有效载荷,用于在Python中发送带有文件附件的电子邮件 [英] MIMEMultipart, MIMEText, MIMEBase, and payloads for sending email with file attachment in Python

查看:323
本文介绍了MIMEMultipart,MIMEText,MIMEBase和有效载荷,用于在Python中发送带有文件附件的电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在没有MIME太多先验知识的情况下,我试图学习如何编写Python脚本来发送带有文件附件的电子邮件.在交叉引用Python文档,Stack Overflow问题和常规Web搜索之后,我使用以下代码 [1] 解决了这一问题,并对其进行了测试.

Without much prior knowledge of MIME, I tried to learned how to write a Python script to send an email with a file attachment. After cross-referencing Python documentation, Stack Overflow questions, and general web searching, I settled with the following code [1] and tested it to be working.

import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from email.MIMEBase import MIMEBase
from email import encoders

fromaddr = "YOUR EMAIL"
toaddr = "EMAIL ADDRESS YOU SEND TO"

msg = MIMEMultipart()

msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = "SUBJECT OF THE EMAIL"

body = "TEXT YOU WANT TO SEND"

msg.attach(MIMEText(body, 'plain'))

filename = "NAME OF THE FILE WITH ITS EXTENSION"
attachment = open("PATH OF THE FILE", "rb")

part = MIMEBase('application', 'octet-stream')
part.set_payload((attachment).read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', "attachment; filename= %s" % filename)

msg.attach(part)

server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(fromaddr, "YOUR PASSWORD")
text = msg.as_string()
server.sendmail(fromaddr, toaddr, text)
server.quit()

  1. 我对这个脚本现在的工作方式有一个大概的了解,并制定了以下工作流程.请让我知道我的流程图(?)有多准确.

  1. I have a rough idea of how this script works now, and worked out the following workflow. Please let me know how accurate my flowchart(?) is.

 as.string()  
 |
 +------------MIMEMultipart  
              |                                                |---content-type  
              |                                   +---header---+---content disposition  
              +----.attach()-----+----MIMEBase----|  
                                 |                +---payload (to be encoded in Base64)
                                 +----MIMEText

  • 我如何知道何时使用MIMEMultipart,MIMEText和MIMEBase?这似乎是一个复杂的问题,所以也许只是向我提供一些一般的经验法则?

  • How do I know when to use MIMEMultipart, MIMEText and MIMEBase? This seems like a complicated question, so maybe just offer some general rules-of-thumb to me?

    [1] http://naelshiab.com/tutorial-send-email-python/
    [2] http://blog.magiksys.net/generate-并使用python-tutorial发送邮件

    [1]http://naelshiab.com/tutorial-send-email-python/
    [2]http://blog.magiksys.net/generate-and-send-mail-with-python-tutorial

    推荐答案

    电子邮件

    电子邮件由标题(例如发件人",收件人",主题"等)和正文(请参阅 RFC 822,第3.1节).

    默认情况下,邮件正文被视为纯ASCII文本. MIME( RFC 2045 RFC 2047 RFC 2048 RFC 2049 )定义了扩展名,这些扩展名允许指定不同类型的电子邮件内容.

    The body of the message is, by default, treated as plain ASCII text. MIME (RFC 2045, RFC 2046, RFC 2047, RFC 2048, RFC 2049) defines extensions which allow specifying different types of email content.

    使用MIME可以做的一件非常有用的事情是指定内容类型(例如text/htmlapplication/octet-stream).

    One very useful thing you are able to with MIME is specify a Content-Type (e.g. text/html or application/octet-stream).

    另一个有用的事情是,您可以创建包含多个部分的消息(例如,如果要在HTML中同时包含HTML和图像).这是通过指定multipart内容类型( RFC 2046第5.1节).

    Another useful thing is that you can create a message with multiple parts (for instance, if you want to have both HTML and an image within the HTML). This is done by specifying a multipart Content-Type (RFC 2046, section 5.1).

    如果一条消息具有multipart Content-Type,则表示它由多条消息组成,并且每条消息都定义了自己的Content-Type(可以是多部分内容,也可以是其他内容).分段消息是使用 MIMEMultipart 类.

    If a message has a multipart Content-Type, that means it consists of multiple messages and each of them defines its own Content-Type (which can again be multipart or something else). Multipart messages are in Python represented by MIMEMultipart class.

    因此,要回答问题3 :使用MIMEMultipart时,是的,它是树状结构,但如果仅

    So, to answer question 3: When MIMEMultipart is used, then yes, it is a tree-like structure, but if only MIMEText is used, then it is not a tree.

    问题4 询问在哪个类上设置标头("To","From"等)-通过

    Question 4 asks on which class to set the headers ("To", "From" etc.) - that is done the the Message class, but all MIME classes inherit from Message, so it can be done on any of them, but those headers only make sense on the root part of a multipart message.

    换句话说,如果消息仅由一个MIME部分组成,请在该部分上指定标头.如果它由多个部分组成,则根为MIMEMultipart-在该部分上指定标题.

    In other words, if a message consists of only one MIME part, specify headers on that part. If it consists of mutiple parts, then the root is a MIMEMultipart - specify the headers on that part.

    问题2 询问何时使用MIMEMultipart,MIMEText和MIMEBase" . -MIMEBase只是一个基类.正如规范所说:通常,您不会专门创建MIMEBase的实例" -MIMEText用于文本(例如text/plaintext/html),如果整个消息是文本格式,或者部分消息是文本. -MIMEMultipart是说我有多个部分" ,然后列出这些部分-如果您有附件,也可以这样做,以提供相同内容的替代版本(例如纯文本版本和HTML版本)

    Question 2 asks "when to use MIMEMultipart, MIMEText and MIMEBase". - MIMEBase is just a base class. As the specification says: "Ordinarily you won’t create instances specifically of MIMEBase" - MIMEText is for text (e.g. text/plain or text/html), if the whole message is in text format, or if a part of it is. - MIMEMultipart is for saying "I have more than one part", and then listing the parts - you do that if you have attachments, you also do it to provide alternative versions of the same content (e.g. a plain text version plus an HTML version)

    问题5 什么是有效载荷"?" -只是消息(或消息部分)内容的一个奇特词

    Question 5 "What exactly is a "payload"?" - that is just a fancy word for the content of the message (or message part)

    问题6 有一个限制,即在SMTP中仅使用7位.有关更多详细信息,请参见此答案.

    Question 6 There is a limitation to using only 7 bits in SMTP. See this answer for more details.

    我没有完全理解问题1 ,但是图表似乎或多或少是正确的.顺便说一句,我不会在这里使用MIMEBase,因为这里有

    I did not completely understand Question 1, but it seems that the chart is more or less correct. BTW, I would not use MIMEBase here, because there is MIMEApplication which seems more appropriate for the intended purpose.

    这篇关于MIMEMultipart,MIMEText,MIMEBase和有效载荷,用于在Python中发送带有文件附件的电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆