JavaGeek
JavaGeek

Reputation: 1529

Unable to close new window after using mailto in javascript

I'm trying to send invoke email when button is clicked and its working fine but extra window is getting opened during the process and i'm unable to close it even though I have tried close method. I have tried in IE 10 and IE 11 and its not working.

Below is the code

    <html>
<head>
<script type='text/javascript'>
    function sendMail(){

        var wi = window.open('mailto:[email protected]?subject=' + encodeURIComponent(document.getElementById("metric").value));
        wi.close();     
   }

</script>
</head>
<body>

<h4>Send e-mail to committee with below subject:</h4>

<form action="javascript:sendMail()" method="post" enctype="text/plain">

<select id="metric">

  <option value="test1">test1</option>
  <option value="test2">test2</option>
</select>
<br><br>

<input type="submit" value="Send">

<p></p>
</form>
</body>
</html>

Upvotes: 0

Views: 105

Answers (1)

Tim Consolazio
Tim Consolazio

Reputation: 4888

I'm going to go ahead and say this isn't how you'd go about it.

A mailto: link opens the user's default email client. If you remove the attempt to close the window in your script, you'll see this happen. But to do anything useful, you'd have to fill out the mail along with sender etc.

So the user will need to trigger the actual send in their email client.

What you would typically do is send the request to the server, and have the server use the SMTP service (pretty much all of them have it now) to put together and send the email out. It's simple to do, how exactly it works depends on what server you are going to be hosting your solution on.

To my knowledge there is no pure javascript/server-less way to do this. If you think about it, this makes sense; what would prevent somebody from putting a script in a webpage that sends out a thousand emails based on something you collected from the webpage?

Upvotes: 2

Related Questions