Send OTP via Email using HTML template in UiPath
Hi welcome to another post of Codehunters.
In this post we will generate a 6 digit alphanumeric code and send it via email using a HTML Template.
Here is my HTML template this is prebuilt, I just need to send this template via email by updating the OTP here.
Launch UiPath, and open a blank project.
We will divide this project in three parts :
The First thing we will do is to generate the OTP.
Second, we will update the template with the generated OTP
Third we will send the template with EMail Body.
To Generate OTP, we will need a variable to hold our Random number we will create a variable and call it strOTP, and Drag Assign Activity from the activities panel.
There are multiple ways to create an alphanumeric string, in this video we will use GUID Method to generate our random string. GUID stands for Global Unique Identifier. It creates a random alphanumeric number of 32 characters something like this.
But we will only use first 6 characters from this for our OTP.
So in the assign Activity write the expression to generate the GUID the expression is GUID.NewGUID().ToString this will create a GUID but we need only first 6 characters so we will split it using Substring so the complete expression will be GUID.NewGUID().ToString.Substrng (0,6).toupper, to get all text in uppercase. this will give us only the first 6 characters from GUID. Your process should look like this.
Now the second step is to Read the HTML Template File and update it
Now as we have created the OTP we need to update this OTP in out Template file.
Create another sequence and name it as Update OTP Template.
We need to read the HTML file we will use Read Text Activity to read the HTML in string format so drag the Read Text File activity and provide the HTML file and to hold the value we will create a variable and call it strTemplate.
To update the value we will use String Replace function so drag and drop the Assign activity and we will replace in same strTemplate Varialbe so assign strTempalte an expression the expression will be strTemplate.Replace(“#OTP#”, strOTP) this is the new value this will replace the #OTP with our value in the OTP variable.
Now we have the Tempalte ready awe need to send the email.
For that we will use SMTP Send Mail. We will be using GMAIL, To access Gmail programmatically you can’t use your regular password you will need to create a App Specific password for that. We already have a video you can go through that to know how to create a App Password for Gmail.
Drag and Drop the Send SMTP Email, and update the properties the body will b our strTemplate, next Subjet we will type subject as “You OTP to Validate Email Address” “smtp.gmail.com”Email is your email addres and password is your Genereated App Specific password and enable the isHTML property and in the To type an email address I am using a fixed email address here you can use variables here as well.
In the From Use same email address by which you are sending email and type the name.
After this we will add a message box that says EMail Sent so that we know that the process is completed.
And thats IT. So just to recap we have generate the OTP using GUID and then trimming the first 6 characters then we have updated out HTML template with our OTP and then sending the via email in BODy.
Lets run this, once the run is completed you should receive email in your inbox
So this is how you can generate and Send OTP using a Template in UiPath.
Hope this helps and I will see you in the next Post. Till then Code with Confidence