0

I am trying to create a script for autoreply in google apps and would love to have that reply to have some formatting. I've been getting a syntax error due to the quotes closing with the opening of the href tag. Is there a way to insert the URL into the html message? My scribbles below.

function autoReply() {
var interval = 5;    //  if the script runs every 5 minutes; change otherwise
  var message = "We are out of the office until Monday morning and will reply to your email then. If you are having an emergency, please text (800) 590-2508, we'll do our best to reply as soon as possible.";
  var htmlMessage = "<p>Hi,</p><p>Thanks for your message.</p><p>We are out of the office until Monday morning and will reply to your email then. In the meantime, check out some helpful resources:</p><p><a href="https://sampleurl.com" target="_blank" rel="noopener">Link text</a></p><p><a href="https://anothersammple.com" target="_blank" rel="noopener">Link text</a></p><p>If you are having an emergency, please text (800) 000-0000, we'll do our best to get back to you as soon as possible.</p><p>Have a pleasant weekend,</p><p>My Signature</p>";
  var date = new Date();
  var day = date.getDay();
  var hour = date.getHours();
  if ([6,0].indexOf(day) > -1) {
    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(message, {htmlBody: htmlMessage});
      threads[i].markRead();
      threads[i].star();
      }
    }
  }
}

The error is on line 4

 var htmlMessage = "<p>Hi,</p><p>Thanks for your message.</p><p>We are out of the office until Monday morning and will reply to your email then. In the meantime, check out some helpful resources:</p><p><a href="https://sampleurl.com" target="_blank" rel="noopener">Link text</a></p><p><a href="https://anothersammple.com" target="_blank" rel="noopener">Link text</a></p><p>If you are having an emergency, please text (800) 000-0000, we'll do our best to get back to you as soon as possible.</p><p>Have a pleasant weekend,</p><p>My Signature</p>";

The quotes after htmlMessage = open the message, and the quotes after the first href= close it, so the rest of the message breaks the syntax.

Any insight on how to include the whole html under the var statement?

Thanks!

3
  • replace double quotes in the begining and end with tild, Commented Feb 19, 2020 at 4:36
  • Does this answer your question? When to use double or single quotes in JavaScript? Commented Feb 19, 2020 at 4:37
  • Thanks, the answer @AkhilAravind suggested is easier to implement, but both work! Thanks, TheMaster, this is a helpful thread, I didn't know these quotes can be used interchangeably! Commented Feb 19, 2020 at 19:43

2 Answers 2

2

add \ before "

 var htmlMessage = "<p>Hi,</p><p>Thanks for your message.</p><p>We are out of the office until Monday morning and will reply to your email then. In the meantime, check out some helpful resources:</p><p><a href=\"https://sampleurl.com\" target=\"_blank\" rel=\"noopener\">Link text</a></p><p><a href=\"https://anothersammple.com\" target=\"_blank\" rel=\"noopener\">Link text</a></p><p>If you are having an emergency, please text (800) 000-0000, we'll do our best to get back to you as soon as possible.</p><p>Have a pleasant weekend,</p><p>My Signature</p>";

another method is to replace " with ' inside the string

Sign up to request clarification or add additional context in comments.

Comments

1

Replace " from the beginning and end with tild `

Check the documentation Here

var htmlMessage = `<p>Hi,</p><p>Thanks for your message.</p><p>We are out of the office until Monday morning and will reply to your email then. In the meantime, check out some helpful resources:</p><p><a href="https://sampleurl.com" target="_blank" rel="noopener">Link text</a></p><p><a href="https://anothersammple.com" target="_blank" rel="noopener">Link text</a></p><p>If you are having an emergency, please text (800) 000-0000, we'll do our best to get back to you as soon as possible.</p><p>Have a pleasant weekend,</p><p>My Signature</p>`;

function autoReply() {
var interval = 5;    //  if the script runs every 5 minutes; change otherwise
  var message = "We are out of the office until Monday morning and will reply to your email then. If you are having an emergency, please text (800) 590-2508, we'll do our best to reply as soon as possible.";
  var htmlMessage = `<p>Hi,</p><p>Thanks for your message.</p><p>We are out of the office until Monday morning and will reply to your email then. In the meantime, check out some helpful resources:</p><p><a href="https://sampleurl.com" target="_blank" rel="noopener">Link text</a></p><p><a href="https://anothersammple.com" target="_blank" rel="noopener">Link text</a></p><p>If you are having an emergency, please text (800) 000-0000, we'll do our best to get back to you as soon as possible.</p><p>Have a pleasant weekend,</p><p>My Signature</p>`;
  document.write(htmlMessage)
  var date = new Date();
  var day = date.getDay();
  var hour = date.getHours();
  if ([6,0].indexOf(day) > -1) {
    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(message, {htmlBody: htmlMessage});
      threads[i].markRead();
      threads[i].star();
      }
    }
  }
}
autoReply()

2 Comments

Note to op: You must enable v8 or else.
Thanks! Learning new stuff every day :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.