Page 1 of 1

Compose an Outlook Email with an embedded picture

Posted: 13 Feb 2024, 13:25
by Thalon
As I needed it for work here is a short script that creates a new mail in outlook with some HTML text and an embedded picture:

Code: Select all

#Requires AutoHotkey v2.0
#SingleInstance Force

UniquePictureId := "MyUniquePictureName"
PathToPicture := A_ScriptDir "\P01132.jpg"

olMailItem := 0
o:= ComObjActive("Outlook.Application").Session()
MailItem := ComObjActive("Outlook.Application").CreateItem(olMailItem)
MailItem.TO :="recipient@email.com" 
; MailItem.CC :="" 
; MailItem.Replyto :="your@email.com" 
MailItem.Subject := "New employees"
MailItem.HTMLBody := "<html><body>This is our new employee Thomas:<br><img src='cid:" UniquePictureId "'></body></html>"

attachment := MailItem.Attachments.Add(PathToPicture,1,1,UniquePictureId)
attachment.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001F", UniquePictureId)    ;Important - Sets the CID-Tag for the attachment to be able to embed it

MailItem.Display(true)
; MailItem.Close(0) ; Creates a draft
; MailItem.Send() ;Send email
return
The mail will remain open for you to edit it, but could be sent automatically or saved as a draft (see commented lines at the end).



--- History ---
19th of February 2024:
Replaced property 0x3712001E by 0x3712001F for some edge cases.

Re: Compose an Outlook Email with an embedded picture

Posted: 13 Feb 2024, 14:01
by flyingDman
Nice. Thank you.

Re: Compose an Outlook Email with an embedded picture

Posted: 15 Feb 2024, 20:43
by FanaticGuru
Thalon wrote:
13 Feb 2024, 13:25

Code: Select all

#Requires AutoHotkey v2.0
#SingleInstance Force

UniquePictureId := "MyUniquePictureName"
PathToPicture := A_ScriptDir "\P01132.jpg"

olMailItem := 0
o:= ComObjActive("Outlook.Application").Session()
MailItem := ComObjActive("Outlook.Application").CreateItem(olMailItem)
MailItem.TO :="recipient@email.com" 
; MailItem.CC :="" 
; MailItem.Replyto :="your@email.com" 
MailItem.Subject := "New employees"
MailItem.HTMLBody := "<html><body>This is our new employee Thomas:<br><img src='cid:" UniquePictureId "'></body></html>"

attachment := MailItem.Attachments.Add(PathToPicture,1,1,UniquePictureId)
attachment.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001E", UniquePictureId)    ;Important - Sets the CID-Tag for the attachment to be able to embed it

MailItem.Display(true)
; MailItem.Close(0) ; Creates a draft
; MailItem.Send() ;Send email
return

Not that it hardly matters but the proptag should probably be 0x3712001F with an F at the end instead of E. It is just a slight difference in data type that probably will not matter in most cases but it could.

https://learn.microsoft.com/en-us/openspecs/exchange_server_protocols/ms-oxprops/9643fc31-3d26-45c0-b639-b08969fcd267

I only bring it up because I have played around with Outlook property tags quite a bit lately seeing what all I can find in them.

Also, you don't seem to have to give the embedded picture a name if you don't want. The HTML will embedded the picture without you specifically giving it a name. At least in my testing, you can just do something like '<img src="' A_Desktop '\Test\TestPicture.jpg" />' and Outlook will create a cid if you don't mind it being semi-random, the same as if you pasted a picture into an email.

FG

Re: Compose an Outlook Email with an embedded picture

Posted: 19 Feb 2024, 02:57
by Thalon
FanaticGuru wrote:
15 Feb 2024, 20:43
Not that it hardly matters but the proptag should probably be 0x3712001F with an F at the end instead of E. It is just a slight difference in data type that probably will not matter in most cases but it could.
Thanks for your great input! I've tested it and I'll update the script.
FanaticGuru wrote:
15 Feb 2024, 20:43
Also, you don't seem to have to give the embedded picture a name if you don't want. The HTML will embedded the picture without you specifically giving it a name. At least in my testing, you can just do something like '<img src="' A_Desktop '\Test\TestPicture.jpg" />' and Outlook will create a cid if you don't mind it being semi-random, the same as if you pasted a picture into an email.

FG
I've tried several combinations, but I don't get this to work.
Could you give me an example how to update those three lines?

Code: Select all

MailItem.HTMLBody := "<html><body>This is our new employee Thomas:<br><img src='cid:" UniquePictureId "'></body></html>"

attachment := MailItem.Attachments.Add(PathToPicture,1,1,UniquePictureId)
attachment.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001E", UniquePictureId)