1

I'm new to Gmail add-ons. Could you please help me how to retrieve current email from address, to address and cc addresses?

GmailApp.getMessageById(messageId).getTo()
GmailApp.getMessageById(messageId).getFrom() is displaying names but not giving email addresses.
Avinash Singh
  • 4,970
  • 8
  • 20
  • 35
Mahesh
  • 11
  • 1
  • Use `Session` class – TheMaster Feb 05 '19 at 18:25
  • Thanks for the reply. But as per the document https://developers.google.com/apps-script/reference/base/session this is giving the user information who is running the script. But for me I need to know the from address and to addresses of the mail information. – Mahesh Feb 07 '19 at 09:35
  • Can't reproduce. I get name as well as the e-mail address in logs. – TheMaster Feb 07 '19 at 11:59
  • I am getting current logged in user details not getting a way to retrieve to addresses – Mahesh Feb 08 '19 at 05:40
  • check [How to get email address from GmailMessage object](https://stackoverflow.com/questions/26242591/is-there-a-way-to-get-the-specific-email-address-from-a-gmail-message-object-in/59248442#59248442) – poloapolo Dec 09 '19 at 11:58

1 Answers1

4

After getting the messageId, you can run the code below to get the GmailMessage.

var message = GmailApp.getMessageById(messageId);

This returns GmailMessage object. Now you can use methods such as getTo(), getFrom(), getCC(), getBcc(). Read more about GmailMessage here. You can use it like,

var from = message.getFrom();

It gives name and email address of the sender. To get email address only, use regular expressions.

var senderEmail = from.replace(/^.+<([^>]+)>$/, "$1");