1
votes

I'm using class GmailApp of google apps script to get body of mail and show it in html.

Body get ok but img of mail error:

ERR_UNKNOWN_URL_SCHEME.

Is there any way to retrieve the entire message body, including the image?

Code GS:

function GetBodyMail(){

  var thread = GmailApp.getThreadById("16cfd66654bb9593") ;
  var mgs = thread.getMessages();
  var mgsBody = mgs[0].getBody();

  return mgsBody;
  
}

console window: enter image description here

elements window of body mail: elements window of body mail

window of inbox gmail: enter image description here

1
Have you tried using the getAttachments() method of GmailMessage and setting the includeInlineImages parameter to true? - I hope this is helpful to you
Also, what are you returning mgsBody to? How are you trying to display the email content that you are obtaining from the function you posted? I'm not able to reproduce the error simply from attaching an in-line image to a mail and using the getBody() method like in your example. - I hope this is helpful to you
@RafaGuillermo Thank you for answering my questions. I return mgsBody to view it on HTML (Web page). I still don't understand your way, can you explain more to me? - Naoa Lee
From your screenshots I can see that the HTML content you are retrieving via the getBody() method contains cid:[email protected], which isn't a valid source for an image in the <img> tag. The images in a Gmail message aren't stored with public facing URLs so you would have to get the attachments individually by calling the getAttachments() method and then inserting the images into the HTML content via script before returning mgsBody at the end of the function. - I hope this is helpful to you
You could do something like convert them to URIs which will be displayed by your browser when visiting your HTML page, though GMail doesn't support data URIs in message bodies. Images embedded in the body are treated as attachements by GMail, so you can use the getAttachments() method to extract the images from the message. - I hope this is helpful to you

1 Answers

0
votes

You can get the embedded image from inside the Gmail message and append it to the end of the HTML response from mgs[0].getBody().

The return type of the getAttachements() method is an array of [GmailAttachment]s, so with that you can get the image data as bytes and make a data URI string which can be put into an <img> tag as the source:

function GetBodyMail(){

  var thread = GmailApp.getThreadById("thread-id");
  var mgs = thread.getMessages();
  var mgsBody = mgs[0].getBody();
  var attachments = mgs[0].getAttachments();

  mgsBody = mgsBody + "<br> <img src='data:" + attachments[0].getContentType() + ";base64," + Utilities.base64Encode(attachments[0].getBytes() + "'>");

  return mgsBody;

}