How To Send an Email OTP

I have seen this asked about a few times. How to send an email automatically with a one time password.

Step 1 - Build out the app.

things you’ll need:
a GMail account ideally with the name of your app attached to it. [email protected] for example


api component
Screen Shot 2020-11-08 at 8.22.59 AM
google app script account

I have provided a thunkable template for example here

Step 2 - create, save, and deploy the appScript

click the link above and crate a script account and enter the console if you havent already
create a new script
giphy
use this code

function doPost(e) {
  var body = e.parameters.message;
  var subject = e.parameters.subject;
  var who = e.parameters.who;
  
GmailApp.sendEmail(who, subject, body)
}

click publish -> deploy as web app -> deploy/update

now you can send this app information and it will send an email. based on the model above we would send an API POST request to the URL that appscript provides when you deploy the app.

step 3 - create the code to send the email

notice the object property names attached to the API QueryParameters match the parameters from the script code above.
you’ll create an OTP and send it based on some action in the app.


Screen Shot 2020-11-08 at 8.18.40 AM

step 4 - check the OTP

you should have an input in the app to ‘try’ the OTP code
you’ll reference that input in your code and check it against the OTP

2 Likes