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.