3

I need to generate a report on the list of messages and the sender of a letter, I can extract the theme of letters, but not understanding how to get the sender's address for each letter, the report turned out to be:

topic: hello From: frank@gmail.com

topic: your basket from: jerry@facebook.com

  function myFunction() {
      var emailAddress = Session.getActiveUser().getEmail();
      var threads = GmailApp.getInboxThreads();


      var output = ContentService.createTextOutput();
      for (var i = 0; i < threads.length; i++) {
        output.append(threads[i].getFirstMessageSubject()+" from:"+'\n');
      }


      GmailApp.sendEmail(emailAddress,"Mail Report", output.getContent()

        );
    }

UPDATE

Thank you for your answers, the solution was simple

   function myFunction() {
      var emailAddress = "example@email.com" ;
      var threads = GmailApp.getInboxThreads();

      var messages = threads[0].getMessages();
      var senderEmail = messages[0].getFrom();


      var output = ContentService.createTextOutput();
      for (var i = 0; i < threads.length; i++) {
        messages = threads[i].getMessages()
        senderEmail = messages[0].getFrom();
        output.append(i + ".  " + threads[i].getFirstMessageSubject()+"   from:"+ senderEmail + '\n');
      }


      GmailApp.sendEmail(emailAddress,"Mail Report", output.getContent()

        );
    }

Example result:

  1. Email Verification - Stack Overflow from:Stack Overflow
  2. Project Update #10: Double Fine Adventure by Double Fine and 2 Player Productions from:Kickstarter
Tremere
  • 41
  • 1
  • 3
  • The code you've provided does not produce the output you've provided. That being said, I *think* patto_chennai has answered your question, although I'm not sure any of us are sure what the question is. – mzimmerman Jul 02 '12 at 16:28

4 Answers4

6

If I understand what you are looking to do, then I believe you could get the messages from the thread, and then the sender from the message

var threads = GmailApp.getInboxThreads();
var messages = threads[0].getMessages();
var senderEmail = messages[0].getFrom();
0
var label = GmailApp.getUserLabelByName('inbox');
var threads = label.getThreads();


for(i in threads)
{

  threads[i].getMessages()[0].getFrom()

}
totti
  • 321
  • 2
  • 9
0

You need to add "https://www.googleapis.com/auth/userinfo.email" in oauthScopes.(appscript.json file)

you can get more details in this link enter link description here

Sanjib Debnath
  • 3,556
  • 2
  • 22
  • 16
-1

For reasons of security, you cannot get the email using Session.getActiveUser().getEmail() if you are on a consumer Google account. It works only when the script is owned by a Google Apps user and the visitor belongs to the same domain.

However, you can try what is mentioned here

Community
  • 1
  • 1
Srik
  • 7,907
  • 2
  • 20
  • 29