Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send Emails using Python Win32. Adding Image to the Body of the Email is not working

Tags:

python-3.x

I am trying to add an Image to an Email Body.

I am using Outlook 16 and Python 3.7 to do this.

The mail gets sent from Mailbox.

Here is my code function to send email and how can I add an Image at the end of the email.

def send_email(sender,recipient):
    outlook = win32.Dispatch('outlook.application')
    mail = outlook.CreateItem(0)
    mail.To = recipient
    mail.Subject = Subject_Req
    mail.HTMLBody = Content_Email
    mail.SentOnBehalfOfName = sender
    mail.GetInspector 
    mail.Send()

The Image is present in my local network :- C:\Users\Sid\AppData\Roaming\Microsoft\Signatures\My Project\image001.png

The Image is nothing but the logo which I want to put at the last part of the HTML body.

So basically from the function it will be Content_Email + This image.

I tried some code, what it does is :- it sends an "X" mark at the end in place of the image itself to the recipient.

And when I send it to myself, It puts the "X" mark but I get an option to download pictures on right click and it gets the Image.

What I would like to do is, put the Image instead of the "X" for both the cases without the user having to have access to that Image.

How can I do this using Python. The solution in here seems to be working with VBA : https://www.experts-exchange.com/questions/26481413/Problem-inserting-HTML-images-into-the-body-of-a-messge.html

like image 968
Siddharth Thanga Mariappan Avatar asked Aug 09 '19 15:08

Siddharth Thanga Mariappan


People also ask

How do you insert an image in HTML using Python?

Use of <img>Tag The <img> tag specifies an image to be displayed in an HTML document.

How do you send an Outlook email as an attachment in Python?

Set up a secure connection using SMTP_SSL() and . starttls() Use Python's built-in smtplib library to send basic emails. Send emails with HTML content and attachments using the email package.


1 Answers

I managed to get this to work through attachment.PropertyAccessor.SetProperty function.

def send_email(sender,recipient):
    outlook = win32.Dispatch('outlook.application')
    mail = outlook.CreateItem(0)
    mail.To = recipient
    mail.Subject = Subject_Req
    attachment = mail.Attachments.Add(signatureimage)
    attachment.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001F", "MyId1")
    mail.HTMLBody = Content_Email + "<html><body><img src=""cid:MyId1""></body></html>"
    mail.SentOnBehalfOfName = sender
    mail.GetInspector 
    mail.Send()

#Change the Paths here, if ran from a different location
signatureimage = "C:\\Users\\Sid\\AppData\\Roaming\\Microsoft\\Signatures\\My Project\\image001.png"

This would mean, it will embed the Image and not link the Picture to a link. Linking to a picture expects the recipients to have access to that particular picture location.

Also, for a learning this link doesn't change at all :- http://schemas.microsoft.com/mapi/proptag/0x3712001F

like image 112
Siddharth Thanga Mariappan Avatar answered Sep 21 '22 03:09

Siddharth Thanga Mariappan