Ask a Question

How to insert an image into message body in TestComplete script with a help of SendEmail function?

jewelsmu
New Contributor

How to insert an image into message body in TestComplete script with a help of SendEmail function?

Hello! I'm creating an automated test script for a desktop application. After test finished, an image with test results is created.

 

After an image was created, it is put by means of  TestComplete script into a folder on a local PC(e.g. "C://image.png"). Then the script sends an email with the help of TestComplete function SendEmail(mFrom, mTo, mSubject, mBody, mAttach).

 

I need to include an image into a message body of SendEmail function (mBody) in such a way that this image would be displayed in message body in the email client. If I include a local path of to an image("<img src=C://image.png>"), then mail client sure doesn't understand where is an image situated and doesn't display an image in my email client.

That's how my SendEmail function is called in this case:

 

SendEmail("from@mail.com", "to@mail.com", "Subject", '<img src="C:/image.png" height="42" width="42">', "attachments");

 

Then I tried to include into message body an image from Internet(something like "<img src=http://www.site.com/image.png>"). In such case image can be displayed in message body in the email client.

That's how my SendEmail function is called in this case:

 

SendEmail("from@mail.com", "to@mail.com", "Subject", '<img src="http://www.site.com/image.png" height="42" width="42">', "attachments");

 

But in such a case, by means of my test script an image should be put from my local PC into some web site or hosting. Is it possible to put an image to a web site by means of TestComplete script?

 

 

Maybe there are more reasonable ways, or if there is the best way of how to include an image to a message body of SendEmail function - like converting an image into base64 etc. Maybe I chose an incorrect way of including an image into message body.

 

If you can just advice me a direction in which I have to move solving this task - I will be very pleased.

 

If my explanation is not clear - please ask me, I'll try to explain it better and give some pictures or more code. Thanks for any help in advance.

 

 

7 REPLIES 7
Ryan_Moran
Valued Contributor

You'll need compose an HTML email for the body and reference the attached image.

I.E. <img src="attachedimage.png">


''-Praise the sun and Give Kudos.''

You can have custom function to do this stuff as like below,

 

 

   function sendmail()
{
var theApp = new Sys.OleObject("Outlook.Application") var theMailItem = theApp.CreateItem(0) // value 0 = MailItem var msg ="<center><img src=" + chr(34) + "<image path>"+ chr(34) + " alt=" + chr(34) + "<Tooltip text>" + chr(34) + "style=" + chr(34) + "width:180px;height:120px;" + chr(34) + "></center>"; theMailItem.to = "<to address>" theMailItem.Subject = ("<SUB line>"); theMailItem.BodyFormat = 2; theMailItem.HTMLBody = (msg); theMailItem.Attachments.Add("<file path>"); theMailItem.display(); theMailItem.Send();

theApp =null;
}

 You can make alignments, tool tip, position and etc,. from below code

 

            var msg ="<center><img src=" + chr(34) + "<image path>"+ chr(34) +  " alt=" + chr(34) + "<Tooltip text>"  + chr(34) + "style=" + chr(34) + "width:180px;height:120px;" + chr(34) + "></center>";

Thanks
Shankar R

LinkedIn | CG-VAK Software | Bitbucket | shankarr.75@gmail.com

“You must expect great things from you, before you can do them”

Extension Available

@shankar_r, @Ryan_Moran :
> image would be displayed in message body in the email client.

As the image will be displayed on the remote client, I think that all images must be encoded and included in the message body so that they are accessible on the remote client.

Not sure about implementation complexity at the moment.

Regards,
  /Alex [Community Hero]
____
[Community Heroes] are not employed by SmartBear Software but
are just volunteers who have some experience with the tools by SmartBear Software
and a desire to help others. Posts made by [Community Heroes]
may differ from the official policies of SmartBear Software and should be treated
as the own private opinion of their authors and under no circumstances as an
official answer from SmartBear Software.
The [Community Hero] signature is used with permission by SmartBear Software.
https://community.smartbear.com/t5/custom/page/page-id/hall-of-fame
================================
tristaanogre
Esteemed Contributor

