1

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

Marios
  • 26,333
  • 8
  • 32
  • 52
Naoa Lee
  • 135
  • 2
  • 19
  • Have you tried using the [`getAttachments()` method of `GmailMessage`](https://developers.google.com/apps-script/reference/gmail/gmail-message#getAttachments(Object)) and setting the `includeInlineImages` parameter to `true`? – Rafa Guillermo Sep 12 '19 at 10:01
  • 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. – Rafa Guillermo Sep 12 '19 at 10:12
  • @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 Sep 12 '19 at 10:28
  • 1
    From your screenshots I can see that the HTML content you are retrieving via the `getBody()` method contains `cid:image001.png@01D563D1.99511440`, which isn't a valid source for an image in the `` 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. – Rafa Guillermo Sep 12 '19 at 10:45
  • 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. – Rafa Guillermo Sep 12 '19 at 10:45
  • TheMaster thanks you edit for me ! – Naoa Lee Sep 13 '19 at 01:27
  • Rafa Guillermo, thank you for answering my questions. So if i use getbody i can't display img and on html page still display it. how to put img of getAttachments() into div of img (body) ? – Naoa Lee Sep 13 '19 at 01:36
  • Just [append](https://developers.google.com/apps-script/reference/html/html-output#append(String)) to `HtmlOutput` – TheMaster Sep 13 '19 at 12:44
  • If you have a specific element you want to put it in within the HTML you can use the [`.innerHTML`](https://www.w3schools.com/jsref/prop_html_innerhtml.asp) property of the element in JavaScript. – Rafa Guillermo Sep 13 '19 at 15:26
  • Thanks TheMaster and Rafa Guillermo. But data of .GS file return to .html file is null and i check log in .gs file data return is [GmailAttachment]. i can't append it in html page. – Naoa Lee Sep 16 '19 at 01:40

1 Answers1

0

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;

}
Rafa Guillermo
  • 14,474
  • 3
  • 18
  • 54