How to extract full body content of a Bounced back

2020-06-28 03:19发布

问题:

The above screenshot is the sample of the Bounced Back Mail received.

I'm using the following code to extract the Body of the mail.

function test() 
{
  var BouncedEmails = GmailApp.search("label:test The following message was undeliverable ");

  for( var i=0;i<BouncedEmails.length;i++)
  {
    var Gmessage = GmailApp.getMessagesForThread(BouncedEmails[i]);

    for(var j=0;j<Gmessage.length;j++)
    {
      var body = Gmessage[j].getPlainBody();
      Logger.log(body);
    }
  }
}

But when I am doing this, I got the following output.

As you can see the last part of the Body is missing, that is :

I also tried using :

var body = Gmessage[j].getBody();

instead of "GetPlainBody()" but the output was still the same.

On using :

 var body = Gmessage[j].getRawContent();

I got this as output for the missing part, which seems to me as some sort of encoding.

So my question is, how do i extract the full content of the Bounced Back Mail?

Thank You.

回答1:

I finally found the answer to my own question.

This has worked for me and will pretty much work for anyone on our planet.

function test()
{
  var BouncedEmails = GmailApp.search("label:test The following message was undeliverable ");

  for( var i=0;i<BouncedEmails.length;i++)
  {
    var threadId = BouncedEmails[i].getId();

    var id = Session.getEffectiveUser().getEmail();
    var body = Gmail.Users.Threads.get(id, threadId, {format : 'full'});

    var messages = body.messages;

    var payLoad = messages[0].payload.parts[2];

    var string = JSON.stringify(payLoad);
    Logger.log(string);
  }
}   

The solutions provided by @AmitAgarwal and @ShyamKansagra would also work for some cases, but which solution to use depends on what is your exact requirement.



回答2:

Don't use Logger.log as it truncates the output after a certain number of lines. Log the output in a spreadsheet and you'll see that the full body is extracted with getPlainBody() or getBody().

I recently published a Google Script to get all bounced emails in Gmail and logs them to a Google sheet. It is open so can build upon that script.



回答3:

I also tried using getBody(), getPlainBody() and getRawContent() methods on bounced back emails. I noticed that these methods didn't give entire body of the email i.e. the part with technical details was skipped entirely in the logs.

So, I used this following code(all credits to @Amit Agarwal), which I found in the link which Amit has shared in his answer and it gave me entire body of bounced back email.

Here is the code:

var t = "in:anywhere from:(mailer-daemon@google.com OR mailer-daemon@googlemail.com)";
GmailApp.search(t,0,500).forEach(function(t)
                                          {
                                            t.getMessages().forEach(function(r)
                                                                    {
                                                                      if(r.getFrom().indexOf("mailer-daemon")!==-1)
                                                                      {
                                                                        var i=r.getPlainBody();
                                                                        Logger.log(i);
                                                                      }
                                                                    }
                                                                    )
                                          }

)

It worked for me and gave the entire content in the logs itself. Hope this helps.