简体   繁体   中英

How to save an email with no attachment using Python?

I need to save emails and attachments from Outlook to a folder on my desktop. The logic I need is - if the email has an attachment, save the attachment. If the email doesn't have an attachment, save the email.

I made the below sample script to run through an inbox that currently has two emails - one with an attachment and one without. It correctly saves the PDF attachment, but isn't saving the email without an attachment. I used a print to check that the == 0 condition was satisfied and it correctly printed the body of the email but no file is being saved for this email.

I tried changing the filepath to.html.

import os
import datetime
import win32com.client #pip install win32com.client
from win32com.client import constants


#output folder
path = r'C:\Users\SJF2\Desktop\Bank'

filename = r'\testfile.msg'

fullpath = path + filename

print(fullpath)

#create output folder
os.makedirs(path, exist_ok=True)

#connect to outlook 
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")

#Get inboox
inbox = outlook.GetDefaultFolder(6)

#Get specific folder contents
email_folder = inbox.Folders['Test']

#Get items from folder
emails = email_folder.Items

for email in emails:

    subject = email.Subject
    attachments = email.Attachments
    body = email.Body

    if email.Attachments.Count == 0: # check if the email has attachments
        email.SaveAs(fullpath)
        
    else:

        for attachment in attachments:
            fullpath = path + "\\" + str(attachment)
            attachment.SaveAsFile(fullpath)
            #print(attachment)
            print(fullpath)

It seems you are passing a badly-composed file path. After the first iteration the fullpath is changed to the file path of the attached file and doesn't contain initial value any longer. Try to use the following code where separate strings are used:

import os
import datetime
import win32com.client #pip install win32com.client
from win32com.client import constants


#output folder
path = r'C:\Users\SJF2\Desktop\Bank'

filename = r'\testfile.msg'

fullpathEmail = path + filename

print(fullpathEmail)

#create output folder
os.makedirs(path, exist_ok=True)

#connect to outlook 
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")

#Get inboox
inbox = outlook.GetDefaultFolder(6)

#Get specific folder contents
email_folder = inbox.Folders['Test']

#Get items from folder
emails = email_folder.Items

for email in emails:

    subject = email.Subject
    attachments = email.Attachments
    body = email.Body

    if email.Attachments.Count == 0: # check if the email has attachments
        email.SaveAs(fullpathEmail)
        
    else:

        for attachment in attachments:
            fullpathAttachment = path + "\\" + str(attachment)
            attachment.SaveAsFile(fullpathAttachment)
            #print(attachment)
            print(fullpathAttachment)

Be aware, the SaveAs method accepts two parameters, so I'd suggest specifying the the file type to save too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM