Wednesday, December 28, 2011

Send well formatted email using mailto

It's been more than a year i posted in my blog. I've done lots of work with learning and experience in this year.Sure i can share my knowledge in this blog so, please keep engaged and don't forget to suggest or criticize.


This time i am going to start with small solution most of you know about it. If you know the better just reply to this post.



How to send well formatted email with subject in HTML?



For your information - I used jQuery javascript library to achieve this.

HTML part:


I put email address in "title" tag to avoid incorrect HTML. And class name "attachMail" is used for reference in jQuery.


<a title="someone@xyz.abc" href="#" class="attachMail">'Send this info in Email</a>


jQuery part:



  1. Use the 'click' event with 'attachMail selector'.

  2. Get the email ID's from the title tag by using 'mailids' variable as shown in below

  3. Copy the page URL using window.location.href

  4. Create subject and body message as shown in below.

  5. Assign the mail content to window.location

  6. Now if you click on the hyper-linked text, that will open the new mail box with default mail address, text and body message



$('a.attachMail').click(function(){
var mailids = $(this).attr('title');
var pageurl = window.location.href;
window.location = 'mailto:'+mailids+'?subject=Test mail subject&body=Below is the details of the page:%0A%0ALine number one%0A%0A2. Line number two%0A%0A3. Line number three.%0A%0APlease go through below page for reference.%0A%0A '+pageurl+'';
});