Python grabbing data from an outlook folder - python

I am currently trying to figure out how to grab attachments from emails in a specific folder. Every Thursday, an email will arrive in my weekly reports folder and I would like to have a script that will grab the attachments of the email and save it to a specific file location.
This is the code I have so far which I believe is correct to instantiate everything.
import win32com.client
import os
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder(6) # "6" refers to the index of a folder - in this case the inbox. You can change that number to reference
messages = inbox.Items
message = messages.GetFirst()
However, I am unsure of how to procede as I have been unable to find good documentation of this module. What would be the next steps in order to grab from a specific folder, in this case it is one below the inbox folder, and save the first email's attachments to a specific location?
If it makes any difference, there are 4 csv files in the weekly email
Thanks

Related

Extract the forwarded emails from outlook using python and split it

Need to extract the emails from outlook. Most of them are forwarded emails. I tried to extract the emails using pywin32 and it worked by extracting the entire email.
But the problem, I need to have it splitted as title, body and signature separately.
For ex:
Title should include From, To, CC, Subject.
Body should include the email body
Signature should include the email signature if its available.
The issue is that as I have threaded emails, the body section extracts the entire forwarded email like below.
Thanks and Regards,
Sam
From: Odar <odar#sysp.com>
Sent: 2 October 2007 09:22
To: rav <rav#tsvs.com>
Cc: hare <hare#sysp.com>; fere <fere#sysp.com>
Subject: CHAN BULB - test at Company
CAUTION - EXTERNAL SENDER !
Dear Sir,
Good day!!
Please note that the product is checked and working.
It will get delivered as soon as possible.
Best Regards
Odar
Executive
This is the body off the email pywin extracted as it is a threaded email.
But I need the title separately includes from,sent,to,cc,subject.
And body should have the middle text and
signature should include the last part.
Any suggestions how it can be done??
The code I tried is below:
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
outbox = outlook.GetDefaultFolder(6)
messages = outbox.Items
print(messages[1].Body)

Python Email Saving and Attachment Within An Attachment

As part of one of the processes we run we receive an email (in Outlook) that has a csv attachment within a .msg attachment that we need to save down.
I'm trying to save the .csv file but cannot figure out how to get to the attachment in the attachment. I am also brand new to Python so go easy on me. I have made the query generic but obviously the below only saves down the first attachment - I tried creating a variable for att.Attachment but that didn't work. HELP!
So far I have this:
import win32com.client
from datetime import datetime
from pathlib import Path
import csv
import os
outlook = win32com.client.Dispatch('outlook.application').GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder(6)
messages=inbox.Items
messages.Sort("[ReceivedTime]", True)
for i in list(messages):
if "Hello" in str(i.subject):
for att in i.Attachments:
att.SaveAsFile(r'/C:/Users/el/Documents/' + att.FileName)
The Outlook object model doesn't provide any methods for processing attached files beside saving them to the disk. So, you can save attached files to the disk by using the Attachment.SaveAsFile method. Then you you can open the saved files by automating their target applications, for example, the .msg file can be run to be opened in the default application (Outlook). In that case you will get a new Inspector window opened because Outlook is singleton, only one application can be run at the same time on the system. For the .csv file Excel can be automated to deal with a file content.

Scraping information from Outlook email folder

Python newbie here...
I'm attempting to pull information from multiple emails within a folder in Outlook.
Everyday an email containing a table of information is sent to the mailbox and is autofiled into a folder. My aim is to pull the information from the table in these emails for the last 6 months and present this in a pandas dataframe.
I have no idea how to scrape this information from an email and would appreciate any help.
Thanks!!
It seems you need to automate Outlook to get the required information. To get all items for the last 6 months you need to use the Find/FindNext or Restrict methods of the Items class. Read more about these methods in the following articles:
How To: Use Find and FindNext methods to retrieve Outlook mail items from a folder (C#, VB.NET)
How To: Use Restrict method to retrieve Outlook mail items from a folder

Python imaplib recover emails with \\Deleted flag and move to trash

I've been getting a lot of spam emails from various colleges. I wrote a simple Python program to go through my emails, and find all the ones whose sender ends in .edu.
Then, to delete it, I though I was supposed to add the \\Deleted flag to those emails using: Gmail.store(id, "+FLAGS", "\\Deleted"). This did not work, and some of them didn't even disappear from the inbox.
After more research, I found that instead I had to use Gmail.store(id, "+X-GM-LABELS", "\\Trash").
So I updated the program to use that, but now it doesn't see any of the emails that I previously added the \\Deleted flag to.
Is there any way I can reset all those emails and then trash them afterward?
They should be in your All Mail folder. Use the WebUI and search for them and trash them, or select the "[Gmail]\All Mail" folder (watch out for localization, this can change name for non-English users).

Get all information about an email

I want to get all my emails with all their information, e.g. Body, Subject, Date Received, Folders, Attachments, From, To, etc.
I found this:
How can I download all emails with attachments from Gmail?
But it's only subjects/from/attachments.
I copied pretty much everything and added this:
for payload in mail.get_payload():
print part.get_payload(decode=True)
But I'm getting (�� (�� (�� at times and html at times.
I just want to have simple access to an email message where everything is decoded, easily handleable.

Categories