Can't get line breaks into email message body

E

edwardwill

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

Geoffrey Summerhayes

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);
 
T

Thomas 'PointedEars' Lahn

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
 
B

Bart Van der Donck

Thomas said:
On Nov 1, 7:24 pm, Geoffrey Summerhayes wrote:

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:[email protected]'
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.
 
B

Bart Van der Donck

Bart Van der Donck said:
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=?=
 
B

Bart Van der Donck

Geoffrey said:
.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.
 
S

SAM

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 ?
 
B

Bart Van der Donck

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top