Can't get line breaks into email message body

  • Follow


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
	}
}

0
Reply edwardwill (8) 11/1/2007 4:41:10 PM

On Nov 1, 12:41 pm, edwardw...@googlemail.com 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

0
Reply Geoffrey 11/1/2007 7:24:35 PM


On Nov 1, 7:24 pm, Geoffrey Summerhayes <sumr...@gmail.com> wrote:
> On Nov 1, 12:41 pm, edwardw...@googlemail.com 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

0
Reply edwardwill 11/1/2007 8:37:30 PM

edwardwill@googlemail.com wrote:
> On Nov 1, 7:24 pm, Geoffrey Summerhayes <sumr...@gmail.com> wrote:
>> On Nov 1, 12:41 pm, edwardw...@googlemail.com 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>
0
Reply Thomas 11/7/2007 1:09:08 PM

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:john@example.com'
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

0
Reply Bart 11/9/2007 11:12:39 AM

Bart Van der Donck <b...@nijlen.com> 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=E9" would be (both Q and B):

  =3D?ISO-8859-1?Q?Caf=3DE9?=3D
  =3D?ISO-8859-1?B?Q2Fm6Q=3D=3D?=3D

Using 'encodeURIComponent()':

  =3D?UTF-8?Q?Caf=3DC3=3DA9?=3D
  =3D?UTF-8?B?Q2Fmw6k=3D?=3D

--
 Bart

0
Reply Bart 11/9/2007 12:01:44 PM

Geoffrey Summerhayes wrote:

> On Nov 1, 12:41 pm, edwardw...@googlemail.com 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

0
Reply Bart 11/10/2007 9:45:29 AM

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
0
Reply SAM 11/10/2007 2:19:05 PM

SAM wrote:

> Bart Van der Donck a =E9crit :
>
>> 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

0
Reply Bart 11/10/2007 3:31:47 PM

8 Replies
396 Views

(page loaded in 0.107 seconds)

Similiar Articles:













7/23/2012 4:33:10 AM


Reply: