简体   繁体   中英

Using a variable in the body of a HTML email as URL in a hyperlink

I have a Google script that sends out a HTML email from a spreadsheet. One cell in the spreadsheet holds an URL that is changeable as it is composed from values of other cells. In my GS I read this URL into a var. Now I want to use this var in my HTML code that renders the email so that the email receiver can click on a link that opens this custom URL.

I can not find a solution to replace the fixed URL with a variable that holds my custom URL Please check the HTML code where it says " " that's where I'm placing a var, but doing it obviously wrong.

**** Google script*****

var CO_PP = COSheet.getRange(2,11).getValue() ; 
var CO_PP_href =  ("<a href=\"" + CO_PP + "\">")  ;  //  use a backslash \ to escape the quotation marks. 

var EmailBody =  HtmlService.createTemplateFromFile(template); 
           EmailBody.EmailVar1 = CO_Name;
           EmailBody.EmailVar2 = CO_Email;
           EmailBody.EmailVar13 = CO_PP_href;


 var MyHtmlBody = EmailBody.evaluate().getContent()
 var EmailSubject = "Credits Order SUCCES  .:: CADsherpa Weblink ::."
 MailApp.sendEmail(CO_Email, EmailSubject, "Your emailreader does not support HTML." + 
         " Try opening this message with a different email reader or take contact with info@cadsherpa.com", {htmlBody: MyHtmlBody, attachments: PDF_ToSend });






*******HTML*******

<!-- button start -->  
    <div>
    <p>&nbsp;</p>

<table align="left" border="0" cellpadding="1" cellspacing="1" style="height:10px;width:185px;">
    <tbody>
        <tr>
            <td style="white-space: nowrap; text-align: center; vertical-align: middle; background-color: rgb(51, 102, 255);">
            <h3> +<?= EmailVar13 ?>+ <span style="font-size:18px;">  <!-- PROBLEM ON THIS LINE -->  
            <strong><em><span style="font-family:arial,helvetica,sans-serif;">
            <span style="color:#FFFFFF;">Pay with PayPal </span></span></em></strong></span></a></h3>
            </td>
        </tr>
    </tbody>
</table>

<p>&nbsp;</p>

    </div>                   
 <!-- button end -->   

When using HTML templating in GAS, the variables assigned to the template will be escaped. That means that in your case, if CO_PP_href was set to the expression "<a href=\\"" + CO_PP + "\\">" the actual tags will not be placed into the HTML, but rather the escaped version ( &lt;a href=&#34;...&#34;&gt; ) so that it can be treated as printable "text" within your page.

For your needs, I propose a different solution. You can firstly create the <a> tag inside the html template, and only the href attribute thereof will be set when evaluating the template. The solution would look as follows:

HTML

<!-- button start -->  
<div>
<p>&nbsp;</p>

<table align="left" border="0" cellpadding="1" cellspacing="1" style="height:10px;width:185px;">
    <tbody>
        <tr>
            <td style="white-space: nowrap; text-align: center; vertical-align: middle; background-color: rgb(51, 102, 255);">
            <h3> <a href="<?= EmailVar13 ?>"> <span style="font-size:18px;">
            <strong><em><span style="font-family:arial,helvetica,sans-serif;">
            <span style="color:#FFFFFF;">Pay with PayPal </span></span></em></strong></span></a></h3>
            </td>
        </tr>
    </tbody>
</table>

<p>&nbsp;</p>

</div>                   
<!-- button end -->   

GAS

var CO_PP = COSheet.getRange(2,11).getValue() ; 

var EmailBody = HtmlService.createTemplateFromFile(template); 
EmailBody.EmailVar1 = CO_Name;
EmailBody.EmailVar2 = CO_Email;
EmailBody.EmailVar13 = CO_PP;

var MyHtmlBody = EmailBody.evaluate().getContent()
var EmailSubject = "Credits Order SUCCES  .:: CADsherpa Weblink ::."
MailApp.sendEmail(CO_Email, EmailSubject, "Your emailreader does not support HTML." + 
         " Try opening this message with a different email reader or take contact with info@cadsherpa.com", {htmlBody: MyHtmlBody, attachments: PDF_ToSend });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM