Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invoke / click a mailto link with JQuery / JavaScript

I'd like to invoke a mailto link from JavaScript - that is I'd like a method that allows me to open the email client on the users PC, exactly as if they had clicked on a normal mailto link.

How can I do this?

like image 656
Justin Avatar asked Oct 05 '10 22:10

Justin


4 Answers

You can use window.location.href here, like this:

window.location.href = "mailto:[email protected]";
like image 51
Nick Craver Avatar answered Sep 23 '22 00:09

Nick Craver


You can avoid the blank page issue discussed above by instead using .click() with a link on the page:

document.getElementById('mymailto').click();
...
<a href="mailto:...." id="mymailto" style="display:none"></a>
like image 24
franzo Avatar answered Sep 25 '22 00:09

franzo


the working answer for me, tested in chrome, IE and firefox together with outlook was this

window.location.href = 'mailto:[email protected]?subject=Hello there&body=This is the body';

%0d%0a is the new line symbol of the email body in a mailto link

%20 is the space symbol that should be used, but it worked for me as well with normal space

like image 44
Toskan Avatar answered Sep 25 '22 00:09

Toskan


Better to use

window.open('mailto:[email protected]?subject=sub&body=this is body')

If we use window.location.href chrome browser is having error in network tab with Provisional headers are shown Upgrade-Insecure-Requests: 1

like image 40
Prabin Tp Avatar answered Sep 25 '22 00:09

Prabin Tp