Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Can't get line breaks into email message body

Reply
Thread Tools

Can't get line breaks into email message body

 
 
edwardwill@googlemail.com
Guest
Posts: n/a
 
      11-01-2007
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
}
}

 
Reply With Quote
 
 
 
 
Geoffrey Summerhayes
Guest
Posts: n/a
 
      11-01-2007
On Nov 1, 12:41 pm, (E-Mail Removed) 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

 
Reply With Quote
 
 
 
 
edwardwill@googlemail.com
Guest
Posts: n/a
 
      11-01-2007
On Nov 1, 7:24 pm, Geoffrey Summerhayes <(E-Mail Removed)> wrote:
> On Nov 1, 12:41 pm, (E-Mail Removed) 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(o Subject)
> +"&body="+escape(oBody);


Thanks Geoff, that worked a treat!

Edward

 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      11-07-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> On Nov 1, 7:24 pm, Geoffrey Summerhayes <(E-Mail Removed)> wrote:
>> On Nov 1, 12:41 pm, (E-Mail Removed) 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(o Subject)
>> +"&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>
 
Reply With Quote
 
Bart Van der Donck
Guest
Posts: n/a
 
      11-09-2007
Thomas 'PointedEars' Lahn wrote:

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


>> .href="mailto:"+oEmailAddress+"?subject="+escape(o Subject)
>> +"&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()', '(E-Mail Removed)'
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

 
Reply With Quote
 
Bart Van der Donck
Guest
Posts: n/a
 
      11-09-2007
Bart Van der Donck <(E-Mail Removed)> 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

 
Reply With Quote
 
Bart Van der Donck
Guest
Posts: n/a
 
      11-10-2007
Geoffrey Summerhayes wrote:

> On Nov 1, 12:41 pm, (E-Mail Removed) 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(o Subject)
> +"&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

 
Reply With Quote
 
SAM
Guest
Posts: n/a
 
      11-10-2007
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
 
Reply With Quote
 
Bart Van der Donck
Guest
Posts: n/a
 
      11-10-2007
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Beginner: read $array with line breaks line by line Marek Stepanek Perl Misc 12 09-02-2006 10:27 AM
Force multi line field value to output with line breaks? bernadou ASP .Net Web Controls 2 01-23-2006 01:23 PM
Pasting JTable content into the body of an email message Marc Van Laer Java 0 04-11-2005 04:13 PM
inserting a line into the body of an eMail before forwarding Lisa Perl Misc 11 12-21-2004 07:50 AM
Maintaining line breaks in HTML email Andrew Banks ASP .Net 2 04-04-2004 04:48 PM



Advertisments