Neal
Neal

Reputation: 198

access duplicate headers from GmailMessage.getHeader()

I am trying to get the Return-Path header from a message in my add-on. I was able to get the value, however, there are multiple Return-Path entries in the header. getHeader only returns the last one. How do I get both entries with getHeader?

Below is the full code if it helps.


function getContextualAddOn(event) {
  var message = getCurrentMessage(event);

  var card = createCard(message);

  return [card.build()];
}

function myAction(e) {
  toAddress = e["formInput"]["forwardTo"];
  var message = getCurrentMessage(e);
  GmailApp.sendEmail(toAddress, "SPAM", message.getRawContent());
}

/**
 * Retrieves the current message given an action event object.
 * @param {Event} event Action event object
 * @return {Message}
 */
function getCurrentMessage(event) {
  var accessToken = event.messageMetadata.accessToken;
  var messageId = event.messageMetadata.messageId;
  GmailApp.setCurrentMessageAccessToken(accessToken);
  return GmailApp.getMessageById(messageId);
}

function createCard(message) {
  var emailFrom = message.getHeader("Return-Path");
  emailFrom = emailFrom.replace(/[<>]/g, "");
  var card = CardService.newCardBuilder();
  card.setHeader(CardService.newCardHeader().setTitle("Forward e-mail"));
  
  var statusSection = CardService.newCardSection();
  statusSection.addWidget(CardService.newTextParagraph()
     .setText("<b>Sender: </b>" + emailFrom ));
  card.addSection(statusSection);
  
  var formArea = CardService.newCardSection();
  var widget = CardService.newTextInput()
    .setFieldName("forwardTo")
    .setTitle("To:");
  
  formArea.addWidget(widget);
  
  var button = CardService.newTextButton()
    .setText("Submit")
    .setOnClickAction(CardService.newAction().setFunctionName("myAction"));
  
  var buttons = CardService.newButtonSet()
    .addButton(button);
  
  formArea.addWidget(buttons);
  
  card.addSection(formArea);
  
  return card;
}

Upvotes: 0

Views: 251

Answers (1)

Tanaike
Tanaike

Reputation: 201643

In that case, I think that it is required to retrieve the values with Return-Path header from the raw content of email. In order to achieve this, how about the following modification?

With V8 runtime:

In this case, please enable V8 at the script editor.

From:

function createCard(message) {
  var emailFrom = message.getHeader("Return-Path");

To:

function createCard(message) {
  // var emailFrom = message.getHeader("Return-Path");
  const res = [...message.getRawContent().matchAll(/Return-Path:([\w\s\S].+)/g)].map(([,e]) => e.trim());
  • In this modification, res is an array including the values of Return-Path header. When 2 Return-Path headers are included in message, those are included in res.

Without V8 runtime:

In this case, you can use the script without V8.

From:

function createCard(message) {
  var emailFrom = message.getHeader("Return-Path");

To:

function createCard(message) {
  // var emailFrom = message.getHeader("Return-Path");
  const res = message.getRawContent().match(/Return-Path:[\w\s\S].+/g).map(function(e) {return e.replace("Return-Path:", "").trim()});
  • The result is the same with above script.

References:

Upvotes: 2

Related Questions