Similar to: Getting only email address to display when using message.getFrom() in JavaMail but could not apply the solution.
Trying to use mailMessage.getFrom() on Google AppScript with Gmail.
Default script:
function loadAddOn(event) {
var accessToken = event.messageMetadata.accessToken;
var messageId = event.messageMetadata.messageId;
GmailApp.setCurrentMessageAccessToken(accessToken);
var mailMessage = GmailApp.getMessageById(messageId);
var from = mailMessage.getFrom();
var openDocButton = CardService.newTextButton()
.setText("open docs")
.setOpenLink(
CardService.newOpenLink().setUrl("https://developers.google.com/gmail/add-ons/"));
var card = CardService.newCardBuilder()
.setHeader(CardService.newCardHeader().setTitle("My First Gmail Addon"))
.addSection(CardService.newCardSection()
.addWidget(CardService.newTextParagraph().setText("The email is from: " + from))
.addWidget(openDocButton))
.build();
return [card];
}
var from = mailMessage.getFrom();
This returns the name of the sender, not the actual email address. Tried getFrom().getAddress()
trying my luck with post mentioned above, but obviously it didn't work, with getFrom()
returning a string.
Any Idea how to access an array of data or dictionary with metadata of sender so I can extract email, name etc. myself?
It appears that when using the getFrom()
method in a card, the name appears but not the email address as you described, which is counter to what is contained in the GmailMessage.getFrom()
documentation.
I have taken the liberty of reporting this behaviour for you on Google's Issue Tracker:
You can hit the ☆ next to the issue number in the top left of this page which lets Google know more people are encountering this and so it is more likely to be seen to faster.
In the mean time, as the getFrom()
method still works within the Apps Script interface, you can obtain the email address from the return string of getFrom()
.
If you use Logger.log(mailMessage.getFrom())
, you get a return in the log of the form:
Firstname Lastname <emailaddress@domain.com>
So, all you need to do is replace:
var from = mailMessage.getFrom();
with:
var from = mailMessage.getFrom().split("<")[1].split(">")[0];