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

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.

15 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.

Matthew Bibby

I haven't tested this Jill, but see if this does the trick:

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');

And thanks for your kind words. I'm glad that tutorial helped explain things a bit.

Ashley Terwilliger

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.

Matthew Bibby

I'm glad that worked for you Jill!

The new tab opening is how the email information gets sent to Outlook. 

You could try adding:

win.close();

To the end of the script... but it probably won't work.

There are other ways to generate an email that shouldn't open a new browser window, but I haven't had a chance to explore those options properly at this stage. I'll add it to my to-do list...

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";

Matthew Bibby

Hi Jill,

This might do what you are after:

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;
window.location.href = 'mailto:' + email + '?subject=' + subject + '&body=' + encodeURIComponent(emailBody);

There are a couple of issues with this solution:

  • There is a delay when the button is pressed that may make it seem like nothing is happening. This can be a bit disconcerting.
  • If the learner has a web-based email service (such as Gmail) set as their default email client, the new email will load on the same page as the course - booting them out of the course.

I haven't tested this in an LMS or with Outlook. So let me know how it goes.

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>';