Reputation: 41
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:
Upvotes: 3
Views: 6921
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
Reputation: 321
var label = GmailApp.getUserLabelByName('inbox');
var threads = label.getThreads();
for(i in threads)
{
threads[i].getMessages()[0].getFrom()
}
Upvotes: 0
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
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