How to copy the entire outlook message content in python

nikhil k nikhilclt87 at gmail.com
Tue Dec 29 08:38:53 EST 2020


Hello All,
I'm a beginner trying to achieve the below in my python script: Can anyone help me on this?  I'm stuck at step2.

1. Input: A locally saved outlook mail (*.msg) path
2. Go to the path and Copy the entire body of the mail 
3. Create a new mail and paste the contents into new mail 
4. send it manually

# ========================================================
# =======================   Script Start ======================
# ========================================================
import win32com.client as win32  

########### Functions
def getMailBody(msgFile):
    start_text = "<html>"
    end_text = "</html>"
    with open(msgFile) as f:
        data=f.read()
    return data[data.find(start_text):data.find(end_text)+len(end_text)]

def releaseMail(body, subject, recipient):
    outlook = win32.Dispatch('outlook.application')
    mail = outlook.CreateItem(0)
    mail.To = recipient
    mail.Subject = subject
    mail.HtmlBody = body
    mail.Display(True)

############### Main ################
msgFile = "C:\\RELM\\testMsg.msg"
mailTo = "mymail at myserver.com"
mailSubject = "Test message"
mailBody = getMailBody(msgFile)
releaseMail(mailBody, mailSubject, mailRecipient)
# ========================================================
# ======================== Script End =======================  
# ========================================================


Below is the error I'm getting.
==============================================
  File "C:\Python\Python38-32\lib\encodings\cp1252.py", line 23, in decode
    return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 924: character maps to <undefined>




More information about the Python-list mailing list