Chinmoy Pathak
Chinmoy Pathak

Reputation: 31

How to extract the date of the received emails in Outlook using Python scripting?

I am new to Python and I developed a Python script to ping messages in an application called Spark. So far, the code is fine and I managed to generate the subject of the emails. But I also want to add the date when I received the email along with the subject. Here is the script :-

import win32com.client
import requests
import time
import datetime
def postMessageInSpark(mytoken, roomId, text):
    header = {'Authorization':mytoken, 'Content-Type':'application/json'}
    payload = {'roomId':roomId,'text':text}
    result = requests.post(url='https://api.ciscospark.com/v1/messages',headers=header,json=payload)
    print(str(result.status_code))
    return str(result.status_code)
token ="Bearer "+'OGU3MDcyMmYtZDUzYS00OWU4LTk0ZTItYTJlNTc1OTUzODA5ZmQzYmRkNjMtYzcy'
teamId='Y2lzY29zcGFyazovL3VzL1JPT00vNGZjYWI5NzAtZGNhMS0xMWU2LWE3ODItYTM1OTY2OWNkMzcx'
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder(6)
folders = inbox.Folders
for folder in folders:
 if (folder.Name == 'TES FOLDER'):
  items = folder.Items
  for item in items:
   subject=item.Subject
   message=item.GetLast()
   date=message.senton.date()  
   for idx in reversed(items):
    fail_msg=subject+date
    postMessageInSpark(token,teamId,fail_msg)
    break

I used date=message.senton.date() but it doesn't return me the date of the emails. Please help me.

Upvotes: 1

Views: 8101

Answers (2)

Chinmoy Pathak
Chinmoy Pathak

Reputation: 31

Hi I found a solution to my problem:-

import win32com.client
import requests
import time
import datetime
def postMessageInSpark(mytoken, roomId, text):
    header = {'Authorization':mytoken, 'Content-Type':'application/json'}
    payload = {'roomId':roomId,'text':text}
    result = requests.post(url='https://api.ciscospark.com/v1/messages',headers=header,json=payload)
    print(str(result.status_code))
    return str(result.status_code)
token ="Bearer "+'OGU3MDcyMmYtZDUzYS00OWU4LTk0ZTItYTJlNTc1OTUzODA5ZmQzYmRkNjMtYzcy'
teamId='Y2lzY29zcGFyazovL3VzL1JPT00vNGZjYWI5NzAtZGNhMS0xMWU2LWE3ODItYTM1OTY2OWNkMzcx'
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder(6)
folders = inbox.Folders
for folder in folders:
 if (folder.Name == 'TES FOLDER'):
  items = folder.Items
  for item in reversed(items):
   subject=item.Subject
   date = item.SentOn.strftime("%d-%m-%y")
   for idx in reversed(items):
    fail_msg=subject+" at "+date
    postMessageInSpark(token,teamId,fail_msg)
    idx.Move(inbox.Folders("TEST"))
    break

Actually the message=item.GetLast() and date=message.senton.date() was only giving me the output of the last email received. But after I used date = item.SentOn.strftime("%d-%m-%y") it gave me the output I desired.

Upvotes: 2

Dmitry Streblechenko
Dmitry Streblechenko

Reputation: 66215

MailItem object does not have a method called GetLast. Only Items object does. Did you mean to use item.SentOn property?

Upvotes: 1

Related Questions