Gmail AppScript mailMessage.getFrom()返回电子邮件而不是名称 [英] Gmail AppScript mailMessage.getFrom() to return email not name

查看:51
本文介绍了Gmail AppScript mailMessage.getFrom()返回电子邮件而不是名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

类似于:在JavaMail中使用message.getFrom()时仅获取要显示的电子邮件地址,但无法应用该解决方案.

Similar to: Getting only email address to display when using message.getFrom() in JavaMail but could not apply the solution.

尝试使用 mailMessage.getFrom()在带有Gmail的Google AppScript中.

Trying to use mailMessage.getFrom() on Google AppScript with Gmail.

默认脚本:

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();

var from = mailMessage.getFrom();

这将返回发件人的姓名,而不是实际的电子邮件地址.尝试过 getFrom().getAddress()来尝试我上面提到的帖子的运气,但很明显它没有用,因为 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?

推荐答案

这似乎是一个错误!

似乎在卡中使用 getFrom()方法时,会显示名称,但不显示您所描述的电子邮件地址,这与中包含的内容相反href ="https://developers.google.com/apps-script/reference/gmail/gmail-message#getFrom()" rel ="nofollow noreferrer"> GmailMessage.getFrom()文档.

This appears to be a bug!

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.

我已自由通过Google的问题跟踪器为您报告此行为:

I have taken the liberty of reporting this behaviour for you on Google's Issue Tracker:

您可以点击此页面左上角发行号旁边的☆,让Google知道更多人正在遇到这种情况,因此更有可能看到更快的速度.

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.

同时,由于 getFrom()方法仍可在Apps脚本界面中使用,因此您可以从 getFrom()的返回字符串中获取电子邮件地址.

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().

如果使用 Logger.log(mailMessage.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();

具有:

var from = mailMessage.getFrom().split("<")[1].split(">")[0];

希望对您有帮助!

这篇关于Gmail AppScript mailMessage.getFrom()返回电子邮件而不是名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