Sign In Register

How can we help you today?

Start a new topic

how to use setHtml with variables

Hi there, I'm working on password recovery system. It's all done and working great. I'm just having trouble beatifying the email that sends the recovery code.

The function to email the code is this

function sendRecoveryEmail(email, name, token) {
    //Here we use sendGrid as an example because we have full integration with their services.
    var myGrid = Spark.sendGrid("username", "[assword]");
    //The email to send the message to and the name of the user
    myGrid.addTo(email, name);
    //Here you'd leave your organization or personal email
    myGrid.setFrom("", "meeeee");
    //The subject of your email
    myGrid.setSubject("Password Recovery");
    // The body and message of your email, here we just send the token
    //Finally send the email

 As of now, you can see I'm using setText to populate the body of the email.

My question is, how do I use setHtml and populate that HTML with the reset token?

My idea is to build a nice recovery email template and replace an element within it with the token.

How can this be done? Thanks for your time.

1 Comment

So you can literally just stick your html code in as a string here, though it is reaaallly messy, it does work.


    "<div style=\"width:579px;margin:auto\" align=\"left\">" +
    "<div style=\"padding:20px\" align=\"left\">" +
    "<img src=" + topImage + "></img>" + "<br /> <br />" +
    "Hello There, <br /> <br /> You player, <strong>"+playerName+"</strong> has registered an email address with your *company name*. to "+
    "enable the Password Reset feature. <br /> <br /> This would allow an email to be sent to you to reset this player's password in the future, if it is forgotten."+ 
    "Your email address will not be shared, sold, or used for any other purpose. "+
    "<br /> <br /> Please "+
    "<a href="+verify+"> click here </a>"+
    " to allow the association of your email address with this player's account."+
    "<br /> <br /> Please "+
    "<a href="+dismiss+"> click here </a>"+
    " if you do not wish to accept this registration."+
    "<br /> <br />Contact us at <a href="+contactURL+"> for more information.</a>."+ 
    "<br /> <br /> <em></em> <br /> <br /> Sincerely,<br /> "+
    "*The Company*</p>" + "</div> </div>";


Another option that works is to create your html in whatever web-dev IDE you like. Then save that as a downloadable on GameSparks.
You can then get that html as text from within your script and set that in your email.
There is a brief example of this below...


function passwordResetHTMLform(reset_code, playerId){

    var request = new SparkRequests.GetDownloadableRequest();
    request.shortCode = 'password_reset';
    var response = request.ExecuteAs(playerId);
    var str = response.url;
    Spark.getLog().debug("Reset Form: "+  JSON.stringify(response) );
    var resp = Spark.getHttp(response.url).get();
    var htmlString = resp.getResponseString();
    Spark.getLog().debug("BEFORE: "+htmlString);
    htmlString  =  htmlString.replace("[CODE]", reset_code);
    htmlString  =  htmlString.replace("[URL]", getCorrectEmailURL());
    Spark.getLog().debug("AFTER: "+htmlString);

    return htmlString;


This example also has fields in the html you can populate with your own data, specific to the player.

1 person likes this
Login to post a comment