««Aug 2008»»
SMTWTFS
      12
3456789
10111213141516
17181920212223
24252627282930
31

Search TipicalCharlie

 


Most Popular Tags

   

Blog Status

  • 3 yrs 17 wks 3 days old
  • Updated: 1 Aug 2008
  • 70 entries
  • 129 comments
Tipical Charlie
Welcome to Tipical Charlie, a repository of all kinds of tips related to computing, from web developer and technologist, Charlie Arehart.
I'll mostly share my own tips that I've found others enjoyed hearing about. I'll welcome tips from others, too.
(Wondering where I came up with the name?)

Forcing a line break in an HTML email link

posted Wednesday, 18 May 2005

OK, this is the first tip I've done that's about HTML, so it may not suit all readers, but for those who do work with HTML, it may be a useful discovery. did you know you can both pre-load the body AND force a line break within that body of an email?

Most HTML (of CFML, JSP, ASP, PHP) developers already know that you can create a hyperlink that opens an email for the browser user, using:

<a href="mailto:someaddress@somedomain.com">create email</a>

And you may further know that you can add, in effect, query string values with keywords that pre-populate the email message with the CC, BCC, SUBJECT, and BODY values. So a more complete link might be:

<a href="mailto:someaddr@somedomain.com?cc=someotheraddr@somedomain.com">create email</a>

Note the use of a single ? character to separate the first keyword from the to address. To add multiple keywords, use &'s to separate any subsequent keywords:

<a href="mailto:someaddr@somedomain.com?cc=someotheraddr@somedomain.com&subject=some subject&body=some body text">create email</a>

(That would all be on one line. The split is being forced by my blog editor).

But a trick many may not know is that when forming the BODY values, you can cause the body text to have a new line within the text you pre-populate by using the ascii characters for a carriage return (%0D) and line feed (%0A), as in:

<a href="mailto:someaddress@somedomain.com?cc=someotheraddress@somedomain.com&subject=some subject&body=Some body text.%0D%0ASome new line.">create email</a>

This will create an email with a body showing:

Some body text.
Some new line.

Note that there have been reports that some browser/email client combinations don't honor that setting, so be sure to test it before relying on it for mission-critical email processing.

links: digg this    del.icio.us    technorati    reddit




1. a reader left...
Friday, 27 May 2005 3:19 pm

I'd just like to say thank you for the mailto: script lesson. Out of all the sites I located online with information about line breaking in the body of an email, ONLY your site and wisdom gave me the answer I was looking for. Thanks again and bless your soul for showing others the way! Your great!

Bill Daly


2. Charlie Arehart left...
Sunday, 5 June 2005 11:03 pm

Thanks you, kindly, Bill. It's comments like that which help motivate folks like me to share tips like this. :-) Glad to have helped.


3. Swetha Khetavath left...
Monday, 15 May 2006 6:38 pm

Thanks so much for the useful tip. I was worried that I wouldnt be able to find a solution for inserting line breaks in the HTML Email Link and this is indeed the only site that gave this information :)


4. Charlie Arehart left...
Thursday, 18 May 2006 12:18 am

Swetha, glad I could help. As with Bill who had commented similarly when he found the tip last year, I'm actually amazed that you guys are finding this in your searching. Fortunately I must have used some words that relate to what you folks are using when you search! :-)

Anyway, glad I could help.


5. viet left...
Wednesday, 9 August 2006 12:05 pm

Thank you for your good hint.

I have one more question. How can we change format of the mailbody? For example, if I want to Bond or Italic a part of the mail body.

Thank you.


6. Charlie Arehart left...
Sunday, 13 August 2006 9:37 am

Viet, I'm afraid that all depends on the mail clients (both yours and those of your intended recipient). You could of course put HTML <b>bold</b> and <i>italics</i> tags around your desired content, but that doesn't mean that it will show that way to users. It may instead appear as &lt;b&gt;bold&lt;/b&gt;, for instance, just showing the tags to the user. The thing is, some mail clients support using HTML, and some do not.

