Javascript adding linebreak in mailto body

后端 未结 3 520
礼貌的吻别
礼貌的吻别 2020-12-03 21:02

I\'m setting the body of an email using values from a form

  firstname = bob
  lastname = dole

   ebody = \'First Name: \' + firstname + \'\\r\\n\' + \'Last         


        
相关标签:
3条回答
  • 2020-12-03 21:13

    I would expect outlook to try and output this as html/rich text so in that case you would need something like the following (including a urlencoded br tag):

     firstname = bob
      lastname = dole
    
       ebody = 'First Name: ' + firstname + '%3C%2Fbr%3E' + 'Last Name: ' + lastname
    
      window.location.href = 'mailto:myemail@mycompany.com?subject=test
      email&body=' + ebody;
    
    0 讨论(0)
  • 2020-12-03 21:16

    You can just use the Encoding %0D%0A for line breaks.

    firstname = 'Aung ';
    lastname = 'Kyaw Zaw';
    
    ebody = 'First Name: ' + firstname + '%0D%0A' + 'Last Name: ' + lastname;
    
    window.location.href = 'mailto:myemail@mycompany.com?subject=testemail&body=' + ebody;
    

    http://www.w3schools.com/tags/ref_urlencode.asp

    0 讨论(0)
  • 2020-12-03 21:23

    RFC 2368 says that mailto body content must be URL-encoded, using the %-escaped form for characters that would normally be encoded in a URL. Those characters includes spaces and (as called out explicitly in section 5 of 2368) CR and LF.

    You could do this by writing

    ebody = 'First%20Name:%20' + firstname + '%0D%0A' + 'Last%20Name:%20' + lastname;
    

    but it's easier and better to have JavaScript do the escaping for you, like this:

    ebody = 'First Name: ' + firstname + '\r\n' + 'Last Name: ' + lastname;
    ebody = encodeURIComponent(ebody);
    

    Not only will that save you from having to identify and look up the hex values of characters that need to be encoded in your fixed text, it will also encode any goofy characters in the firstname and lastname variables.

    0 讨论(0)
提交回复
热议问题