Search code examples
google-apps-scriptgmail

How can I customise the Google Apps Script I created?


So previously I made a script as follows:

  function autoReply() {
  var interval = 5;    //  if the script runs every 5 minutes; change otherwise
  var date = new Date();
  var day = date.getDay();
  var hour = date.getHours();
  if ([4,5,6,0].indexOf(day) > -1 || (day == 1 && hour < 9) || (day == 3 && hour >= 17)) {
    var timeFrom = Math.floor(date.valueOf()/1000) - 60 * interval;
    var threads = GmailApp.search('is:inbox after:' + timeFrom);
    for (var i = 0; i < threads.length; i++) {
      if (threads[i].isUnread()){
      threads[i].reply("xxxx");
      threads[i].markRead();
      threads[i].markImportant();
      }
    }
  }
}

The part

xxxx

are the words I want my readers to check. However, they come as some plain text and I wonder how I can modify the script to include my default Gmail signature and HTML elements?

Thank you so much in advance.

Edit: I have already set my signatures for forward and reply uses. But how can I make use of them directly via the given script? enter image description here

My received auto replay seems like below: enter image description here

So what's wrong here??


Solution

  • Solution

    Regarding the HTML body

    Now you are using the .reply(String body) method signature. This method only accepts a String parameter representing the body of the email will be sent as reply.

    In order to use more parameters such HTML body for example you will have to use the reply(String body, Object options) signature. This method accepts an additional JavaScript Object parameter that can contain these fields:

    • cc String: a comma separated list of email addresses to CC
    • bcc String: a comma separated list of email addresses to BCC
    • htmlBody String: if set, devices capable of rendering HTML will use it >instead of the required body argument; you can add an optional inlineImages field in HTML body if you have inlined images for your email
    • name String: the name of the sender of the email (default: the user's name)
    • from String: the address that the email should be sent from, which must be one of the values returned by GmailApp.getAliases()
    • replyTo String: an email address to use as the default reply-to address (default: the user's email address)
    • noReply Boolean: true if the email should be sent from a generic no-reply email address to discourage recipients from responding to emails; this option is only possible for G Suite accounts, not Gmail users
    • attachments BlobSource[]: an array of files to send with the email
    • inlineImages Object: a JavaScript object containing a mapping from image key (String) to image data (BlobSource); this assumes that the htmlBody parameter is used and contains references to these images in the format

    Regarding the signature

    Make sure you have set your default signature for ON REPLY/FORWARD USE in the Gmail Settings.

    References

    GmailThread reply(String,Object)

    Gmail Settings