简体   繁体   中英

I'm trying to use HTML img tag in python to send a mail, but I'm getting the following error

    import win32com.client as win32
    outlook = win32.Dispatch('outlook.application')
    mail = outlook.CreateItem(0)
    mail.To = 'abc@gmail.com'
    mail.Subject = 'Test Mail'
    mail.Body = 'Test'
    mail.HTMLBody = '''<img src="C:\Users\tkakati\Desktop\Automated mail\books_read.png" alt="Plot 1">''' #this field is optional
mail.Send()

I've been trying to run this code but I'm getting the following error:

(unicode error) 'unicodeescape' codec can't decode bytes in position 437-438: truncated \UXXXXXXXX escape

您需要转义src的单个\\ ,最简单的方法是使用字符串文字r

mail.HTMLBody = r'''<img src="C:\Users\tkakati\Desktop\Automated mail\books_read.png" alt="Plot 1">'''

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM