Search code examples
google-apps-scriptgmailgoogle-docs

How to save text of a mail body to Google Docs


I am writing a small script in Google.Scripts to look for a particular mail address in my Gmail account and copy the body to a google.docs. It seems to work fine when finding the emails and reading the content of the body, as I can see in the traces of the execution.

It also creates the document or opens it if does not exist. However, when I check the document, it is empty. I am lost here, as it seems to me that I am using the doc methods corretly. Here is the code of the script:

/**
 * Creates a Google Doc and copy the texts of mails in it.
 * Mails will be retrieved by sender. This script is intended
 * as a way to get information from newsletters in a single document
 */
function getMailsToDoc() {
  // Create a new Google Doc named 'MyMailsText'
  var doc = DocumentApp.create('MyMailsText');

  // Get the email address of the active user - that's you.
  var email = Session.getActiveUser().getEmail();

  var threads = GmailApp.search('from:NewsLetterMail', 0, 20);
  Logger.log("Messages unread in inbox: " + GmailApp.getInboxUnreadCount());
  var messages = threads[0].getMessages();
  var senderEmail = '[email protected]';
  for (var i = 0; i < threads.length; i++) {
        messages = threads[i].getMessages()
        Logger.log(messages[0].getPlainBody());

        // Get all possible mails in the thread and copy their bodies to 
        for (var j = 0; j < messages.length; j++) {
          if(senderEmail == messages[j].getFrom()){
            Logger.log(messages[0].getFrom());
            doc.getBody().appendParagraph(messages[j].getPlainBody());
          }
        }
  }
  doc.saveAndClose();

}

And here an example of the traces:

[18-09-08 06:07:56:535 PDT] Iniciando ejecución
[18-09-08 06:07:57:592 PDT] DocumentApp.create([MyMailsText]) [1,051 segundos]
[18-09-08 06:07:57:593 PDT] Session.getActiveUser() [0 segundos]
[18-09-08 06:07:57:593 PDT] User.getEmail() [0 segundos]
[18-09-08 06:07:58:109 PDT] GmailApp.search([from:NewsLetterMail, 0, 20]) [0,514 segundos]
[18-09-08 06:07:58:212 PDT] GmailApp.getInboxUnreadCount() [0,102 segundos]
[18-09-08 06:07:58:213 PDT] Logger.log([Messages unread in inbox: 265, []]) [0 segundos]
[18-09-08 06:07:58:334 PDT] GmailThread.getMessages() [0,12 segundos]
[18-09-08 06:07:58:335 PDT] GmailThread.getMessages() [0 segundos]
[18-09-08 06:07:58:443 PDT] GmailMessage.getPlainBody() [0,107 segundos]
**[18-09-08 06:07:58:444 PDT] Logger.log([News and information related to different topics, blablablabla, []]...) [0 segundos]**
[18-09-08 06:07:58:444 PDT] GmailMessage.getFrom() [0 segundos]
[18-09-08 06:07:58:839 PDT] GmailThread.getMessages() [0,395 segundos]
[18-09-08 06:07:58:934 PDT] GmailMessage.getPlainBody() [0,094 segundos]
**[18-09-08 06:07:58:934 PDT] Logger.log([More interesting news from this interesting newsletter blablabla , []]...) [0 segundos]**

Could you give me some hint on what am I doing wrong or any suggestion to how to figure out why the google doc is empty? I would appreciate any help. Thank you very much!


Solution

  • Solution based on comment

    As was pointed out by commenter tehhowch, the if condition was always false. It was cause by a wrong senderEmail string to compare with messages[j].getFrom().

    Changing it as follows makes it work:

    var senderEmail = 'NewsLetterMail <[email protected]>';