2
votes

Okay so with this code I am able to get the actual mail content which comes by Email but what I want is I want to get message and attachment too. what can be done in order to list out attachments and later on giving option to download the attachments gmail api.

    var ifrm = document.getElementById("iframe").contentWindow.document;
    ifrm.body.innerHTML = getMessageBody(message.payload);
  };

  let getMessageBody = (message) => {
    var encodedBody = "";
    if (typeof message.parts === "undefined") {
      encodedBody = message.body.data;
    } else {
      encodedBody = getHTMLPart(message.parts);
    }

    return Base64.decode(encodedBody);
  };

  let getHTMLPart = (arr) => {
    for (var x = 0; x <= arr.length; x++) {
      if (typeof arr[x].parts === "undefined") {
        if (arr[x].mimeType === "text/html") {
          return arr[x].body.data;
        }
      } else {
        return getHTMLPart(arr[x].parts);
      }
    }
    return "";
  };

Gmail API when I click on message.

 getOneMessage = (messageId) => {
    return window.gapi.client.gmail.users.messages
      .get({
        userId: "me",
        id: messageId,
      })
      .then(
        (response) => {
          this.setState({
            message: response.result,
          });
        },
        (err) => {
          console.error("getMessage error", err);
        }
      );
  }; 
  

  handleMessageClick = (e) => {
    const messageId = e.currentTarget.getAttribute("id");
    this.getOneMessage(messageId);
    
1
highly recommend you don't try to do it yourself and use a library that wraps the api for youuser120242
Please include in the script how you are using gmail-api to get the message body.Alessandro
I have updated the question with the Gmail api.Dhruv patel

1 Answers

1
votes

Solution

You are using the Users.messages: get endpoint. This is fine to retrieve the message body, but to retrieve attachments you will have to use the Users.messages.attachments: get. Here you can find a link to the documentation.

Proposed code editing:

getAttachments = (message, callback) => {
  var parts = message.payload.parts;
  for (var i = 0; i < parts.length; i++) {
    var part = parts[i];
    if (part.filename && part.filename.length > 0) {
      var attachId = part.body.attachmentId;
      var request = gapi.client.gmail.users.messages.attachments.get({
        'id': attachId,
        'messageId': message.id,
        'userId': userId
      });
      request.execute(function(attachment) {
        callback(part.filename, part.mimeType, attachment);
      });
    }
  }
}

 getOneMessage = (messageId) => {
    return window.gapi.client.gmail.users.messages
      .get({
        userId: "me",
        id: messageId,
      })
      .then(
        (response) => {
          this.setState({
            message: response.result,
          });
          // Get the attachment and do something with it
          getAttachments(response.result, callback);
        },
        (err) => {
          console.error("getMessage error", err);
        }
      );
  }; 
  

  handleMessageClick = (e) => {
    const messageId = e.currentTarget.getAttribute("id");
    this.getOneMessage(messageId);

Reference

Users.messages.attachments