An option:

1) Write the image to your test log using Log.Picture

2) Export your test log at the end of your test to a local file

3) Attach the exported log to your e-mail

 

That would send the appropriate picture as well as have the bonus of including the rest of the test log.

 

Also, if you don't HAVE to have the picture embedded in the e-mail, this option is available to you.  Additionally, you could simply attach the image to the e-mail and not worry about it being embedded.

 

Another option:

@shankar_r has a library of code that writes test log information out to HTML.  Perhaps you could utilize something like that and write the output to an HTML file in a shared drive location.  Then, in your e-mail, simply send a link to that location in the body of the e-mail and the end user can click the link to view the results.

All this is, of course, contingent on whether or not the image MUST be embedded in the e-mail body.  If it is, then you're dealing with a complicated issue of embedding an image so that it is viewable on a non-local machine.


Robert Martin
[Hall of Fame]
Please consider giving a Kudo if I write good stuff
----

Why automate?  I do automated testing because there's only so much a human being can do and remain healthy.  Sleep is a requirement.  So, while people sleep, automation that I create does what I've described above in order to make sure that nothing gets past the final defense of the testing group.
I love good food, good books, good friends, and good fun.

Mysterious Gremlin Master
Vegas Thrill Rider
Extensions available

@AlexKaras, you are right.

 

I modified code the in-order to display the image in email body. (changes highlighted in RED)

 

function sendmail()
{
	var theApp = new Sys.OleObject("Outlook.Application")
	var theMailItem = theApp.CreateItem(0) // value 0 = MailItem
	var msg ="<center><img src=" + chr(34) + "cid:<image file name with extension>"+ chr(34) +  " alt=" + chr(34) + "<Tooltip text>"  + chr(34) + "style=" + chr(34) + "width:180px;height:120px;" + chr(34) + "></center>";

	theMailItem.to = "<to address>"
	theMailItem.Subject = ("<SUB line>");
	theMailItem.BodyFormat = 2;
	'add the image in hidden manner, position at 0 will make it hidden
	theMailItem.Attachments.Add("<image file path>", olByValue, 0);
	theMailItem.HTMLBody = (msg);
	theMailItem.display();
	theMailItem.Send();

	theApp =null;
}

Source : http://excel-macro.tutorialhorizon.com/excel-vba-send-mail-with-embedded-image-in-message-body-from-...

 


Thanks
Shankar R

LinkedIn | CG-VAK Software | Bitbucket | shankarr.75@gmail.com

“You must expect great things from you, before you can do them”

Extension Available

Hi Shankar,

 

Wow, looks awesome!

I'll definitely spend some time with your code to get a better understanding of its working internals.

And thank you for the link provided.

 

P.S. To create a mail using your approach, Outlook is required. Just wondering if you checked can the mail be read on the client where Outlook is absent? (E.g. using some mobile web browser.)
If not, then Robert's suggestion with attached exported TestComplete's log looks pretty promising as well. Though it requires IE's presence on the client to be able to display attached .mht log.

Regards,
  /Alex [Community Hero]
____
[Community Heroes] are not employed by SmartBear Software but
are just volunteers who have some experience with the tools by SmartBear Software
and a desire to help others. Posts made by [Community Heroes]
may differ from the official policies of SmartBear Software and should be treated
as the own private opinion of their authors and under no circumstances as an
official answer from SmartBear Software.
The [Community Hero] signature is used with permission by SmartBear Software.
https://community.smartbear.com/t5/custom/page/page-id/hall-of-fame
================================
jewelsmu
New Contributor

Thanks a lot everyone for your help. I solved my issue with a help of base64 encoder.

 

So, I encoded my image path into base64 with a help of corresponding web application, then copied encoded value and pasted it into my img src. Then I put my image into message body and after this finally I can see my image in body of an email message.

cancel
Showing results for 
Search instead for 
Did you mean: