0

There are these bouncing (mail delivery) emails and I should save their body in a csv file, but my problem is when it saved Asian punctuation is on the body.

Sample:

This is what i see on csv: 格浴㹬格慥㹤਍洼瑥⁡瑨灴攭畱癩∽潃瑮湥⵴祔数•潣瑮湥㵴琢硥⽴瑨汭※档牡敳㵴獩ⵯ㠸㤵㈭㸢⼼敨摡㰾潢祤ാ㰊㹰戼㰾潦瑮挠汯牯∽〣〰㘰∶猠穩㵥㌢•慦散∽牁慩≬䄾⁺竼湥瑥欠竩敢敳渠浥猠歩牥泼⁴⁡敶歴穥⃵穭瑥整慶祧挠潳潰瑲歯猠慲㰺是湯㹴⼼㹢⼼㹰਍昼湯⁴潣潬㵲⌢〰〰〰•楳敺∽∲映捡㵥吢桡浯≡㰾㹰愼栠敲㵦洢楡瑬㩯異歳獡敮琮浩䁩敭挮浯㸢異歳獡敮琮浩䁩敭挮浯⼼㹡戼㹲਍⼼潦瑮ാ㰊潦瑮挠汯牯∽〣〰〰∰猠穩㵥㌢•慦散∽牁慩≬䄾⁺竼湥瑥⁥敮敬瑴欠竩敢癴ⱥ洠牥⁴⁡穭瑥⁴ⵥ慭汩猠潺杬泡慴慪攠畬慴潴瑴⹡戼㹲਍⼼潦瑮ാ㰊潦瑮挠汯牯∽〣〰〰∰猠穩㵥㈢•慦散∽慔潨慭㸢戼㹲਍⼼㹰਍瀼䄾欠盶瑥敫猠敺癲穥瑥攠畬慴潴瑴⁡穡ﰠ敺敮整㩴朠慭

This is what I see in outlook: Az üzenet kézbesítése nem sikerült a következő címzettek vagy csoportok számára: ****** Az üzenete nem lett kézbesítve, mert a címzett e-mail szolgáltatója elutasította.

import win32com.client
import datetime
import pytz
import csv
from tqdm import tqdm

outlook = win32com.client.Dispatch("Outlook.Application")
active_explorer = outlook.ActiveExplorer()
inbox = active_explorer.CurrentFolder
tz = pytz.timezone('Europe/Budapest')
start_date = tz.localize(datetime.datetime(2023, 2, 1, 10, 52, 0)) 
end_date = tz.localize(datetime.datetime(2023, 5, 9, 20, 58, 0)) 
restriction = f"[ReceivedTime] >= '{start_date.strftime('%m/%d/%Y %H:%M %p')}' AND [ReceivedTime] < '{end_date.strftime('%m/%d/%Y %H:%M %p')}'"
filtered_items = inbox.Items.Restrict(restriction)


with open(r"D:\Fejlesztések\riport" + ".csv", "w", newline="", encoding="utf-8") as csvfile:
    writer = csv.writer(csvfile)
    writer.writerow(["Beérkezési dátum", "Tárgy", "sikeres/nem sikeres", "Tartalma"])

    for message in tqdm (filtered_items, desc="Dolgozom..."):
        try: 
            row = [message.CreationTime.strftime("%Y-%m-%d %H:%M:%S"), message.Subject, "siker", message.Body]
            writer.writerow(row)       
        except Exception as e:
            row = [message.CreationTime.strftime("%Y-%m-%d %H:%M:%S"), message.Subject, "nem sikeres", message.Body]
            writer.writerow(row)

I tried the .HTMLBody but I got AttributeError. I tired different encodings but still not works.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
  • 1
    Please put sample data either in quote block or in code block. It's confusing to readers what is the question and what is data. – Dharman May 09 '23 at 11:24
  • See if this provides any usable ideas https://stackoverflow.com/questions/29040321/outlook-reportitem-body-returning-messed-up-encoding. If so, post an answer. – niton May 09 '23 at 13:32

2 Answers2

1

It's been a problem in Outlook for years - if you touch an NDR with Outlook Object Model, its Body property will be garbled.

The report text is stored in various MAPI recipient properties. The problem is the ReportItem object does not expose the Recipients collection. The workaround is to either use Extended MAPI (C++ or Delphi) or Redemption (I am its author - any language) - its RDOReportItem.ReportText property does not have this problem (VB):

set oItem = Application.ActiveExplorer.Selection(1)
set oSession = CreateObject("Redemption.RDOSession")
oSession.MAPIOBJECT = Application.Session.MAPIOBJECT
set rItem = oSession.GetRDOObjectFromOutlookObject(YourOutlookItem)
MsgBox rItem.ReportText
Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78
0

First, an Outlook folder may contain different kind of items, so when you iterate over all items found in the folder:

for message in tqdm (filtered_items, desc="Dolgozom..."):

Not only mail items can be included to the filtered_items collection. To make sure that you deal with mail items only you need to check the item type or check the MessageClass property value.

Second, it makes sense to print out the Body property value to make sure that you get a valid string - to find out whether the issue is related to writing to the csv file or comes from the Outlook object model.

I tried the .HTMLBody but I got AttributeError.

Not all items provide the HTMLBody property. Make sure that you deal with a mail item in the code at runtime.


Also I'd suggest trying to run the code in Outlook VBA environment to make sure the issue is not related to security issues when automating Outlook from external applications. The fact is that the Outlook object model generates security issues or give security prompts to users when protected properties or methods are called using automation.

The HTMLBody property may fire an exception when you try to automate Outlook. In this case most probably you are faced with an Outlook security issue. It can also be a prompt issued by Outlook if you try to access any protected property or method. But in your case that can be an exception or error. You get the security prompts/exceptions because Outlook is configured on the client computer in one of the following ways:

  • Uses the default Outlook security settings (that is, no Group Policy set up)
  • Uses security settings defined by Group Policy but does not have programmatic access policy applied
  • Uses security settings defined by Group Policy which is set to warn when the antivirus software is inactive or out of date

You can create a group policy to prevent security prompts from displaying if any up-to-date antivirus software is installed on the system or just turn these warning off (which is not really recommended).

Read more about that in the Security Behavior of the Outlook Object Model article.

Also you may consider using a low-level code on which Outlook is built and which doesn't give security issues - Extended MAPI. Consider using any third-party wrappers around that API such as Redemption.

Another option would be the Outlook Security Manager which allows suppressing Outlook security issues at runtime on the fly.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45