Jump to content
Sign in to follow this  
Bam

Imaged embedded into an email

Recommended Posts

Bam

Kinda just need pointed in the right direction / can it be done with auto it.

Im looking for a way to embed an image into an email, I've done alot of searching and I guess the way to do it is with cid: after attaching the image to the email but Ive tried this with the outlook udfs already with no joy.

I found a python script that I found that does what I want but I never have used python until today.

I've also used a data URI (Im not sure what its called, this site can convert an image into text and then then you just insert the text into <img src="LengthyStringHereGeneratedBySite">), This does work if the email is opened with Thunderbird or other email clients but if opened with gmail theirs just a plank spot where the image should be.

It would be awesome if it could be done with out outlook but I still prefer using AutoIt with outlook vrs learning python.

Any help is appreciative.

Share this post


Link to post
Share on other sites
pieeater

here's an to an _INetSmtpMailCom() function made by jos with attatchment capabilitys


[spoiler]My UDFs: Login UDF[/spoiler]

Share this post


Link to post
Share on other sites
Bam

Thanks for the reply. Ive tried that before but Im not trying to add an image as a attachment Im trying to make it show up as part of the email.

Share this post


Link to post
Share on other sites
Bam

Well have jury rigged solution, using the python script and

$File = FileOpen(@ScriptDir & "\bam-mail.py")
$Data = FileRead($File)
FileClose($File)
$Data = StringReplace($Data, "#@email.com", "to@emailadress")
$File = FileOpen(@ScriptDir & "\bam-mailA.py", 2)
FileWrite($File, $Data)
FileClose($File)

$Run = RunWait("C:\Python32\python.exe C:\Users\Bam\Desktop\Python\bam-mailA.py","")
MsgBox(1,1,$Run)

Still open to any other AutoIt solutions.

Share this post


Link to post
Share on other sites
water

You could try my OutlookEX UDF (for download please see my signature).

Function _OL_ItemAttachmentAdd allows to specify the position where to insert the attachment if the mail body is of type RTF.

Example 3 in the _OL_ItemAttachmentAdd.au3 file will give you an idea how it could work.

I have to admit that I haven't yet made it working but according to the M$ docu it should be possible.

If you like I can do some testing.

Edit:

Can you run the Python script so we can see how the HTML mail looks like?

Edit 2:

I think I found an example that let's you do what you want using HTML. I will have to test it as soon as I'm in my office again.

Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites
Bam

Thanks for the response.

<b>Some <i>HTML</i> text</b> and an image.<br><img src="cid:image1"><br>Nifty!

is the html part, the cid:image1 part is what im mainly interested in. It seems to pull the picture from the attachments and displays the picture in the message with out having to view it in attachments or show remote content.

I also like that it includes a plain text version of the email.

