I am trying to get a script running that will allow me to do the following:
After sending recipient X an email and NOT receiving a response, I want to be able to send recipient X a “Kind Reminder Email” in response to the original email that I had sent recipient X (I want it to be in the same thread so that recipient X can reference the original email in the thread).
This is what I have got going so far:
function replyToSelectedEmails() {
// Prompt the user to enter the label name
var ui = SpreadsheetApp.getUi();
var response = ui.prompt('Enter the label name of emails you want to reply to:');
var labelName = response.getResponseText();
if (response.getSelectedButton() != ui.Button.OK) {
ui.alert('No label name entered. Exiting.');
return;
}
// Prompt the user to enter the reply message
response = ui.prompt('Enter your reply message:');
var replyMessage = response.getResponseText();
if (response.getSelectedButton() != ui.Button.OK) {
ui.alert('No reply message entered. Exiting.');
return;
}
// Get the label
var label = GmailApp.getUserLabelByName(labelName);
if (label == null) {
ui.alert('Label not found!');
return;
}
// Get threads with the specified label
var threads = label.getThreads();
for (var i = 0; i < threads.length; i++) {
var thread = threads[i];
var messages = thread.getMessages();
// Get the recipient's email address from the first message in the thread
var originalMessage = messages[0];
var recipientEmails = originalMessage.getTo();
var recipientEmail = recipientEmails[0]; // Take the first email address
// Reply to the last message in the thread
var lastMessage = messages[messages.length - 1];
lastMessage.reply({
htmlBody: replyMessage,
name: 'name', // Replace with the desired sender's name
to: recipientEmail // Reply to the recipient of the original email
});
}
ui.alert('Replied to ' + threads.length + ' emails.');
}
It didnt do the following:
Respond to the email. It send me an email instead.
The email didnt have my name (it displayed by email in the From section: ((despite me editing “name: ‘name’, // Replace with the desired sender’s name” with my name)
Email didnt have the Kind reminder msg I entered when prompted and instead had [object Object]
any and all help appreciated.
user25467206 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.