Tremere
Tremere

Reputation: 41

how to get the email adress of the sender

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: [email protected]

topic: your basket from: [email protected]

  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 = "[email protected]" ;
      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

Upvotes: 3

Views: 6921

Answers (4)

Sanjib Debnath
Sanjib Debnath

Reputation: 3785

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

Upvotes: 0

totti
totti

Reputation: 321

var label = GmailApp.getUserLabelByName('inbox');
var threads = label.getThreads();


for(i in threads)
{

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

}

Upvotes: 0

Srik
Srik

Reputation: 7957

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

Upvotes: -1

patto_chennai
patto_chennai

Reputation: 71

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

Upvotes: 6

Related Questions