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");
}
}
}