How to send HTML OTP Email to user for signup?

Hi guys I was creating a email OTP template using doPost function in Google scripts. Actually I have an application in which when user ask for OTP then his details like name,email and app generated OTP are sent to this script. Then it sends the email to the user. But I thought to implement a beautiful html template… But I am stuck in the Google Scriptlets, how to get variable Username and OTP from doPost function and send to html…

Here is my Code…

Code.js

function doPost(e) {
//this function will send emails from your gmail address
var recipient = e.parameters.recipient;
    recipient = decodeURI(recipient);
var subject = e.parameters.subject;
  subject = decodeURI(subject);
var body = e.parameters.body;
  body = decodeURI(body);
var username = e.parameters.username;
  username = decodeURI(username);
var otp = e.parameters.otp;
  otp = decodeURI(otp)

var htmlTemplates = HtmlService.createTemplateFromFile('myMail');
var htmlBody = htmlTemplate.evaluate().getContent();

MailApp.sendEmail({to: recipient, subject: subject, otp: otp, htmlBody: htmlBody});
}

myMail.html

<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<div style="font-family: Helvetica,Arial,sans-serif;min-width:1000px;overflow:auto;line-height:2">
<div style="margin:50px auto;width:80%;padding:20px 0">
<div style="border-bottom:5px solid #eee">
  <a href="" style="font-size:30px;color: #f7c800;text-decoration:none;font-weight:600">AppName</a>
</div>
<p style="font-size:15px">Hello <?= username ?>,</p>
<p>Thank you for choosing Appname. Use this OTP to complete your Sign Up procedures and verify your account on AppName.</p>
<p>Remember, Never share this OTP with anyone.</p>
<h2 style="background: #00466a;margin: 0 auto;width: max-content;padding: 0 10px;color: #fff;border-radius: 4px;"><?= otp ?></h2>
<p style="font-size:15px;">Regards,<br />Team AppName</p>
<hr style="border:none;border-top:5px solid #eee" />
<div style="float:right;padding:8px 0;color:#aaa;font-size:0.8em;line-height:1;font-weight:300">
  <p>App Name Inc</p>
  <p>address</p>
 </div>
</div>
</div>
</body>
</html>

Please help me… I would be thankful to you…

  1. Read / re-read here for passing variables to an html template
    HTML Service: Templated HTML  |  Apps Script  |  Google Developers

  2. Do you have a typo ?

1 var htmlTemplates = HtmlService.createTemplateFromFile('myMail');
2 var htmlBody = htmlTemplate.evaluate().getContent();

Line 1: htmlTemplates
Line 2: htmlTemplate

I have read it already…
But I am facing trouble in doPost function, when url parameters are given from web component of kodular. I am not able to assign those parameters to html file…

I want expect somewhat like this.

I want to send html email instead of plain text as shown in topic below
Topic: Customizeable Email From Google Scripts

Have you correctly published and updated your google apps script web app, & checked that your script url matches the one in the apps script deployment ?

The method is different in the new script editor

Yes I did…

Try this

function doPost(e) {

var recipient = e.parameters.recipient;
    recipient = decodeURI(recipient);
var subject = e.parameters.subject;
  subject = decodeURI(subject);
var body = e.parameters.body;
  body = decodeURI(body);
var otp = e.parameters.otp;

var htmlTemplates = HtmlService.createTemplateFromFile('myMail');
htmlTemplates.otp = decodeURI(otp)
htmlTemplates.username = decodeURI(e.parameters.username);
var htmlBody = htmlTemplates.evaluate().getContent();

MailApp.sendEmail({to: recipient, subject: subject, otp: otp, htmlBody: htmlBody});
}
1 Like

Thank You guys for your involvement…
I got the solution…:innocent::smiley:
Instead of using e parameters seperately, I use JSON stringify and JSON parse
It worked fine for me…
If anyone need help with the code then please mention, I will post…
THANK YOU!

This didn’t worked but thank you for taking interest.:innocent:

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.