Further, and far more important in this situation, is what happend in YOUR mail client when the A HREF MAILTO opens a new email for you. Will your mail client format the message as HTML? Or plain text?

This will depend typically on your changing a formatting setting setting for your outgoing message(s). In outlook, for instance, you can use Format>HTML when in a new mail message, or you can change that setting permanently in Tools>Options>Mail Format.

But the mailto directive just opens an email in your default mail client and fills in standard fields. It can't cause any such settings in your mail programs for you.

If you do some google searching on this subject, I'm sure others have had thoughts.


7. I Have left...
Thursday, 31 August 2006 1:11 pm

Great code for the line breaks. Works impeccably in versions of Outlook bundled with Windows 2000. Am I the only one who took a minute to realize those are zeros in %0D%0A and not o's?

Thanks Again


8. Petiflo left...
Thursday, 31 August 2006 2:26 pm

Hi,

Very great indeed, but is it possible with this method to insert the default signature inside the email body?

Thank you.


9. Charlie Arehart left...
Saturday, 2 September 2006 3:08 pm

Thanks for clarifying that the %0d is a zero not the letter o. Sorry I didn't think to clarify that. As for the person asking about using this approach to add a signature inside the mail body, sure, no reason it wouldn't work (for a text-based signature only, of course.) The signature is just more text to be put inside the email, right?


10. Kersi left...
Wednesday, 18 October 2006 11:03 am

Thanks. Very useful and clear help. Excellent. I have added a aid too. :)


11. Barbara Young left...
Friday, 20 October 2006 12:12 pm

I found your hint when I was asked to create a sample e-mail for our employees to send to their children's teachers. I was not only able to preformat the body of the e-mail using your hint, but I discovered another useful thing: You can place any text in the To: portion of the e-mail link. For example, I used "mailto: enter teacher's e-mail address here," and that's exactly what shows up when you click the link and it opens a new e-mail message (Outlook in this case; may not work for all e-mail clients).


12. Charlie Arehart left...
Friday, 20 October 2006 1:31 pm

Kersi and Barbara, glad you found it useful, and nice bonus there, Barbara. Thanks. :-)


13. Matthew Something left...
Thursday, 23 November 2006 8:19 am

Thanks for your very helpful tip. It saved me from much research.


14. Andy left...
Monday, 18 December 2006 12:09 am

Thanks for this tip You. Are. A. Legend.


15. Kosty left...
Friday, 5 January 2007 9:33 am

I especilialy thank you for the "new line" hint, THANK YOU !


16. NK left...
Tuesday, 16 January 2007 11:18 am

Guys, can anyone help with Bolding the text of the email body. Thanks.


17. Charlie Arehart left...
Wednesday, 17 January 2007 1:07 am

NK, this has been discussed above in the comments. See the question from "Viet" and my answer to him. I'm afraid that's all I can think of, and no one has added more on it.


18. Druv left...
Thursday, 25 January 2007 11:18 am

Thanks you saved my day! :)


19. Druv left...
Thursday, 25 January 2007 11:19 am

Thanks you saved my day! :)


20. Bethany left...
Thursday, 1 February 2007 10:47 am

Thank you so much for this code. Very helpful and definitely saved me a lot of work creating a form when I don't really need it.


21. JB left...
Friday, 2 February 2007 8:20 am

when i use mailto: the cursor in the email remains at the top of the body no matter what text or codes I use. How do you force the cursor to go to the end of the line.

EG in <a href="mailto:joeblow@foo.com?subject=survey answer body=Please enter your information below%0D%0AName:%0D%0AAddress%0D%0ACity,State,Zip%0D %0APhone%0D%0Aemail%0D%0AYour comments">Answer our survey</a> the cursor appears way at the top BEFORE "Please enter your information below"


22. Charlie Arehart left...
Friday, 2 February 2007 11:16 am

JB, you're simply expecting too much from a little ol' hyperlink. This isn't a desktop application you're building, with the power of precise cursor control. Indeed, even in a web page, using tools like javascript, you couldn't put the cursor at the end of a line, but only at the beginning of an input field (by setting its focus, as far as I know).

Anyway, it sounds like you need to move to creating a form, where your prompt would be text and the place to enter data would be a form field. This isn't the place to explain how to do that. I send you to any basic book, article, or class on creating HTML forms. But I will say that you can use the same mailto:address that we've used above to be the form's action, causing it to send you the form's content without any need for a server-side program like ASP, ColdFusion, JSP, or PHP. The following link shows you how:

http://www.tizag.com/htmlT/forms.php

But this has one drawback. Just as with a hyperlink (as discussed in my blog entry here), this relies on the client's mail program to generate the email. Unlike the hyperlink, in my testing just now, it simply created the email and put it my Outlook outbox to be sent. Nothing wrong with that. But there are two gotchas.

First, I noticed that when I received the email, the form data was not shown to me in the body of the email but was instead in a file called postdata.att. I had to open that myself to see the form field=value pairs. All that may confuse some users attempting to use this approach.

Second, and more important, with a hyperlink, if there was a problem (no email program on the client's computer, for instance), they'd know immediately on clicking the link. If instead you create this form with the mailto in the action as discussed above, they won't know until they fill out the form and click submit. This is explained here:

http://www.netmechanic.com/news/vol3/form_no4.htm

So you don't want to use this to prompt for a lot of info. If you need to prompt for more, and need a form with more control, but don't want to setup a server program like ASP, ColdFusion, JSP, or PHP, note that that article does point out a free service that will mail you the form results:

http://www.response-o-matic.com/

That service will let you point to a program on their service (as the form ACTION) and forward you the email for free, for just one form that is, though they'll process as many submissions by your users as you'd like. Check it out.

That may be a little more than you were looking for JB, but I hope it helps you or anyone else.


23. Tom Reihnold left...
Wednesday, 28 February 2007 1:34 pm :: http://plavix.phpbbx.de/

Interesting idea, interesting post. A bit of adverticement: <a href= " http://plavix.phpbbx.de "> plavix </a>, <a href= " http://cephalexin.phpbbx.de "> cephalexin </a>


24. Richard S. left...
Thursday, 1 March 2007 9:18 pm

Your CfLf tip is great. I tried using it in a body statement that also includes variables concatenated with an ampersand, as in ASP 3.0? The final statement threw an error suggesting that the ampersand modifies the resulting string and causes errors. Is there a reliable method to handle this?


25. Charlie Arehart left...
Thursday, 1 March 2007 11:09 pm

Thanks, Richard, but as for your question, I'm not sure I understand. You want to append vars to the URL in the mailto? For what purpose? To fill in the body with ASP variables? In that case, you're confusing things I think. You could put variable data on your server-side page in what gets placed in that BODY attribute of the mailto URL. But if you add any more ampersands on the mailto URL, the browser's going to think it's part of the mailto, and the only things the browsers seem to recognize is subject, body, and cc.


26. Duncan left...
Sunday, 16 September 2007 1:46 am :: http://ausalba.blogspot.com

Thanks Charlie. I was floundering around with this, using 'content', and various tricks to try to get the link to work properly. All good now :). One thing though: I have to insert %20 wherever there should be a word space, or MSOE cuts the string there.


27. alib left...
Thursday, 22 November 2007 8:16 am

This is really helpful. I am trying to go one stage further by adding a link within the body portion - i.e. <a href='http://www.wyehead.co.uk>CLICK HERE</a>. I have tried converting angle brackets and = into hex but to no avail - i.e. %3Ca href%3D'http://www.wyehead.co.uk'%3ECLICK HERE%3C%5Ca%3E" - any ideas would be gratefully received as am really scratching my head here


28. Mike left...
Friday, 23 November 2007 6:54 pm

Thanks for this posting Charlie. I have a similar problem and I tried using your suggestion, but it did not work. I'm hoping you can help. :)

So I have a contact form that is using a PHP script to process the submission in to an email delivered to my client. The form works great, except for one field that includes multiple checkbox variables.

The code wants to place commas between each of the variables, but I would like to replace the comma with a line break. I tried inserting your charachter, but alas it did not work.

Any suggestions would be appreciated. Here's the code:

<?php

$where_form_is="http://".$_SERVER.strrev(strstr(strrev($_SERVER),"/"));

session_start(); if( ($_SESSION==$_POST['security_code']) && (!empty($_POST)) ) { // Checkbox handling $field_11_opts = $_POST[0]."%0D%0A". $_POST[1]."%0D%0A". $_POST[2]."%0D%0A". $_POST[3]."%0D%0A". $_POST[4]."%0D%0A". $_POST[5]."%0D%0A". $_POST[6]."%0D%0A". $_POST[7]."%0D%0A". $_POST[8]."%0D%0A". $_POST[9]."%0D%0A". $_POST[10]."%0D%0A". $_POST[11]."%0D%0A". $_POST[12];

mail("xxxx@xxxxx.com","Website - Form submission","The following form data was submitted from your website:

First Name: " . $_POST . " Last Name: " . $_POST . " Title: " . $_POST . " Organization: " . $_POST . " Phone: " . $_POST . " Email: " . $_POST . " Address: " . $_POST . " City: " . $_POST . " State: " . $_POST . " Zip: " . $_POST . " Referral: " . $_POST . " Comments/Feedback: " . $_POST . " Interested in: %0D%0A $field_11_opts ");

include("confirm.html"); } else { echo "Invalid Captcha String."; }

?>


29. Charlie Arehart left...
Tuesday, 27 November 2007 5:07 pm

Alib, about your challenge of the ascii chars for < and > not working, what are you really saying? Because they do work for me. The hyperlink code you offer shows up in the body of my email if I place it in the BODY attribute on the mailto line.

Now, if you're saying that that DOES appear in the email, but you mean for it to show up as a hyperlink, not HTML code, well that's an entirely different problem. You need for the email to be opened in an HTML format, and that's not at all something you can control this way. You can't even know if the user clicking your link has an email client that supports HTML formatted email.

Instead, just put the URL out there. Most email clients will in fact render that as a clickable link when the mail is delivered. Sorry I can't offer more than that.


30. Charlie Arehart left...
Tuesday, 27 November 2007 5:15 pm

Mike, you're barking up the entirely wrong tree on this one. My entry is about the HTML A HREF tag and using it to cause a link to open an email. You, instead, are talking about creating an email from within a PHP program. Entirely unrelated.

I'm afraid I don't use PHP, but I can say that the fact that the values from the form checkbox come in as comma-separated is expected behavior for browsers/HTTP. Most server-side tools offer some mechanism to either let you break those into separate values (which you could then loop over individually and add a line break) or just convert the comma to a <br> tag. You may be over-thinking this one. Should be pretty easily solved--unless you've just cobbled the PHP code from others. In that case, I have to point you to other resources to learn more about the fundamentals of the language. Sorry I can't help more.


31. alib left...
Wednesday, 28 November 2007 8:49 am

Hi Charlie - thank you so much for your help. You were right - I wanted it to show up as a link in the mail rather than the html for the link being visible. I did suspect I was flogging a dead horse on this one it was just that my client particularly to do it this way. I will come up with another plan.


32. vin99 left...
Friday, 25 April 2008 7:30 am

Thanks for this post :)


33. Jerry left...
Thursday, 22 May 2008 4:56 pm

Excellent article. thanks.


34. Bhavtosh left...
Friday, 13 June 2008 11:48 am

Thanks you very much for your post. Its really helped and its a unique post.


35. Anthony left...
Wednesday, 25 June 2008 11:42 am

I am looking to do the same pre population of the body in an email but in a hyperlink on a word document as a way to generate a reponse form as a mode of communication between my end users and my company. I am able to pre-populate the body but I am limited in the number of characters I can have in the body.

Any advice is greatly appreciated.

Thank you

Anthony


36. Debbie left...
Tuesday, 8 July 2008 11:55 am

This is exactly what I was looking for. I searched many places looking for this information and was unable find it anywhere else but here. Thank you so much!