2

I'm having a hard time figuring out how to add a label to an email I'm sending from a script, the documentation on sending an email doesn't have an advanced parameter that adds a label: https://developers.google.com/apps-script/reference/gmail/gmail-app#sendEmail(String,String,String,Object)

This is what I tried instead with .addLabel(), but it isn't working, has anybody figured this out?

function myFunction() {
  var test = "This is a email was sent from Google Apps Script.";

  var subjectTitle = "This email was scripted via Google Apps Scripts";

  var email = Session.getActiveUser().getEmail(); 

  GmailApp.sendEmail(email, subjectTitle, test);

  var labelthread = GmailApp.getInboxThreads();
  for (var j = 0; j < labelthread.length; j++) {
    if (labelthread[j].getFirstMessageSubject() == subjectTitle) {
      labelthread[j].addLabel("Scripted Email");
    }
  }
}
user3334776
  • 113
  • 1
  • 10

1 Answers1

0

You can do a test in the Sent Items folder instead of the Inbox.

var thread = GmailApp.search("in:sent subject" + subjectTitle, 0, 1);
var label  = GmailApp.getUserLabelByName("Scripted Email");
thread[0].addLabel(label);
Amit Agarwal
  • 10,910
  • 1
  • 32
  • 43
  • I tried your suggestion, it didn't work. It says addLabel isn't a recognized command. – user3334776 Jun 14 '14 at 00:17
  • Still getting the same problem... You would think there should be a built in option for labels with the "sendEmail" function, but it's only giving options for attachments, bcc, CC, from, htmlBody, inlineImages, name, noReply and replyTo... like wtf – user3334776 Jun 16 '14 at 17:36