Pass variable to href tag within body of an email

First time posting, although I have spent many a moon lurking around the site. I was unable to find if anyone had a similar post and thus I am posting to see if someone can answer this question for me.

I am trying to embed a hyperlink into the body of an email. I am taking data from a specific cell from a sheet

var n = ws.getRange("A2").getDisplayValue();
var data1 = ws.getRange("L2").getDisplayValue();

and want to pass that variable, data1, to an html template to embed into a specific href tag URL within the template to mail out.

var temp= HtmlService.createTemplateFromFile("xyz");

temp.name = n;

var htmlMessage = temp.evaluate().getContent();
  GmailApp.sendEmail(
      Email, 
      Subject,"Your email doesn't support HTML.", 
      {name: "AD", htmlBody: htmlMessage}
    );

Example html file "xyz":

<p>Hi, <?= name ?>,</p>

<p>Please see attached <a href="data1">link</a></p>

How would I go about passing the variable into the tag? Thanks in advance!

3 thoughts on “Pass variable to href tag within body of an email”

  1. Simply add a link in href="" with ?variable=data1 like below

    <p>Please see attached <a href="http://www.google.com?tag=<?php echo data1;?>">link</a></p>
    
    Reply
  2. I believe your goal as follows.

    • You want to retrieve the values from Spreadsheet and want to put the retrieved values to the HTML template. And, you want to use the HTML data at the HTML body of an email.
    • In your question, you want to put a value of data1 to data1 of <p>Please see attached <a href="data1">link</a></p>.

    I think that in your situation, the scriptlets of <?= ... ?> and <?!= ... ?> can be used. When this is reflected to your script, it becomes as follows.

    HTML side: xyz.html

    <p>Hi, <?= name ?>,</p>
    
    <p>Please see attached <a href="<?= data1 ?>">link</a></p>
    
    • In this case, <?= data1 ?> or <?!= data1 ?> can be used.

    Google Apps Script side: Code.gs

    var n = ws.getRange("A2").getDisplayValue();
    var data1 = ws.getRange("L2").getDisplayValue();
    
    var temp= HtmlService.createTemplateFromFile("xyz");
    temp.name = n;
    temp.data1 = data1;
    var htmlMessage = temp.evaluate().getContent();
    
    GmailApp.sendEmail(
      Email, 
      Subject,"Your email doesn't support HTML.",
      {name: "AD", htmlBody: htmlMessage}
    );
    

    References:

    Reply
  3. To send values from JS to PHP always use Ajax/post and post your values to the PHPfile script and then receive it as $_POST parameter since PHP code runs at the server side, and it doesn’t know anything about what is going on on the client side.

    so you can use

    $.ajax({
      type: "POST",
      url: url,
      data: data,      
       
    });
    

    in some scenarios you can also use localstorage but considering that PHP being client side will be processed first and then client side so manage your variables accordingly

    Reply

Leave a Comment