【问题标题】:Google Script: get content of an email answer without the original emailGoogle Script:在没有原始电子邮件的情况下获取电子邮件答案的内容
【发布时间】:2017-12-26 05:38:37
【问题描述】:

我在处理我自己发送的电子邮件的答复时遇到了麻烦。事实上,当我使用“getPlainBody”方法来获取电子邮件的内容时,我总是会得到一个包含我想要的消息和我的原始消息的文本。此外,格式永远不会相同,我猜这取决于对方的电子邮件客户端。

一些例子来说明这一点:

Hello,
This is my answer to your email.
Best regards

On Wed, Jul 12, 2017 at 10:02 AM, Bastien <bastien@example.org> wrote:

> Hi,
>
> This is the original email I sent you.
> Bastien

或者这个:

Hello,
This is my answer to your email.
Best regards


From: bastien@example.org [mailto:bastien@example.org]
Sent: Wednesday, July 12, 2017 6:49 PM
To: someone <someone@example.com>
Subject: this is the original subject

Hi,

This is the original email I sent you.
Bastien

我只想检索这部分:

Hello,
This is my answer to your email.
Best regards

我也有许多不同语言的电子邮件,因此将原始消息和答案分开的信息可以用任何语言编写,因此对于任何语言以编程方式检测它有点复杂。

我确信 Gmail 知道如何做到这一点,因为在 Gmail 界面中它只显示我想要的电子邮件部分,并自动隐藏我的第一封电子邮件。我只是不知道是否有办法使用 Google Script 提取消息。

【问题讨论】:

  • 不要得到普通的身体,为什么不得到raw content?您可能会在邮件的 HTML 结构中找到一些答案,就像 Gmail 一样?
  • 感谢您的回答。我试图查看原始内容,但对我来说并不容易真正阅读......有一个巨大的标题,提供了很多关于电子邮件服务器、身份验证、dkim 签名、smtp 消息的信息,我真的不明白等...然后我得到的文本与“getPlainBody”完全相同。我不确定我应该如何使用标题中的所有数据,你知道我应该寻找什么吗?
  • Gmail 知道如何做到这一点,因为它拥有整个电子邮件链。它隐藏了与早期消息匹配的部分。有时,当您收到两条内容相同的消息时,即使不是回复,第二条也会隐藏其正文。
  • 尝试SO post 中给出的解决方法。但是,这依赖于从电子表格中读取和写入。看看能不能帮到你。

标签: email google-apps-script gmail-api


【解决方案1】:

巴斯蒂安,

我确实以一种不优雅但相当稳定的方式解决了获取正文并利用 Gmail 知道 的情况,即寻找标签 &lt;div class="gmail_quote"&gt; GMail 显示的[...]

[...]不是这种情况的情况下(例如,通过 microsoft mail 发送的邮件),第一个 &lt;div&gt; 有时可以解决问题。 您将看到自此代码首次实现(大约一年前)以来出现的其他两次错误尝试。在我的情况下,senders Universe 非常小,所以这个俗气的解决方案效果很好。 祝你好运。

mRtaU = mails[j].getBody();

  mRta = mRtaU;

  mRta = mRta.substring(0,mRta.toLowerCase().indexOf('<div class="gmail_quote">'));  

  if( mRta == '' ){ 
    mRta = mRtaU;
    if( mRta.slice( 0, 5 ) == '<div>' ){ //case Microsoft try I...

      mRta = mRta.substring(0,mRta.toLowerCase().indexOf('</div>'));
    }else{

      if( mRta.slice( 0, 2 ) == '<p' ){ //case Microsoft try II...

        mRta = mRta.substring(0, mRta.toLowerCase().indexOf('</p>')); 
      }else{

        mRta = mRta.substring(0,mRta.indexOf('\n')); //last try, a crlf could split the answer from original (yeap, no crlf in html but...)
      }
    }
  }
  mRta = mRta.split('<br>').join('\n').replace(/<\/?[^>]+(>|$)/g, "");

【讨论】:

    猜你喜欢
    • 2015-09-25
    • 2017-10-08
    • 2011-06-06
    • 1970-01-01
    • 1970-01-01
    • 2010-11-04
    • 1970-01-01
    • 2012-05-29
    • 1970-01-01
    相关资源
    最近更新 更多