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!

Sics Arts
  • 13
  • 3
  • replace double quotes in the begining and end with `tild`, – Akhil Aravind Feb 19 '20 at 04:36
  • Does this answer your question? [When to use double or single quotes in JavaScript?](https://stackoverflow.com/questions/242813/when-to-use-double-or-single-quotes-in-javascript) – TheMaster Feb 19 '20 at 04: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! – Sics Arts Feb 19 '20 at 19:43

2 Answers2

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

Manu Varghese
  • 791
  • 8
  • 25
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()
Akhil Aravind
  • 5,741
  • 16
  • 35