Feb20

pre-filling an email in the email client using javascript

Posted by: Brian Chan | Filed in: technology | Tags: | 05:44 pm, February 20th, 2012 Add comments

Sometimes people prefer to use their own email client application to add their own styling. That’s when we will need to pass the relevant email content to the email client. It’s simple enough so I won’t explain much. Here I am passing the email info from php to javascript.

1
2
3
4
5
6
$email_to = "johnsmith@somewhere.com";
$email_subject = "some email subject";
$email_body = "some long text, will need to escape this.";
//$email_body = urlencode($email_body);  // not this one, empty spaces will turn into +'s
$email_body = rawurlencode($email_body);  // this will encode correctly
$email_url = "mailto:$email_to?Subject=$email_subject&Body=$email_body";
?View Code JQUERY
1
2
3
$('#email_btn').click(function(){  // bind to the email btn
	window.location = "$email_url";
});

That’s it! Now upon pressing on the email button, the user’s default email client should pop up a new email window with the fields pre-filled.




Related posts:

  1. using js to open links in current window, new window, or in the background
  2. Stop UTF 8 with javascript
  3. CLLocationManager error
  4. check file size at the client side
  5. The “select all” checkbox to select all checkboxes

Leave a Reply