How to pull content of variable into body of email using javascript?

Nov 06, 2016

The javscript below is working fine; the email window opens with the addressee filled in and a subject line.  The point of sending this email from inside the course is to send the value of a variable. Will someone who knows javascript add the correct lines to do this?  

The variable is a text entry variable with 50- 100 words typed by the learner.

var email="yourAddress@email.com";
var subject="subject line";
var body_start="How you want to begin your body.";
var mailto_link='mailto:'+email+'?subject='+subject+'&body='+body_start;
win=window.open(mailto_link,'emailWin');

Thanks for your help.

11 Replies
Jill Freeman

Matthew, your tutorial is so helpful! I was already playing with the script but it wasn't making a lot of sense.

I want to do one more thing and am having trouble making it work. Can you help?

I want to include the learner's username in the email body.

In my SL2 file, I have a text entry (essay question) with a variable called "words" that captures the user's input. (I also have a variable on the same screen called "Counted" that captures the number of "words" and when user presses a button it displays a count. The client wants 50-100 words only.)

I have another variable called "username" where SL asks the user to type in their company username into a text entry box on a previous screen.

Here's my code:

var player =  GetPlayer();

var email = 'roc@company.com'

var getwords = player.GetVar('words');

var subject = 'ROC User Input';

var getusername = player.GetVar('username');

var emailBody = 'getusername' + submitted these things that he/she learned in Module 1:' + '\n' + getwords;

var mailto_link = 'mailto:' + email + '?subject=' + subject + '&body=' + encodeURIComponent(emailBody);

win = window.open(mailto_link, 'emailWin');

I've published this and tested on my local hard drive as HTML5 and Flash. The username won't pull in.   Any help is appreciated! Thank you.

Ashley Terwilliger-Pollard

Jill,

You'll want to test the published output by uploading it to the intended environment vs. testing it locally. The reason being that the browser security for testing content locally could be interfering and you can see a bit more information about that here.  

If you don't have a web server to test on, you could also look at using Tempshare or Amazon S3 as mentioned in that article and here are some more detailed directions for Amazon S3.

Jill Freeman

Thanks. You are right, adding win.close(); tried to close the current window, not the new one.  I tried window.location.href  and it opened only my email, without the Outlook popup warning, and without a new browser tab. Great...but the email is blank and does not pull in the data from the variables.

Do you think this would work, if I had the script right?

var player = GetPlayer();
var email = 'roc@company.com';
var getwords = player.GetVar('words');
var subject = 'ROC User Input for Module 1';
var getusername = player.GetVar('username');
var emailBody = getusername + ' submitted these things that he/she learned in Module 1:' + '\n' + getwords;
var window.location.link = 'mailto:' + email + '?subject=' + subject + '&body=' + encodeURIComponent(emailBody);
window.location.href = "mailto:roc@company.com";

Russell Killips

Hi Bruce,

I would try building the body like an html table.

Try something like this:

var body = '<table><tr><td>Q</td><td>WordProcessing</td><td>Spreadsheets</td><td>Email</td></tr>';

body += '<tr><td>1</td><td>'+WP01+'</td><td>'+SSQ01+'</td><td>'+EMQ01+'</td></tr>';

body += '<tr><td>2</td><td>'+WP02+'</td><td>'+SSQ02+'</td><td>'+EMQ02+'</td></tr>';

body += '<tr><td>3</td><td>'+WP03+'</td><td>'+SSQ03+'</td><td>'+EMQ03+'</td></tr>';

.

.

.

body += '<tr><td>Total Score=</td><td>'+TOTALscore+'</td></tr></table>';

 

This discussion is closed. You can start a new discussion or contact Articulate Support.