有没有办法从 Google 应用脚本中的 Gmail 邮件对象中获取特定的电子邮件地址? [英] Is there a way to get the specific email address from a Gmail message object in Google app scripts?

查看:34
本文介绍了有没有办法从 Google 应用脚本中的 Gmail 邮件对象中获取特定的电子邮件地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Google App Scripts 中处理收件箱中的电子邮件列表.我需要从消息中获取每条消息的发件人和收件人地址.当我使用message.getFrom() 方法来获取发件人地址,它返回如下内容:FirstName LastName".但是,我想要的是普通的电子邮件地址:email@domain.com" 我在 GmailApp 对象或 Message 对象上找不到仅返回此信息的方法.

In Google App Scripts to process a list of email messages in my inbox. From the messages I need to get the from and to addresses of each message. When I use the message.getFrom() method to get the from address it returns something like this: "FirstName LastName ". But, what I want is the plain email address: "email@domain.com" I can't find a method on the GmailApp object, or the Message object, that would return just this information.

getFrom() 返回一个简单的字符串,而不是一个可以进一步剖析的地址对象.我错过了什么吗?

getFrom() returns a simple string, not an address object that can be dissected further. Am I missing something?

推荐答案

getFrom() 方法应该以 Some Person 格式返回消息的发送者;.如果您只需要字符串的电子邮件地址部分,您可以使用这样的正则表达式提取它(改编自 getFrom() 方法说明):

getFrom() method should return the sender of the message in the format Some Person <someone@somewhere.com>. If you need just the email address portion of the string, you can extract it using regular expression like this (adapted example from getFrom() method description):

var thread = GmailApp.getInboxThreads(0,1)[0]; // get first thread in inbox
var message = thread.getMessages()[0]; // get first message
Logger.log(message.getFrom().replace(/^.+<([^>]+)>$/, "$1"));

该正则表达式将 getFrom() 方法返回的字符串替换为尖括号中的字符串部分(发件人的电子邮件地址).

That regex replace the string returned by getFrom() method with the part of the string in angle brackets (the sender's email address).

这篇关于有没有办法从 Google 应用脚本中的 Gmail 邮件对象中获取特定的电子邮件地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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