Here's the source of the email if that's what you where looking for. (had to shorten the image data, was too long and the code tags didn't like it :mellow: )

Content-Type: multipart/related; boundary="===============1827817307=="
MIME-Version: 1.0
Subject: Subject
From: email@address.com
To: email@address.com
Message-ID: <################@####.#######.###>
Date: Fri, 19 Aug 2011 13:34:21 -0500 (CDT)
X-Sendgrid-EID: AH77f4+342/dscWd6jTxVQ6eWP508CuYzOjTWDheTB57KzaG05LaWcPnpnIq7zsmBdJNYYzUPRdGh66xDl6LTpwttsAvjcnSA+IXh/0jCRrnsdV3uYgcvDcpSybQhVLOi9uMOymGx9mehYPOAj+LcSl7cBfhnf8gPLxA1Ul+Brk=
Sender: Bam=##############@#######.###

This is a multi-part message in MIME format.
--===============1827817307==
Content-Type: multipart/alternative; boundary="===============1877621409=="
MIME-Version: 1.0

--===============1877621409==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit

This is the alternative plain text message.
--===============1877621409==
Content-Type: text/html; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit

<b>Some <i>HTML</i> text</b> and an image.<br><img src="cid:image1"><br>Nifty!
--===============1877621409==--

--===============1827817307==
Content-Type: image/png
MIME-Version: 1.0
Content-Transfer-Encoding: base64
Content-ID: <image1>

iVBORw0KGgoAAAANSUhEUgAAASwAAAELCAIAAADstiysAAAACXBIWXMAAAsS
yKomG8t9ZSFqVdWFngjpQqhbJJVZUU0c/QvZiEqKXcy/ywAAAABJRU5ErkJg
gg==

--===============1827817307==--

Share this post


Link to post
Share on other sites
water

The "cid:" thing is what I've found on the net too and what seems to be the solution. The problem is how to tell Outlook what to do!

Here is the sample code which I need to translate to AutoIt: http://help.wugnet.com/office/embed-image-Outlook-Message-VBA-ftopict1172044.html

Edit: Unfortunately this code example uses CDO which is no longer available on Outlook 2007 systems or later. It has to be downloaded and installed on every computer running Outlook.

Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites
water

Here is an example that works with my OutlookEX UDF. Make sure the file "The_Outlook.JPG" is in the same directory as the script.

#include <OutlookEx.au3>

$oOutlook = _OL_Open()
$oMail = _OL_ItemCreate($oOutlook, $olMailItem, "*", "", "Subject=TestMail", "BodyFormat=" & $olFormatHTML, _
    'HTMLBody=This is a picture.<img src="cid:The_Outlook.jpg">This is more text.')
If @error <> 0 Then Exit MsgBox(16, "OutlookEX UDF: _OL_ItemCreate Example Script", "Error creating a mail item. @error = " & @error & ", @extended = " & @extended)
$oItem = _OL_ItemAttachmentAdd($oOutlook, $oMail, Default, @ScriptDir & "\The_Outlook.JPG")
If @error <> 0 Then Exit MsgBox(16, "OutlookEX UDF: _OL_ItemCreate Example Script", "Error adding an attachment to the mail item. @error = "  & @error & ", @extended = " & @extended)
$oMail.Display
_OL_Close($oOutlook)

The same is valid for a background image. The code was found here at the end of the page.

Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites
Bam

When it displays the email it look awesome just they way it should but I cant seem to find a way to add an email address to send the email to and how to send it.

Also is there any to have a plain text version of the email with an html version? (I know the plain text wont be able to show the image but I'm just curious)

Share this post


Link to post
Share on other sites
water

Use _OL_ItemRecipientAdd to add a recipient and _OL_ItemSend to send the mail.

To create a plain text mail you have to use:

$oMail = _OL_ItemCreate($oOutlook, $olMailItem, "*", "", "Subject=TestMail", "BodyFormat=" & $olFormatPlain, 'Body=This is plain text.')

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites
KaFu

How about using this

to create a mail with CDO and using this

AddRelatedBodyPart http://msdn.microsoft.com/en-us/library/ms526569%28v=exchg.10%29.aspx

to add the resource?

Share this post


Link to post
Share on other sites
water

_INetSmtpMailCom() doesn't support embedded pictures and CDO is no longer a part of Office 2007 and 2010 (it can be downloaded separately but I don't know if it is supported any longer).

Edit: CDO is no longer supported for Outlook 2010.

Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites
KaFu

_INetSmtpMailCom() doesn't support embedded pictures

Yep, that's why I referred to the AddRelatedBodyPart method which needs to be in-cooperated into the _INetSmtpMailCom() function. Here's another snippet I found:

#cs
Const CdoReferenceTypeName = 1
Dim objCDO, objBP
Set objCDO = Server.CreateObject("CDO.Message")
objCDO.MimeFormatted = True
objCDO.To = "you@yourdomain.com"
objCDO.From = "you@yourdomain.com"
objCDO.Subject = "Embedded image demo"
objCDO.HTMLBody = "<html>Check this out: <img src=""cid:myimage.gif""></html>"

' Here's the good part, thanks to some little-known members.
' This is a BodyPart object, which represents a new part of the multipart MIME-formatted message.
' Note you can provide an image of ANY name as the source, and the second parameter essentially
' renames it to anything you want.  Great for giving sensible names to dynamically-generated images.
Set objBP = objCDO.AddRelatedBodyPart(Server.MapPath("/images/myimage.gif"), "myimage.gif", CdoReferenceTypeName)

' Now assign a MIME Content ID to the image body part.
' This is the key that was so hard to find, which makes it
' work in mail readers like Yahoo webmail & others that don't
' recognise the default way Microsoft adds it's part id's,
' leading to "broken" images in those readers.  Note the
' < and > surrounding the arbitrary id string.  This is what
' lets you have SRC="cid:myimage.gif" in the IMG tag.
objBP.Fields.Item("urn:schemas:mailheader:Content-ID") = "<myimage.gif>"
objBP.Fields.Update

objCDO.Send
#ce

and CDO is no longer a part of the Windows operating system (it can be downloaded separately but I don't know if it is supported any longer).

Really? Seems to work on my Win7-64bit (Ultimate) though.

Share this post


Link to post
Share on other sites
Bam

#include <OutlookEx.au3>

$Email = "email@address.w/e"

$oOutlook = _OL_Open()
$oMail = _OL_ItemCreate($oOutlook, $olMailItem, "*", "", "Subject=TestMail", "BodyFormat=" & $olFormatHTML, _
    'HTMLBody=This is a picture.<img src="cid:The_Outlook">This is more text.')
If @error <> 0 Then Exit MsgBox(16, "OutlookEX UDF: _OL_ItemCreate Example Script", "Error creating a mail item. @error = " & @error & ", @extended = " & @extended)
$oItem = _OL_ItemAttachmentAdd($oOutlook, $oMail, Default, @ScriptDir & "\The_Outlook.JPG")
If @error <> 0 Then Exit MsgBox(16, "OutlookEX UDF: _OL_ItemCreate Example Script", "Error adding an attachment to the mail item. @error = "  & @error & ", @extended = " & @extended)
_OL_ItemRecipientAdd($oOutlook, $oMail, Default,$olTo,$Email)

_OL_ItemSend($oOutlook, $oItem, Default)
_OL_Close($oOutlook)

I just tried the code above and when I received the email the picture didn't show up in the message but it was in attachments.

Email MIME (I think its the MIME) from script:

------=_NextPart_000_0007_01CC60D8.D75954A0
Content-Type: image/jpeg;
    name="The_Outlook.jpg"
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
    filename="The_Outlook.jpg"

I think the problem is it doesn't have the "Content-ID:" wich is in the source of the email from the python script.

--===============1827817307==
Content-Type: image/png
MIME-Version: 1.0
Content-Transfer-Encoding: base64
Content-ID: <image1>
Edited by Bam

Share this post


Link to post
Share on other sites
water

Edited post CDO is no longer part of Office 2007 and 2010 (not Windows as I have written before). Added a link for Office 2010 regarding support.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites
water

Hi Bam,

I've taken the code from here. Maybe it explains why you see the image as an attachment and not included.

Or you could try the 2nd example at the bottom of the page. Maybe this works more reliable.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites
TheBlacksheep

Hi - apologies for re-opening an old thread but I appear to have got this working on the _INetSmtpMailCom UDF function (which uses CDO and which will be deprecated at some point) - it maybe useful to someone;

Opt("MustDeclareVars", 1)

;##################################
; Include
;##################################
#include <Inet.au3>
#Include<file.au3>
;##################################
; Variables
;##################################
local $SmtpServer = "mail server name"       ; address for the smtp-server to use - REQUIRED
local $FromName = "from name"               ; name from who the email was sent
local $FromAddress = "from address"     ; address from where the mail should come
local $ToAddress = "from address";      ; destination address of the email - REQUIRED

local $CcAddress = ""      ; address for cc - leave blank if not needed
local $BccAddress = ""                        ; address for bcc - leave blank if not needed
local $Importance = "Normal"                  ; Send message priority: "High", "Normal", "Low"
local $Username = "username@server.com"       ; username for the account used from where the mail gets sent - REQUIRED
local $Password = "password"                 ; password for the account used from where the mail gets sent - REQUIRED
;$IPPort = 25                           ; port used for sending the mail

local $IPPort=465                          ; GMAIL port used for sending the mail
local $ssl=1                               ; GMAIL enables/disables secure socket layer sending - put to 1 if using httpS

local $Subject = "Embedded Image Test!!!"
local $Body = "<h1>Embedded Image test</h1>"
local $AttachFiles = "c:\image1.png;c:\image2.png;c:\image3.png"

Func _INetSmtpMailCom($s_SmtpServer, $s_FromName, $s_FromAddress, $s_ToAddress, $s_Subject = "", $as_Body = "", $s_AttachFiles = "", $s_CcAddress = "", $s_BccAddress = "", $s_Importance="Normal", $s_Username = "", $s_Password = "", $IPPort = 25, $ssl = 0, $embed = True)
    
    Local $objEmail = ObjCreate("CDO.Message")
    Local $objBP 
    Local $szDrive, $szDir, $szFName, $szExt

    $objEmail.From = '"' & $s_FromName & '" <' & $s_FromAddress & '>'
    $objEmail.To = $s_ToAddress
    Local $i_Error = 0
    Local $i_Error_desciption = ""
    If $s_CcAddress <> "" Then $objEmail.Cc = $s_CcAddress
    If $s_BccAddress <> "" Then $objEmail.Bcc = $s_BccAddress
    $objEmail.Subject = $s_Subject
   
    $objEmail.MimeFormatted = $embed
     
    If $s_AttachFiles <> "" Then
        Local $S_Files2Attach = StringSplit($s_AttachFiles, ";")
        For $x = 1 To $S_Files2Attach[0]
            $S_Files2Attach[$x] = _PathFull($S_Files2Attach[$x])
            If FileExists($S_Files2Attach[$x]) Then
               
               if $embed = True Then
                  _PathSplit($S_Files2Attach[$x], $szDrive, $szDir, $szFName, $szExt)
 
                  $as_Body &= "<img src=""cid:" & $szFName & $szExt & """>"     
                  $objBP = $objEmail.AddRelatedBodyPart($S_Files2Attach[$x], $szFName & $szExt, 1)
                  $objBP.Fields.Item("urn:schemas:mailheader:Content-ID") = "<" & $szFName & $szExt & ">"
                  $objBP.Fields.Update
                  
               Else   
                  $objEmail.AddAttachment($S_Files2Attach[$x])
                  
               EndIf
            Else
                ConsoleWrite('!> File not found to attach: ' & $S_Files2Attach[$x] & @LF)
                SetError(1)
                Return 0
            EndIf
        Next
    EndIf
    
    If (StringInStr($as_Body, "<") And StringInStr($as_Body, ">")) or ($embed = True) Then
        $objEmail.HTMLBody = $as_Body
    Else
        $objEmail.Textbody = $as_Body & @CRLF
    EndIf

    $objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
    $objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = $s_SmtpServer
    If Number($IPPort) = 0 then $IPPort = 25
    $objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = $IPPort
    ;Authenticated SMTP
    If $s_Username <> "" Then
        $objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
        $objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusername") = $s_Username
        $objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendpassword") = $s_Password
    EndIf
    If $ssl Then
        $objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
    EndIf
    ;Update settings
    $objEmail.Configuration.Fields.Update
    ; Set Email Importance
    Switch $s_Importance
        Case "High"
            $objEmail.Fields.Item ("urn:schemas:mailheader:Importance") = "High"
        Case "Normal"
            $objEmail.Fields.Item ("urn:schemas:mailheader:Importance") = "Normal"
        Case "Low"
            $objEmail.Fields.Item ("urn:schemas:mailheader:Importance") = "Low"
    EndSwitch
    $objEmail.Fields.Update
    ; Sent the Message
    $objEmail.Send
    If @error Then
        SetError(2)
        Return $oMyRet[1]
    EndIf
    $objEmail=""
EndFunc   ;==>_INetSmtpMailCom
 
_INetSmtpMailCom($SmtpServer, $FromName, $FromAddress, $ToAddress, $Subject, $Body, $AttachFiles, $CcAddress, $BccAddress, $Importance, $Username, $Password, $IPPort, $ssl, true)

Exit

just added the bits relevant to this url;

http://support.jodohost.com/threads/tut-how-to-add-embedded-images-in-cdo-mail.7692/

it automatically adds the inline images to the body of the email in the order they were attached - if this isn't what you want (because you need to control the html that's being generated) then comment out the bit where it adds to the body and add it manually outside the function.

TheBlackSheep

Share this post


Link to post
Share on other sites
Tjalve

I remade the _INetSmtpMailCom in (more or less) the same way as TheBlackSheep did. And everything works fine for ALL mailclients except for Apple. This is a problem since we use IOS devices. So is thaere a way to make this work on Apples mailclient?

Edited by Tjalve

Share this post


Link to post
Share on other sites
water

Hi Tjalve,

what do you mean by "does not work"?

Do you get an error message?

No pictures at all?

Wrong position of the picture?

...?


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×