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

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

问题描述

在Google应用程式脚本中处理我收件匣中的电子邮件清单。
从我需要获取每条消息的地址和地址的消息中获得。
当我使用
message.getFrom()方法获取发件人地址时,它会返回如下所示的内容:FirstName LastName。但是,我想要的是普通电子邮件地址:email@domain.com我无法在GmailApp对象或Message对象上找到只返回此信息的方法。



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

解决方案> > > getFrom()方法应该返回消息的发件人格式 Some Person< someone@somewhere.com> 。如果您只需要字符串的电子邮件地址部分,则可以使用像这样的正则表达式来提取它(可以从 getFrom()方法描述):

  var thread = GmailApp.getInboxThreads(0,1)[0]; //获取收件箱中的第一个线程
var message = thread.getMessages()[0]; //获取第一条消息
Logger.log(message.getFrom()。replace(/^.+& lt;([^>] +)> $ /,$ 1));

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


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() returns a simple string, not an address object that can be dissected further. Am I missing something?

解决方案

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

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天全站免登陆