+ 1

Python Outlook Mail Organizer

Read emails (with Loop?) and if the duplicate received numbers appear in the inbox, move them to the corresponding Outlook folder. Reason: As soon as an order is placed, we receive confirmation that the order has gone through. Hours later we receive the corresponding order confirmation. In both e-mails, the order no. is visible, as well as the quota. As soon as we receive both emails, it is an indication that the order went through properly and the both mails can be moved to the corresponding Outlook folder. This process should be automated by Python.

24th Sep 2022, 7:29 AM
Christos Tsiparidis
Christos Tsiparidis - avatar
31 Antworten
+ 2
it's like web crawling, but for emails.
4th Oct 2022, 11:08 AM
Bob_Li
Bob_Li - avatar
+ 1
Thanks for the link Bob but this is not what I was searching. My Code is running smoothly and without any problems but the requirement is not met in this case. Here my code: https://code.sololearn.com/c57OMkazBX16/?ref=app The confirmation numbers must be intercepted. As soon as there are duplicate confirmation numbers, the move to the folder should take place. My code, on the other hand, only checks whether both e-mails match each other so that I use in this case the count method. I suspect that the pattern method must be used here. But unfortunately I don't know how to read the Message.Subject with pattern.
24th Sep 2022, 8:42 PM
Christos Tsiparidis
Christos Tsiparidis - avatar
+ 1
Is the order number in the message.subject or in the message.body? maybe you should extract the order numbers and store them in a python set to keep a record of all unique numbers and use that as the basis for comparison. The emails might not be consecutive.
25th Sep 2022, 1:01 AM
Bob_Li
Bob_Li - avatar
+ 1
#create a set to store quote numbers qnums = set() str1 = "Order Acknowledgement Notification for 291975000 and PO Number: 6300096574-Q-291975000-PSRT-11128915_19000_REN" #extract quote number from order acknowledgement for w in str1.split(): if w.isdigit(): qnums.add(w) #print(w) str2 = "Order Completion Notification for 291975000 and PO Number: 6300096574-Q-291975000-PSRT-11128915_19000_REN" for qn in qnums: if qn in str2.split(): print(True) else: print(False) str3 = "Sales Order Acknowledgement (PO#6300096574-Q-291975000-PSRT-11128915_19000_REN)" for qn in qnums: if qn in str3.split("-"): print(True) else: print(False)
25th Sep 2022, 10:03 AM
Bob_Li
Bob_Li - avatar
+ 1
instead of print, put your actions instead and maybe you don't even need the else statement. the qnums set can be deleted on exit or maybe you would want to store them. it's up to you.
25th Sep 2022, 10:44 AM
Bob_Li
Bob_Li - avatar
+ 1
Thanks Bob! I will try to integrate this code and I'll let you know when the requirement has been met 👍
25th Sep 2022, 12:57 PM
Christos Tsiparidis
Christos Tsiparidis - avatar
+ 1
also, in building your confirmation list, you could first filter out those that are read and those that are not read first. That way, you only work with the already read mails.
25th Sep 2022, 1:08 PM
Bob_Li
Bob_Li - avatar
+ 1
small tip which might or might not be useful when you are building your confirmation list: you can use multiple strings in the startswith() method if you pass them as a tuple... s.startswith(('a', 'b', 'c')) then the list willl consist of all words starting with a or b or c. confirmation = [message for message in messages if message.subject.startswith(('Order Completion Notification', 'Sales Order Acknowledgement'))] if you want to put them in the same list.
25th Sep 2022, 1:17 PM
Bob_Li
Bob_Li - avatar
+ 1
The uppercase letter from subject to Subject is also not working. Too bad, I will keep trying to find a solution. Thank you Bob very much for your prompt replies.
26th Sep 2022, 11:08 AM
Christos Tsiparidis
Christos Tsiparidis - avatar
+ 1
Sorry I cannot be of further help. I don't have Outlook installed and your emails are only accessible by you. I would suggest reading up on the win32com documentation to find other functionalities that might be useful to you.
26th Sep 2022, 12:25 PM
Bob_Li
Bob_Li - avatar
+ 1
Hey Bob, I could figure out the problem. In this way I had to convert the expression to String so that it works and also shows me a print output. e.g.: confirmation3 = [message for message in inbox.Items if message.subject.startswith('Order Completion Notification')] for message3 in confirmation3: confirmation3 = str(message3) print(confirmation3) Now some exceptions need to be worked out because e.g. sometimes the quote number doesn't appear in the subject of Sales Order Acknowledgment. The Set method could be used very effectively here and I thank you very much for your help. Here is my updated coding: https://code.sololearn.com/cBWh8X3CkVtF/?ref=app Maybe it could help someone to organize the Outlook mails.
4th Oct 2022, 10:03 AM
Christos Tsiparidis
Christos Tsiparidis - avatar
+ 1
This looks awesome Bob, I'll try to integrate this code. Thank you so much
4th Oct 2022, 9:45 PM
Christos Tsiparidis
Christos Tsiparidis - avatar
0
Here is exact data from message.subject: First Mail - message.subject Order Acknowledgement Notification for 291975000 and PO Number: 6300096574-Q-291975000-PSRT-11128915_19000_REN Second Mail - message.subject Order Completion Notification for 291975000 and PO Number: 6300096574-Q-291975000-PSRT-11128915_19000_REN Third Mail - message.subject Sales Order Acknowledgement (PO#6300096574-Q-291975000-PSRT-11128915_19000_REN) In this case the Quote Number 291975000 should be identified. When all three Mails with the Quote Number 291975000 arrive, only then should these emails moved to the corresponding Outlook folder. I think, the idea to extract and store these numbers in a python set to keep a record and so on is the way to handle this case. What would the code look like in this situation? Many thanks in advance for your feedback
25th Sep 2022, 9:18 AM
Christos Tsiparidis
Christos Tsiparidis - avatar
0
Hey Bob, unfortunately the below code is not working: ______________________________________________ import win32com.client as client outlook = client.Dispatch('Outlook.Application') namespace = outlook.GetNameSpace('MAPI') account = namespace.Folders['user.name@company.com'] inbox = account.Folders['Inbox'] x = account.Folders['Order Confirmatiions 20XX'] qnums = set() confirmation1 = [message for message in inbox.Items if message.subject.startswith('Order Acknowledgement Notification')] for message in confirmation1.split(): if message.isdigit(): qnums.add(message) print(message) confirmation2 = [message for message in inbox.Items if message.subject.startswith('Order Completion Notification')] for message in qnums: if message in confirmation2.split(): print(True) else: print(False) confirmation3 = [message for message in inbox.Items if message.subject.startswith('Sales Order Acknowledgement')] for message in qnums: if message in confirmation3.split("-"): print(True) else: print(False) ____________________________________________ I get the following error message: AttributeError: 'list' object has no attribute 'split' When it is only a string like: str1 = "Order Acknowledgement Notification for 291975000 and PO Number: 6300096574-Q-291975000-PSRT-11128915_19000_REN" the code works correctly but not when I'm using the below line: confirmation1 = [message for message in inbox.Items if message.subject.startswith('Order Acknowledgement Notification')] In this way the split method is not recognized here because this is not like a string. I don't know if you can help me with this. I really appreciate the information I have already received from you. Thank you very much
26th Sep 2022, 7:24 AM
Christos Tsiparidis
Christos Tsiparidis - avatar
0
Christos Tsiparidis try this: confirmation1 = [message for message in inbox.Items if message.subject.startswith('Order Acknowledgement Notification')] for message in confirmation1: if message.subject.split().isdigit(): qnums.add(message) print(message) confirmation2 = [message for message in inbox.Items if message.subject.startswith('Order Completion Notification')] for message in confirmation2: for qn in qnums: if qn in message.subject.split(): print(True) else: print(False) confirmation3 = [message for message in inbox.Items if message.subject.startswith('Sales Order Acknowledgement')] for message in confirmation3: for qn in qnums: if qn in message.subject.split("-"): print(True) else: print(False)
26th Sep 2022, 8:13 AM
Bob_Li
Bob_Li - avatar
0
use message.subject.split().isdigit() to build your quote number set. if the print are working. then you can replace them with your actual commands.
26th Sep 2022, 8:14 AM
Bob_Li
Bob_Li - avatar
0
Again the same error message: AttributeError: 'list' object has no attribute 'split' See below code: import win32com.client as client outlook = client.Dispatch('Outlook.Application') namespace = outlook.GetNameSpace('MAPI') account = namespace.Folders['user.name@company.com'] inbox = account.Folders['Inbox'] x = account.Folders['Order Confirmations 20XX'] messages = inbox.Items messages.Sort("[ReceivedTime]", True) qnums = set() confirmation1 = [message.subject for message in inbox.Items if message.subject.startswith('Order Acknowledgement Notification')] for message in confirmation1.split(): if message.isdigit(): qnums.add(message) print(message) confirmation2 = [message.subject for message in inbox.Items if message.subject.startswith('Order Completion Notification')] for message in qnums: if message in confirmation2.split(): print(True) else: print(False) confirmation3 = [message.subject for message in inbox.Items if message.subject.startswith('Sales Order Acknowledgement')] for message in qnums: if message in confirmation3.split("-"): print(True) else: print(False)
26th Sep 2022, 8:43 AM
Christos Tsiparidis
Christos Tsiparidis - avatar
0
This code is also not working and I don't get any print. Again the same error message: if message.subject.split().isdigit(): AttributeError: 'list' object has no attribute 'isdigit' see the complete code: import win32com.client as client outlook = client.Dispatch('Outlook.Application') namespace = outlook.GetNameSpace('MAPI') account = namespace.Folders['user.name@company.com'] inbox = account.Folders['Inbox'] x = account.Folders['Order Confirmations 20XX'] messages = inbox.Items messages.Sort("[ReceivedTime]", True) qnums = set() confirmation1 = [message for message in inbox.Items if message.subject.startswith('Order Acknowledgement Notification')] for message in confirmation1: if message.subject.split().isdigit(): qnums.add(message) print(message) confirmation2 = [message for message in inbox.Items if message.subject.startswith('Order Completion Notification')] for message in confirmation2: for qn in qnums: if qn in message.subject.split(): print(True) else: print(False) confirmation3 = [message for message in inbox.Items if message.subject.startswith('Sales Order Acknowledgement')] for message in confirmation3: for qn in qnums: if qn in message.subject.split("-"): print(True) else: print(False)
26th Sep 2022, 8:59 AM
Christos Tsiparidis
Christos Tsiparidis - avatar
0
very strange. comment out everything below confirmation1 = [message for message in inbox.Items.......] and just do print(confirmation1) to see what kind of items are stored in your list
26th Sep 2022, 9:09 AM
Bob_Li
Bob_Li - avatar
0
Code: confirmation1 = [message for message in inbox.Items if message.subject.startswith('Order Acknowledgement Notification')] print(confirmation1) This is the output: [<COMObject <unknown>>, <COMObject <unknown>>, <COMObject <unknown>>, <COMObject <unknown>>] But with this code: confirmation = [message for message in inbox.Items if message.subject.startswith('Order Acknowledgement')] for message in confirmation: print(message) I get the requested output: Order Acknowledgement Notification for 271976533 and PO Number: 6300096593-Q-271976533-PSRT-11184730_250_NEW Order Acknowledgement Notification for 221976807 and PO Number: 6300096595-Q-221976807-PSRT-11197266_200_REN Order Acknowledgement Notification for 211978146 and PO Number: 6300096597-Q-211978146-SNT-11157054_400_NEW Order Acknowledgement Notification for 201978079 and PO Number: No PO-Q-201978079-PSRT-11167160_500, 600_NEW
26th Sep 2022, 9:49 AM
Christos Tsiparidis
Christos Tsiparidis - avatar