Forum Discussion

Andrew35's avatar
Andrew35
Community Member
28 days ago
Solved

Javascript for sending an email

Hi there

Hope someone can help tell me what's wrong with my JS for sending an email with Text Entry data.

For context I want the email to go to a specific address and it will contain the text entries from three questions.  Answer could be long so I'm hoping I can get the body to appear as...

Question 1

Text Entry

Question 2

Text Entry

Question 3

Text Entry

The JS used so far is...

var player = GetPlayer();
var email = "Test@Test.com";
var text = player.GetVar("TextEntry") ;
var text = player.GetVar("TextEntry1") ;
var text = player.GetVar("TextEntry3") ;
var subject = "Case Study 1 Answers" ;
var emailBody = "Question 1 "+escape('\r\n') + TextEntry + " Question 2 "+escape('\r\n')+ TextEntry1 + "+escape('\r\n')+ TextEntry3 ;
var mailto_link = 'mailto:' + email + '?subject=' + subject + '&body=' + encodeURIComponent(body_start + texto + body_end);
win = window.open(mailto_link, 'emailWin') ;

 

Hoping someone can help fix this.

Thank you

Andrew

  • Both let and var create new variables in JavaScript. The difference between the two is their scope. 

    • A variable declared by let is only available inside of the block in which it is defined
    • A variable declared by var is available throughout the function in which it is defined, or globally if it's defined outside of a function
  • JoeFrancis's avatar
    JoeFrancis
    Community Member

    Both let and var create new variables in JavaScript. The difference between the two is their scope. 

    • A variable declared by let is only available inside of the block in which it is defined
    • A variable declared by var is available throughout the function in which it is defined, or globally if it's defined outside of a function
  • Basically you got your variables wrong. You have 3 times the variable text... and in your emailBody you refer to TextEntry1,2 and 3... being the inputfields and not the variables.

    It needs to be something like this...

    let player = GetPlayer();
    let ip1 = player.GetVar("TextEntry");
    let ip2 = player.GetVar("TextEntry1");
    let ip3 = player.GetVar("TextEntry2");
    var email="test.test@gmail.com";
    var subject="subject line";
    var body_start="How you want to begin your body." +ip1+" "+ip2+" "+ip3;
    var mailto_link='mailto:'+email+'?subject='+subject+'&body='+body_start;
    win=window.open(mailto_link,'emailWin');

    Sample shared...

    • Andrew35's avatar
      Andrew35
      Community Member

      I just tried your Story file and I get this window pop up...

      Does this look like there's something blocking it e.g. company firewalls or similar?

      Out of interest, should it work whilst in SL or does it need to be published for it to work?

      Thanks.

      Andrew

      • Nedim's avatar
        Nedim
        Community Member

        You should publish to Web or Review 360; otherwise, Storyline will not be able to access your email client from its native environment.

    • Andrew35's avatar
      Andrew35
      Community Member

      Thank you.  I copied what I had seen from others on here.  What does let mean or do then because every other one I've seen had var in this part?

      Also, do you know if this will give me the format I need above?  To be clear Question 1 etc. is not part of the text entry variable.

      Andrew