In Gmail, using Google Apps Script, is it possible to forward the TRANSLATED emails which I receive to another email address?

Viewed 414

I receive many emails in Japanese everyday (I am living in Japan). Gmail automatically detect that the email is written in Japanese, so that one can click the "translate" button and get it translated. I would like to forward the translated email to another email address (or to a mailing list). It is easy to set up the mail forwarding, but when I do, only the original message (in Japanese) is forwarded. So my question is:

Is it possible, using Google Apps Scripts script (or any another tool), to forward the TRANSLATED emails which I receive to another email address?

I am a very beginner with Google tools, so any help is appreciated!

2 Answers

You can use the built-in LanguageApp service to translate text.

GmailApp.getInboxThreads().forEach((thread) => {
  thread
    .getMessages()
    .filter((message) => {
      return (
        message.getFrom().toLowerCase().indexOf("sender@example.com") !== -1
      );
    })
    .forEach((message) => {
      if (MailApp.getRemainingDailyQuota() > 1) {
        message.forward("email@example.com", {
          htmlBody: LanguageApp.translate(message.getBody(), "jp", "en"),
        });
      }
    });
});

To complete @Amit's answer:

You can build your filter based on the sender using the .getFrom() method on a message instance. Here you can find an example.

GmailApp.getInboxThreads().forEach( thread => { 
    thread.getMessages().forEach( message => {
         if (message.getFrom() === "email2@example.com" { 
              message.forward("email@example.com", { htmlBody: LanguageApp.translate(message.getBody(), "jp", "en") }
         }
    });
});

Reference

GmailApp

Related