Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use <div> as a button and trigger a mailto when it is clicked

I'm creating a custom button on my webpage which actually is a <div>, I want to trigger a mailto when the button is clicked. What is the best way out?

I've tried calling a javascript function using-onClick that looks like this -

function foo(){     window.open("mailto:[email protected]"); } 

But that opens a new tab in Chrome first, and then asks for the relevant app to send out the email. This experience is different from what we generally get when we simply do a <a href=mailto:.....> in HTML.

I can also create a new document element in the JS function, and simulate a click like this -

function sendEmail() {     var mail = 'mailto:[email protected]';     var a = document.createElement('a');     a.href = mail;     a.click(); }; 

But i'm not too sure if that's the right way! Anyone has a better solution?

like image 510
divyanshm Avatar asked Oct 28 '13 16:10

divyanshm


People also ask

How do I add a mailto to a button?

If you're not using a rich text module or a CTA, you can create a mailto link manually instead. Highlight the text you want to link, then click the Link to dropdown menu and select URL. In the Link URL field, enter mailto:[email protected], then replace [email protected] with the recipient's email address.

How do I create a mailto Button in HTML?

In HTML you can specify a mailto: address in the <form> element's [action] attribute. What this will do is allow the user's email client to create an email prepopulated with the fields in the <form> .

How do I pass HTML formatted body in mailto?

The Mailto format does not support HTML code emails. Outlook was used at 2003, but to become compliant with the mailto: standard they removed that functionality. But you can Use %0D%0A for a line break in HTML body.

How do you use mailto in a tag?

HTML <a> tag provides you option to specify an email address to send an email. While using <a> tag as an email tag, you will use mailto: email address along with href attribute. Following is the syntax of using mailto instead of using http. This code will generate the following link which you can use to send email.


2 Answers

Try this, and tell me if works. (If not, I will delete answer.)

<script> function sendEmail()  {     window.location = "mailto:[email protected]"; } </script> <div onclick="sendEmail();">Send e-mail</div> 

It is possible to pass the parameters subject and body, but I think that it is not possible to format the text:

<a href='mailto:[email protected]?subject=Me&body=Hello!'>EMAIL</a> 
like image 83
Tony Avatar answered Sep 19 '22 21:09

Tony


Extremely late to the party I know, but what about combining these answers into something simpler and more practical:

<div class="button" onclick="location.href='mailto:[email protected]';">Send E-Mail</div> 
like image 25
Chris Kempen Avatar answered Sep 20 '22 21:09

Chris Kempen