I'm trying to determine where this issue is coming from. The overwhelming majority of emails are able to process correctly and don't put up any errors. I've looked around for this error and I've seen people having issues with outlook, but not just pulling from a PST file.
def enumerate_folders(FolderObj, mymode):
for ChildFolder in FolderObj.Folders:
enumerate_folders(ChildFolder, mymode)
iterate_messages(FolderObj, mymode)
def iterate_messages(FolderObj, mymode):
global mycounter2
for item in FolderObj.Items:
try:
body_content = item.HTMLbody
writeToFile(body_content, mypath)
mycounter2 = mycounter2 + 1
except AttributeError:
pass
def writeToFile(messageHTML, path):
global mycounter2
filename = '\htmloutput' + str(mycounter2) + '.html'
file = open(path + filename, "x")
try:
file.write(messageHTML)
except UnicodeEncodeError:
altText = str(messageHTML.encode('ascii', 'ignore'))
file.write(altText)
#print("Hit unicode error, trying alternate format at output " + str(mycounter2))
outlook.AddStore(pst)
PSTFolderObj = find_pst_folder(outlook, pst)
try:
enumerate_folders(PSTFolderObj, whatmode)
except Exception as exc:
print(exc)
The issue was related to the users Outlook encryption certificate, not with the code. The issue stemmed from a user recently changing computers, and no longer having their certificate for encrypted emails on their computer.
I discovered what the issue was when I unpacked the PST in my own Outlook and received the exact same exception as when I ran it through my code. This lead me to ask them if they were able to open up the email in their own instance of Outlook, and they could not. They then explained they had recently changed computers and no longer had their certificate on the computer.
Once they re-added the certificate and exported the PST once again, we were able to run it through the application successfully.