pre-filling an email in the email client using javascript

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.

$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";
$('#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.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s