Wednesday, June 30, 2010

How to prepopulate an email message with subject and body

Today I learned about how to populate an email message on client-side.

I think most people know about how to create an email link in html. It can be easily done by using
<a href="somebody@mail.com>Mail Somebody</a>
But what if you want to partially pre-populate the subject title and message body?

After googling for the answer, I found out that you can easily get the job done by using
<a href="mailto:somebody@mail.com?subject=Hello There&body=Line 1%0ALine 2%0ALine 3>Mail Somebody</a>

Since I'm calling the method from jsf tag, instead of using <a href>, I can use h:outputLink since h:outputLink produces html links. Also I've read people complaining about the html link not working if you put space in the subject or body. You can simply solve the issue by putting '%20' instead of space.

Click Mail to try out.

If you also want to know how to include cc and bcc recipients, here's a great page:
http://www.pageresource.com/html/mail2.htm

The complaint I have regarding this is that I cannot use other jsf tag to get the same performance. I would like to use a menu item (for consistency) instead of a link. I tried it in many different ways and it is just not feasible. Also, please note that you will need to have an email client(i.e Outlook Express etc. ) set up for this to work.

No comments:

Post a Comment