Compose an Outlook Email with an embedded picture

Post your working scripts, libraries and tools.
Thalon
Posts: 19
Joined: 24 Nov 2016, 04:55

Compose an Outlook Email with an embedded picture

13 Feb 2024, 13:25

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.
Last edited by Thalon on 19 Feb 2024, 02:58, edited 1 time in total.
User avatar
flyingDman
Posts: 3012
Joined: 29 Sep 2013, 19:01

Re: Compose an Outlook Email with an embedded picture

13 Feb 2024, 14:01

Nice. Thank you.
14.3 & 1.3.7
User avatar
FanaticGuru
Posts: 1945
Joined: 30 Sep 2013, 22:25

Re: Compose an Outlook Email with an embedded picture

15 Feb 2024, 20:43

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
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
Thalon
Posts: 19
Joined: 24 Nov 2016, 04:55

Re: Compose an Outlook Email with an embedded picture

19 Feb 2024, 02:57

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)

Return to “Scripts and Functions (v2)”

Who is online

Users browsing this forum: No registered users and 32 guests