How to Format the Body of an Email Using Java Script?

Aug 17, 2021

Does anyone know how to format the body of email using java script? (Or can point me to a good resource?)

Thanks to the E-Learning Hereos, I've been able to create a working prototype in SL 360 that consolidates multiple text entry variables into an email, but the text in the body of the email does not have any spacing or formatting and is all strung together. I would like the text from each variable to be separated by a paragraph break. 

var player = GetPlayer();
var email=player.GetVar("EnterEmailAddress");
var subject="The Top 5 Operational Risks in My Job";
var body_start=player.GetVar("Risk1and2")+player.GetVar("Risk3and4");
var mailto_link='mailto:'+email+'?subject='+subject+'&body='+escape(body_start);,'emailWin');

I would think I should be able to add some code to the "var body_start=player.GetVar" line? I just don't know... not my language. 

Thanks, - Paul

5 Replies
Paul DeHorn

Hello Walt, 

Well, your suggestion on adding spaces works, but I can't get line breaks to work. I learned that "\n" is a line break in JavaScript, but I can't get this to work. I tried using +\n and, +"\n", and just \n in between each player.GetVar just like the +" " but when I do I break the JavaScript and the email doesn't launch. I realize something must be wrong with the syntax I'm using?

If I wanted to it to look like this in the body of the email, do you know what the syntax should be for the JavaScript?




Thanks, - Paul

Walt Hamilton

Try the line breaks in the thread above. I don't remember what they are, but I'm not sure I ever got the /n to work.

Your best troubleshooting tool is to add this line to your script:

alert("works to here");

JS is executed until it finds an error, then it just stops without any error message. If that alert line is executed, it will show a dialog box, and you will know that the script is working up to that point. Continue to move the alert line down in the code. Eventually, you will find that it works at one point, but one line later, it doesn't. Something is wrong with the previous line.