相关疑难解决方法(0)

如何从电子邮件中获取csv附件并保存

我试图从电子邮件中获取附件并将​​其保存到具有原始文件名的特定文件夹.电子邮件是非常基本的,除了附件之外没有太多的东西.该文件是一个csv文件,每封电子邮件只有一个.这是我到目前为止所做的,但我是新手,我不知道如何继续.如果有帮助,这是使用Outlook.任何帮助表示赞赏.

import imaplib
import email


mail=imaplib.IMAP4('mailserver.com')
mail.login("username", "password")
mail.select("DetReport")

typ, msgs = mail.uid('Search', None, '(SUBJECT "Detection")')
msgs = msgs[0].split()

for emailid in msgs:
    resp, data = mail.fetch(emailid, "(RFC822)")
    email_body = data[0][1] 
    m = email.message_from_string(email_body)


    message=m.get_content_maintype()
Run Code Online (Sandbox Code Playgroud)

仅供参考,当我运行时,message=m.get_content_maintype()它说它是文本.

python csv email-attachments imaplib python-2.7

9
推荐指数
1
解决办法
2万
查看次数

附件可以在MIME中嵌套的多部分吗?

我知道多部分电子邮件的每个部分都可以是多部分.附件是仅作为顶级部件添加,还是也可以嵌套在多部件中?

例如,我的意思是,这里attachment1.doc是嵌套的,而是attachment2.doc顶级部分.

multipart/mixed
   |---Title: text/plain
   |---Text content: text/plain
   |---Nested multipart: multipart/mixed
   |      |--- attachment1.doc (BASE64)
   |---attachment2.doc (BASE64)

我问,因为我从/sf/answers/1928966721/遇到了这段代码:

    # Iterate the different parts of the multipart message.
    for part in msg.walk():
        # Skip any nested multipart.
        if part.get_content_maintype() == 'multipart':
            continue
Run Code Online (Sandbox Code Playgroud)

它是在Python中,它们遍历消息的不同部分以搜索附件,但跳过任何本身是多部分的部分.

他们这样做是否正确?我尝试阅读RFC3501,但找不到任何明确的说明文件附件是否可以嵌套.

email mime

4
推荐指数
1
解决办法
1813
查看次数

多部分电子邮件中的"部分"是什么?

一点背景......

前段时间,我在Python上编写了一个处理电子邮件消息的程序,总有一件事就是知道电子邮件是否是"多部分".

经过一些研究,我知道它与包含HTML或附件等的电子邮件有关......但我并不是真的理解它.

我对它的使用仅限于2个实例:

1.当我必须从原始电子邮件中保存附件时

我刚刚在互联网上发现了这一点(可能在这里 - 很抱歉没有记下编写它的人,但我似乎无法再找到他了:/)并将其粘贴在我的代码中

def downloadAttachments(emailMsg, pathToSaveFile):
    """
    Save Attachments to pathToSaveFile (Example: pathToSaveFile = "C:\\Program Files\\")
    """
    att_path_list = []
    for part in emailMsg.walk():
        # multipart are just containers, so we skip them
        if part.get_content_maintype() == 'multipart':
            continue

        # is this part an attachment ?
        if part.get('Content-Disposition') is None:
            continue

        filename = part.get_filename()

        att_path = os.path.join(pathToSaveFile, filename)

        #Check if its already there
        if not os.path.isfile(att_path) :
            # finally write the stuff
            fp = open(att_path, 'wb') …
Run Code Online (Sandbox Code Playgroud)

python email imap python-3.x

2
推荐指数
1
解决办法
1018
查看次数

标签 统计

email ×2

python ×2

csv ×1

email-attachments ×1

imap ×1

imaplib ×1

mime ×1

python-2.7 ×1

python-3.x ×1