I have created a rule in Outlook to move all incoming messages from a particular sender to a subfolder in my Inbox.Like -
Inbox
- Subfolder
I wrote a piece of code
import win32com.client
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder(6) #6 = Inbox (without mails from the subfolder)
messages = inbox.Items
message = messages.GetLast()
body_content = message.body
print body_content #Sometimes has parsing error due to different encoding format
How can I
1) Read the mails in this particular folder inside Inbox
2) Take care of error like UnicodeEncodeError: 'charmap' codec can't encode - character maps to
print (u'\2109') issues this error too.
outlook.GetDefaultFolder(6) is "Inbox" position by default. You need to traverse the list of folders in it, so try this
inbox = outlook.GetDefaultFolder(6).Folders.Item("Your_Folder_Name")
u'\2109' looks a lot like UTF-8 encoding.
So print(body_content.encode("utf-8")) will do the trick.
outlook = win32com.client.Dispatch('outlook.application')
mapi = outlook.GetNamespace("MAPI")
inbox = mapi.GetDefaultFolder(6).Folders["SubFolder"]
mails = inbox.Items
The above method will also work.
Related
I am trying to download and save the outlook email attachment from the most recent email in a folder.
I have a code that downloads all of the attachment from a outlook folder and saves it.
Any help is appreciated.
from pathlib import Path
import win32com.client
output_dir = Path.home()/r"Documents\Test"
output_dir.mkdir(parents=True, exist_ok=True)
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder(6).folders("Sample Folder").folders("Sample Subfolder")
messages = inbox.Items
message = messages.GetFirst()
for message in messages:
if message.Subject == 'Sample Subject' or message.Subject == 'Sample Subject 2':
attachments = message.Attachments
subject = messages.GetFirst().Subject
for attachment in attachments:
attachment.SaveAsFile(output_dir / str(attachment))
I am trying to download and save the outlook email attachment from the most recent email in a folder.
To get the most recent item from the folder you need to sort the collection first by using the Sort method in the following way (VBA):
messages = inbox.Items
messages.Sort("[RecievedTime]", false)
message = messages.GetFirst()
Also iterating over all items in the folder is not really a good idea:
for message in messages:
if message.Subject == 'Sample Subject' or message.Subject == 'Sample Subject 2':
Instead, you need to use the Find/FindNext or Restrict methods of the Items class. They allows getting items that correspond to your conditions without iterating over all items in the folder. Read more about these methods in the articles that I wrote for the technical blog:
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
I hope you are well.
I have been doing some python practice with web scraping and have come across the win32com library and have been really struggling to get the attachment from my outlook email ( it is just the one)! I have managed to view all of the emails by the following code. I was wondering if you could help me with getting the attachment file called lets say "data_bay1.xlsx" as i am stumped on the errors. I can so far get the data from the email but i cannot get the attachment xlsx file and the attachment part of the code when i try to run it gives me errors. Please see what I have done so far and I hope any of you can help. I am using anaconda and windows. Thank you in advance! Kind regards, Lily
import win32com.client
#connecting python to outlook
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
#connecting to our inbox
inbox = outlook.GetDefaultFolder(6)
(inbox)
# here we are ensuring the indexes exist so we can grab data
outlook=win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
for i in range(50):
try:
box = outlook.GetDefaultFolder(i)
name = box.Name
print(i, name)
except:
pass
messages = inbox.items
messages = messages.GetFirst() earlier
# get the last email
#message = messages.GetLast()
(message)
#to loop through the email in the inbox
while True:
try:
print(message.subject) # get the subject of the email
# if you use messages.GetFirst() earlier
message = messages.GetNext()
# if you use messages.GetPrevious() earlier
#message = messages.GetPrevious()
except:
# if you use messages.GetFirst() earlier
message = messages.GetNext()
# if you use messages.GetPrevious() earlier
#message = messages.GetPrevious()
# get the attachment
attachments = message.Attachments# return the first item in attachments
attachment = attachments.Item(1)
# the name of attachment file
attachment_name = str(attachment).lower()
attachment.SaveASFile(path+ 'C:\Users\lily\OneDrive - Dataenv\Documents + attachment_data_bay1)
Iterating over all items in the folder is not really a good idea:
#to loop through the email in the inbox
while True:
try:
print(message.subject) # get the subject of the email
# if you use messages.GetFirst() earlier
message = messages.GetNext()
# if you use messages.GetPrevious() earlier
#message = messages.GetPrevious()
except:
# if you use messages.GetFirst() earlier
message = messages.GetNext()
# if you use messages.GetPrevious() earlier
#message = messages.GetPrevious()
Instead, you need to use the Find/FindNext or Restrict methods of the Items to find items that correspond to your conditions. 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
The best you can do is filter the list down to only the items with attachments. For that use PR_HASATTACH (DASL name is http://schemas.microsoft.com/mapi/proptag/0x0E1B000B) MAPI property and then loop through the returned items and process their Attachments collection.
I'm struggling with python integration with outlook using win32com.client.
All I am trying to do is get the most recent email from outlook and (at the moment) retrieve and print the name of the attachment
The code I'm trying to use:
import win32com.client
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespcae("MAPI")
inbox = outlook.GetDefaultFolder(6)
message = inbox.GetLast()
att = message.Attachmets
print (att.filename)
Output
com_error: (-2147221005, 'Invalid class string', None, None)
Any help would really be appreciated.
The error is Outlook can't be found on the system but You have also misspelled GetNamespace, you have GetNamespcae
Change inbox.GetLast(), to messages = inbox.Items then message = messages.GetLast()
Example
import win32com.client
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder(6)
messages = inbox.Items
messages.Sort('[ReceivedTime]', False)
message = messages.GetLast()
for attachment in message.Attachments:
print(attachment.FileName)
Here is another example with filter https://stackoverflow.com/a/57931132/4539709
I wanted to check how can I read all emails from outlook in python
I am using below code, but this code is reading only first mail,
import win32com.client
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
# any other folder
messages = inbox.Items
message = messages.GetLast()
body_content = message.Body
subject = message.Subject
categories = message.Categories
print(body_content)
print(subject)
print(categories)
I tried to find a way so that we can read all emails but unable to get a solution, is anyone know how we can read all emails and store in the database.
You can iterate through the messages object to get all email content.
import win32com.client
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder(6)
messages = inbox.Items
'''message = messages.GetLast()
body_content = message.Body
subject = message.Subject
categories = message.Categories
print(body_content)
print(subject)
print(categories)'''
for message in messages:
print(message.Subject)
Intro
I'd like to create some sort of archiving script, which would collect all Outlook (unicode) emails's date, sender (name+address), recipient(s) (name(s)+address(es)), subject and put them in a CSV file.
(The extra super solution would be if it could extract the containing folders' name and possible categories as well - although it is not a must.
And as final step, I would like to make it portable, so others could use it without having Python.)
(I'm using Python 2.7 and Outlook 2013)
Code
Here's what I have so far:
import win32com.client
import sys
import unicodecsv as csv
output_file = open('./outlook_farming_001.csv','wb')
output_writer = csv.writer(output_file, delimiter = ";", encoding='latin2')
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.
messages = inbox.Items
for i, message in enumerate(messages): # enumerated the items
try:
sender = message.SenderName
sender_address = message.sender.address
sent_to = message.To
date = message.LastModificationTime
subject = message.subject
output_writer.writerow([
date,
sender,
sender_address,
sent_to,
subject])
except Exception as e:
()
output_file.close()
The questions:
How to make sure it extracts all the email? (When I run the script, it works, but it extracted only 1555 emails, although my Outlook Inbox sais, it contains 4785.)
How to make it work on all the Outlook folders? (It only deals with Inbox, but I would need all the other folders (sent, and other created ones))
How to get the recipients' email address? (I can only extract the screened names)
If you have any tip for any of the questions, that would be greatly appreciated.
Thanks in advance!!
For question 2:
inbox = outlook.GetDefaultFolder(6)
"6" in the code refers to Inbox.
for folder in outlook.Folders:
print(folder.Name)
use the above for loop to look for all the folders in your mailbox.
For question 3:
To get sender email ID, you can use this piece of code:
messages = inbox.Items
message = messages.GetFirst()
sender_emailid =message.SenderEmailAddress
For question 1:
I dont have an answer. Sorry