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. appName@gmail.com 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

12 Likes

Hi, could you also show the way of doing this as a SMS or whatsapp message rather than a gmail !!!

I love your Tutorial :partying_face:

Hey guys. When you copy this app, please change the recipient of the email in the blocks. I keep getting those otp emails. :joy:

Eventually I will update the project.

2 Likes

do you have a project link? if you have please do send it to me!
thanks!

Hiya!

Im managing to send an email, but the response im getting is ‘Load failed’. How can I correctly receive the status code?

my blocks are as follows:

  1. So you’re saying that it is successfully sending an email message?
  2. What is the exact message you’re seeing? Is it from the error block or response block?
1 Like
  1. I am successfully sending the email.

  2. The message I get from the status block is ‘undefined’
    The message from the error block is ‘load failed’
    The message from the response block is just blank…

FYI this is how im testing:

1 Like

Load Failed is an error from the Google Apps Script not from Thunkable.

1 Like

Can you share the code you’re using also?

2 Likes

Script and execution log…

Uploading: Screenshot 2022-05-09 at 07.26.44.png…

It is undefined because your script is not returning anything back to the calling app.

Load Failed need to be checked where it comes from and in late 2021 there was a problem when running web deployed Google script from iOS devices.

Blank because you are not sending anything back from your script.

1 Like

Hi all,

I live tested on my device and got the values I needed returned…problem solved :smiley:

Just for my own personal knowledge, why does this work on the live test and not in the web preview?

1 Like

Most APIs will not work in web preview due to privacy settings from the server side, also called CORS.

1 Like

Thank you so much for the tutorial, however, the email does not want to send. the API always seems to fail in sending the email. Can you please check my code for any errors?