问题
I appreciate if you could help.
I need to find all unread emails in my gmail 1@example.com and send them all to 2@example.com by using sendEmail(to,replyTo, subject, body) https://developers.google.com/apps-script/reference/mail/mail-app
I tried to write a script but unfortinately it does not want work. I hope you could help
function RespondEmail(e) {
//send response email
var threads = GmailApp.search("to:(1@example.com) label:unread");
for (var i = 0; i < threads.length; i++) {
threads[i].sendEmail("1@example.com",
"2@example.com",
"TPS report status",
"What is the status of those TPS reports?")}
// mark all as read
var threads = GmailApp.search("to:(1@example.com) label:unread");
GmailApp.markThreadsRead(threads);
}
I also would be happy if you could advise me how I can change the subject of the ReplyTo email according to original email which I receive on 1@example.com
回答1:
The problem in your script lies in the fact that sendEmail() belongs to the GmailApp Service, so it always needs to be called the following way:
GmailApp.sendEmail()
For your needs, it may be more appropriate to use the forward() method.
In the following example, I added a custom subject which you can edit and adapt to your needs.
function RespondEmail() {
//send response email
var threads = GmailApp.search("to:origin@gmail.com is:unread");
var subject = "";
var msg = "";
var c = 0; // will be used to count the messages in each thread
var t = "";
var attachment = "";
var forwarded = "";
for (var i = 0; i < 3 /*threads.length*/ ; i++) {
// I set 'i' to 3 so that you can test the function on your 3 most recent unread emails.
// to use it on all your unread email, remove the 3 and remove the /* and */ signs.
t = threads[i]; // I wanted to avoid repetition of "threads[i]" for the next 2 lines haha
c = t.getMessageCount() - 1;
msg = t.getMessages()[c];
forwarded = msg.getBody(); // that is the body of the message we are forwarding.
subject = msg.getSubject();
attachment = msg.getAttachments();
msg.forward("destination@gmail.com", {
replyTo: "origin@gmail.com",
subject: "TPS report status of [" + subject + "]", // customizes the subject
htmlBody: "What is the status of those TPS reports below?<br><br>" //adds your message to the body
+
"<div style='text-align: center;'>---------- Forwarded message ----------</div><br>" + forwarded, //centers
attachments: attachment
});
t.markRead(); // mark each forwarded thread as read, one by one
}
}
来源:https://stackoverflow.com/questions/43790496/how-to-forward-email-to-another-address-with-sendemailto-replyto-subject-body