cancel
Showing results for 
Search instead for 
Did you mean: 

sending mail with images via xsjs

namrata_d
Active Participant
0 Kudos

Hi,

I am using SMTP relay server to send mails via XSJS project. I am able to send mails with HTML content and normal text type. However now i also want to embed images in the mails. I am using typical HTML format <img width=500 height=100 src=cid:Capture>", however it is not working.When the mail is received there is error shown in space of image : The linked image cannot be displayed.

Any pointers on how to send mails using xsjs using HTML content with embedded images.

pfefferf
Active Contributor
0 Kudos

Please can you share how to create the parts of the mail (the image file part and the html part).

Thx,
Florian

namrata_d
Active Participant
0 Kudos

Below is the HTML content of the mail with the image embedded :

var mailContent = "<html><body><img width=500 height=100 src=cid:Capture.PNG><p>Test email,</p> </body></html>";<I have placed the image with name Capture.png in the XS project folder directly>

Below is the code for sending the mail :

// Create an attachment $.net.Mail.Part from JSObject.

var Attachment = new $.net.Mail.Part({ type: $.net.Mail.Part.TYPE_ATTACHMENT,

data: <Here i have converted the image into binary format which is long queues of 0s and 1s and passing as string>

contentType: "image/png",

fileName: "Capture.PNG",

fileNameEncoding: "UTF-8" });

var HTMLContent= new $.net.Mail.Part();

HTMLContent.type = $.net.Mail.Part.TYPE_TEXT;

HTMLContent.text = bodyContent;

HTMLContent.contentType = "text/html";

HTMLContent.encoding = "UTF-8";

var mail = new $.net.Mail({ sender: {address: "namrata.dixit@xyz.com"}, to: [{ address: "abc@xys.com}], subject: mainContent, subjectEncoding: "UTF-8" }); mail.parts.push(Attachment,HTMLContent);

Regards,

Namrata

Accepted Solutions (0)

Answers (1)

Answers (1)

pfefferf
Active Contributor
0 Kudos

Instead of an attachment you should create an mail part of type $.net.Mail.Part.TYPE_INLINE in which you can define the Content ID you refer in the HTML part.

You can check the example in the documentation: https://help.sap.com/http.svc/rc/3de842783af24336b6305a3c0223a369/2.0.01/en-US/$.net.Mail.Part.html

Please enclose the value of the image src attribute also in double quotes to avoid any issues (e.g. src="cid:CAPUTURE"). Of course you need to adjust the value to contain the defined content id in the inline part.

Regards,
Florian

namrata_d
Active Participant
0 Kudos

Thanks . i will follow the same. But what should i pass in the data1 value for the property "data" :

var firstPart =new $.net.Mail.Part();

firstPart.type = $.net.Mail.Part.TYPE_INLINE;

firstPart.data = data1;// data1 contains the binary data of the image firstPart.contentType ="image/jpg"; firstPart.contentId ="IMAGE1_ID"; firstPart.fileName ="fileName1.jpg"; firstPart.fileNameEncoding ="UTF-8";

Regards,

Namrata

pfefferf
Active Contributor
0 Kudos

The "data" property of the inline part has to contain the image data in binary format (the same as you do it already for your current attachment part).

namrata_d
Active Participant
0 Kudos

The value that i am passing in property data is like

data:firstPart.data = "1000000000000011111111111111111111111111111111111111111111111111111111111111111111111111111111111111"+ "1000000000000011111111111111111111111111111111111111111111111111111111111111111111111111111111111111"+ "1000010010000011111111111111111111111111111111111111111111111111111111111111111111111111111111111111"+ "1000011111000011111111111111111111111111111111111111111111111111111111111111111111111111111111111111"+ "1000000111000011111111111111111111111111111111111111111111111111111111111111111111111111111111111111"+ "1000010111000011111111111111111111111111111111111111111111111111111111111111111111111111111111111111"+ "1000110111000011111111111111111111111111111111010111110111010100001010010111111111111111111111111111"+ "1000111111000011111111111111111111111111111111101101001111010001101001011111111111111111111111111111"+ "1001111111000011111111111111111111111111111111111111111111111111111111111111111111111111111111111111"+ "1111111111000011111111111111111111111111111111111111111111111111111111111111111111111111111111111111"+ "1111111111110011111111111111111111111111111111111111111111111111111111111111111111111111111111111111"+ "1111111111111011111111111111111111111111111111111111111111111111111111111111111111111111111111111111"+ "1111111111111011111111111111111111111111111111111111111111111111111111111111111111111111111111111111"+ "1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111";

I have got this binary data of the image from a 3rd party tool to convert image to binary format. Is this fine or should i be doing it another way.Thanks in advance.

pfefferf
Active Contributor
0 Kudos

Does not really look like a binary for an image.

Because you did not share anything about where your image data comes from, what initial format it has and how you convert it, no further advice can be given at the moment.

namrata_d
Active Participant
0 Kudos

the image i want to send as mail hear is company logo and i have the compage logo image saved in png format on local file system. and i have pasted this image in the xs project created. Can you please advice.

Later i used this site http://www.dcode.fr/binary-image to conver the local image into binary format ? is this way correct. i couldnt find any documentation on how to get binary data of the image. Any advice is appreciated.

pfefferf
Active Contributor

How do you read the image from the repo. I would assume that you get it already in the right format if you would use e.g. the file rest api or the objectslib xsjslib implementation (e.g. method getTarget of "sap.hana.xs.dt.base.server.objectsLib.xsjslib").

namrata_d
Active Participant
0 Kudos

Hi Florian,

Yes i am using "sap.hana.xs.dt.base.server", "utilsLib") library to fetch the image using below code :

var workspace = "_SYS_BIC\.\...\app\gsap\ZPACKAGE\Namrata\Mailfunctionality\";

//var activationMode = 0;

; var version = -1;

var active = null;

var conn = $.db.getConnection(8);

var session = utilsLib.getInactiveSession(conn,workspace);

//var zip = new $.util.Zip();

var objects = XSDS.$defineEntity("Packages", '"_SYS_REPO"."ACTIVE_OBJECT"');

var jsonOut = {};

jsonOut.Objects = objects.$query().$project({OBJECT_NAME : true}) .$where(objects.PACKAGE_ID.$like(app.gsap.ZPACKAGE.Namrata.Mailfunctionality') .$and(objects.OBJECT_NAME.$like('%'))).$order({by : objects.OBJECT_NAME}).$distinct().$execute();

var object = jsonOut.Objects[0].OBJECT_NAME;

var path = "app/gsap/ZPACKAGE/Namrata/Mailfunctionality" + object + ".jpg";

if (objectsLib.getTarget(path, version, session, active)) { return objectsLib.getTarget(path, version, session, active).bdata; }

However i am getting error here at utilsLib.getInactiveSession(conn,workspace); as it says workspace column name is incorrect. I am suspecting it is due to the workspace variable i have passed. Do you know the correct format in which workspace should be passed.

Regards,

Namrata

pfefferf
Active Contributor
0 Kudos

Have to tried it?with an empty workspace.

namrata_d
Active Participant
0 Kudos

Yes i have tried with empty workspace it gives below error :

InternalError: dberror(Connection.prepareStatement): 260 - invalid column name: UNDEFINED: line 1 col 220 (at pos 219)

namrata_d
Active Participant
0 Kudos

Resolved !!! Passed HANA server address in the workspace and it worked.