Can't get line breaks into email message body

Discussion in 'Javascript' started by edwardwill@googlemail.com, Nov 1, 2007.

  1. Guest

    ASP.NET

    I have a function on my aspx page to format an email message and send
    it. The function is below.

    The problem is that I want the body to be formatted with CR/LF -
    newlines - between the CS No, the Customer Reference No. and the
    body. E.g.

    CSNo: ABC123
    Customer Ref: 321321
    This is the body of the text

    What my function gives me when I look at the email message in the mail
    client (Outlook) is

    CS No: ABC123Customer Ref: 321321This is the body of the text

    In other words, no carriage returns.

    Any thoughts?

    Thanks

    Edward

    function BuildEmailResponse(id)
    {
    try
    {
    var gridrowindex;
    if (!(oRow = GetRow(window.event.srcElement))) return true;
    gridrowindex = oRow.rowIndex + 2;
    var editcontrolprefix = "dgSearch__ctl" + gridrowindex + "_";
    var oEmailAddress =
    document.getElementById('txtContactEmail').value;
    var oSubject =
    document.getElementById('txtSubject').value;
    if (oSubject.length == 0)
    {
    oSubject = "[Blank]";
    }
    var oBody = 'CS No: ' + document.getElementById('txtCSNo').value +
    '\r\n';
    oBody += 'Customer Ref: ' +
    document.getElementById('txtCustomerRef').value + '\r\n';
    oBody += document.getElementById(editcontrolprefix +
    'txtResponse').value;
    document.getElementById(id).href = "mailto:" + oEmailAddress + "?
    subject=" + oSubject + "&body=" + oBody
    }
    catch(e)
    {
    document.getElementById(id).href = "mailto:" + oEmailAddress + "?
    subject=" + oSubject
    }
    }
     
    , Nov 1, 2007
    #1
    1. Advertising

  2. On Nov 1, 12:41 pm, wrote:
    > ASP.NET
    >
    > I have a function on my aspx page to format an email message and send
    > it. The function is below.
    >
    > The problem is that I want the body to be formatted with CR/LF -
    > newlines - between the CS No, the Customer Reference No. and the
    > body. E.g.
    >
    > CSNo: ABC123
    > Customer Ref: 321321
    > This is the body of the text


    ..href="mailto:"+oEmailAddress+"?subject="+escape(oSubject)
    +"&body="+escape(oBody);

    ---
    Geoff
     
    Geoffrey Summerhayes, Nov 1, 2007
    #2
    1. Advertising

  3. Guest

    On Nov 1, 7:24 pm, Geoffrey Summerhayes <> wrote:
    > On Nov 1, 12:41 pm, wrote:
    >
    > > ASP.NET

    >
    > > I have a function on my aspx page to format an email message and send
    > > it. The function is below.

    >
    > > The problem is that I want the body to be formatted with CR/LF -
    > > newlines - between the CS No, the Customer Reference No. and the
    > > body. E.g.

    >
    > > CSNo: ABC123
    > > Customer Ref: 321321
    > > This is the body of the text

    >
    > .href="mailto:"+oEmailAddress+"?subject="+escape(oSubject)
    > +"&body="+escape(oBody);


    Thanks Geoff, that worked a treat!

    Edward
     
    , Nov 1, 2007
    #3
  4. wrote:
    > On Nov 1, 7:24 pm, Geoffrey Summerhayes <> wrote:
    >> On Nov 1, 12:41 pm, wrote:
    >>> ASP.NET
    >>> I have a function on my aspx page to format an email message and send
    >>> it. The function is below.
    >>> The problem is that I want the body to be formatted with CR/LF -
    >>> newlines - between the CS No, the Customer Reference No. and the
    >>> body. E.g.
    >>> CSNo: ABC123
    >>> Customer Ref: 321321
    >>> This is the body of the text

    >> .href="mailto:"+oEmailAddress+"?subject="+escape(oSubject)
    >> +"&body="+escape(oBody);

    >
    > Thanks Geoff, that worked a treat!


    However, the standards compliant UTF-8-safe encodeURIComponent() instead
    of the proprietary escape() is recommended here. And using `mailto:' is
    recommended against because it is unreliable, especially as you have
    ASP(.NET) available to provide for a reliable server-side mailer.

    ....location = [
    "mailer.aspx?to=", encodeURIComponent(oEmailAddress),
    "&subject=", encodeURIComponent(oSubject),
    "&body=", encodeURIComponent(oBody)
    ].join("");

    Or, even better, let the user submit a form like this:

    <form action="mailer.aspx" method="POST" ...>
    <input name="email" ...>
    <input name="subject" ...>
    <textarea name="msgbody" ...></textarea>
    <input type="submit" ...>
    </form>

    No client-side scripting will be required then, although you could use the
    `onsubmit' intrinsic event handler attribute to provide for client-side form
    validation where it is supported.


    PointedEars
    --
    "Use any version of Microsoft Frontpage to create your site. (This won't
    prevent people from viewing your source, but no one will want to steal it.)"
    -- from <http://www.vortex-webdesign.com/help/hidesource.htm>
     
    Thomas 'PointedEars' Lahn, Nov 7, 2007
    #4
  5. Thomas 'PointedEars' Lahn wrote:

    > On Nov 1, 7:24 pm, Geoffrey Summerhayes wrote:


    >> .href="mailto:"+oEmailAddress+"?subject="+escape(oSubject)
    >> +"&body="+escape(oBody);

    >
    > However, the standards compliant UTF-8-safe encodeURIComponent()
    > instead of the proprietary escape() is recommended here.


    In this 'mailto:'-example, 'escape()' is better and much safer.

    For instance, using 'encodeURIComponent()', 'mailto:'
    would be converted into 'john%40example.com'. It is recommended to
    keep the non-percent-encoded '@' in every mailto-hyperlink.

    Then, a subject line may never be URI-encoded, neither by 'escape()'
    nor by 'encodeURIComponent()'. Please refer to sections 4.2. and 8 in
    RFC2047 about Q-encoding in subject lines.

    Applying 'encodeURIComponent()' to the body of the message would be
    unwise too; it assumes that the default character set of the email
    client is UTF-8. Under all Windows/Office email-clients, this is not
    the case by default.

    > And using `mailto:' is recommended against because it is unreliable,
    > especially as you have ASP(.NET) available to provide for a reliable
    > server-side mailer.
    >
    > ....location = [
    > "mailer.aspx?to=", encodeURIComponent(oEmailAddress),
    > "&subject=", encodeURIComponent(oSubject),
    > "&body=", encodeURIComponent(oBody)
    > ].join("");


    This is entirely different strategy; you are making another GET-
    request over HTTP here, thus passing the responsibility for the
    sendout of the email to the server.

    If mail.aspx knows that the query string is encoded in UTF-8, and can
    correctly parse it, this should work fine. I would recommend
    'encodeURIComponent()' too here.

    But in both cases, I would be concerned about the URI-length in GET
    requests holding entire email bodies. RFC 2068:

    | Servers should be cautious about depending on URI lengths above
    | 255 bytes, because some older client or proxy implementations
    | may not properly support these lengths.

    > Or, even better, let the user submit a form like this:
    >
    > <form action="mailer.aspx" method="POST" ...>
    > <input name="email" ...>
    > <input name="subject" ...>
    > <textarea name="msgbody" ...></textarea>
    > <input type="submit" ...>
    > </form>


    I agree. But UTF-8 precautions should be taken here too.

    --
    Bart
     
    Bart Van der Donck, Nov 9, 2007
    #5
  6. Bart Van der Donck <> wrote:

    > Then, a subject line may never be URI-encoded, neither by 'escape()'
    > nor by 'encodeURIComponent()'. Please refer to sections 4.2. and 8 in
    > RFC2047 about Q-encoding in subject lines.


    It appears I am wrong about this.

    A correct 'escape()' for Subject Line "Café" would be (both Q and B):

    =?ISO-8859-1?Q?Caf=E9?=
    =?ISO-8859-1?B?Q2Fm6Q==?=

    Using 'encodeURIComponent()':

    =?UTF-8?Q?Caf=C3=A9?=
    =?UTF-8?B?Q2Fmw6k=?=

    --
    Bart
     
    Bart Van der Donck, Nov 9, 2007
    #6
  7. Geoffrey Summerhayes wrote:

    > On Nov 1, 12:41 pm, wrote:
    >> The problem is that I want the body to be formatted with CR/LF -
    >> newlines - between the CS No, the Customer Reference No. and the
    >> body. E.g.

    >
    >> CSNo: ABC123
    >> Customer Ref: 321321
    >> This is the body of the text

    >
    > .href="mailto:"+oEmailAddress+"?subject="+escape(oSubject)
    > +"&body="+escape(oBody);


    I think you should not use 'escape()' here; what matters is which end-
    of-line characters were used in the original string.

    alert(escape('\n'));
    alert(escape('\r'));
    alert(escape('\r\n'));

    RFC2368 is very explicit about line-ends in emails:

    | Also note that line breaks in the body of
    | a message MUST be encoded with "%0D%0A".

    (http://rfc.net/rfc2368.html - Chapter 5 'Encoding')

    You should be okay with 'escape()' if your original string uses '\r\n'
    as line-end. I would write out '%0D%0A' by hand in the original
    variable for maximum safety.

    --
    Bart
     
    Bart Van der Donck, Nov 10, 2007
    #7
  8. SAM Guest

    Bart Van der Donck a écrit :
    >
    > RFC2368 is very explicit about line-ends in emails:
    >
    > | Also note that line breaks in the body of
    > | a message MUST be encoded with "%0D%0A".
    >
    > (http://rfc.net/rfc2368.html - Chapter 5 'Encoding')
    >
    > You should be okay with 'escape()' if your original string uses '\r\n'
    > as line-end. I would write out '%0D%0A' by hand in the original
    > variable for maximum safety.


    Only using %0A seems to work for me.
    Witch emailer needs the \r ?


    --
    sm
     
    SAM, Nov 10, 2007
    #8
  9. SAM wrote:

    > Bart Van der Donck a écrit :
    >
    >> RFC2368 is very explicit about line-ends in emails:

    >
    >> | Also note that line breaks in the body of
    >> | a message MUST be encoded with "%0D%0A".

    >
    >> (http://rfc.net/rfc2368.html- Chapter 5 'Encoding')

    >
    >> You should be okay with 'escape()' if your original string uses '\r\n'
    >> as line-end. I would write out '%0D%0A' by hand in the original
    >> variable for maximum safety.

    >
    > Only using %0A seems to work for me.


    That is perectly possible; as there will be others where only %0D
    works.

    > Witch emailer needs the \r ?


    Long story, it began in 1960[*], and still causing ambiguity today :)

    http://en.wikipedia.org/wiki/Newline
    http://en.wikipedia.org/wiki/Carriage_return (\r)
    http://en.wikipedia.org/wiki/Line_feed (\n)

    [*] http://en.wikipedia.org/wiki/Smith_Corona

    --
    Bart
     
    Bart Van der Donck, Nov 10, 2007
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andrew Banks

    Maintaining line breaks in HTML email

    Andrew Banks, Apr 4, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    923
    Andrew Banks
    Apr 4, 2004
  2. Marc Van Laer
    Replies:
    0
    Views:
    434
    Marc Van Laer
    Apr 11, 2005
  3. bernadou

    Force multi line field value to output with line breaks?

    bernadou, Jan 22, 2006, in forum: ASP .Net Web Controls
    Replies:
    2
    Views:
    392
    bernadou
    Jan 23, 2006
  4. Lisa
    Replies:
    11
    Views:
    256
    Gunnar Hjalmarsson
    Dec 21, 2004
  5. Marek Stepanek
    Replies:
    12
    Views:
    440
    Peter J. Holzer
    Sep 2, 2006
Loading...

Share This Page