How to send HTML form data by email to an email ID by using JavaScript and without using server-side language and without using email client securely? I do not want any email client to pop open on clicking submit.
-
what about https://github.com/whiteout-io/smtpclient http://emailjs.org/ ? – Andrey Jul 15 '15 at 15:47
-
2possible duplicate of [How do I send an HTML Form in an Email .. not just MAILTO](http://stackoverflow.com/questions/7449767/how-do-i-send-an-html-form-in-an-email-not-just-mailto) – Ted Jul 15 '15 at 15:47
3 Answers
It's very simple to send email by JavaScript:
<textarea id="Body">
Mail Content
</textarea>
<button onclick="sendMail();">Send</button>
SendMail function:
function sendMail() {
var link = "mailto:me@example.com"
+ "?cc=myCCaddress@example.com"
+ "&subject=" + escape("This is my subject")
+ "&body=" + escape(document.getElementById('Body').value)
;
window.location.href = link;
}
You can call SendMail()
function on submit.
Hope this help you.

- 1,413
- 1
- 18
- 29
-
1hi, thank you but I don't want any client to open up, in this case client opens up. I want mail to be directly sent on clicking the form submit button. – Arijit Patra Jul 15 '15 at 16:04
Your requirement basically says that you want to hit an smtp server directly and the mail should go from there.
This would mean that Javascript should support a way to send data to an smtp server.
The default way to talk to a smtp server is via TCP/IP (you can use telnet on a smtp server, usually on port 25).
Since Javascript does not support TCP/IP directly it is not possible to do it directly from Javascript.
This does not mean that
- You cannot use a email service which provides rest interface
- Write your own backend based on REST to relay it to an SMTP server
- Have a backend which acts as a proxy to you incoming rest and translate it to a tcp/ip request and send it to the corresponding server and port
The above three methods seem to be the only ways I can think of to send a mail from Javascript.
Edit: There seems to be a TCPSocket https://developer.mozilla.org/en-US/docs/Web/API/TCPSocket but it is not widely supported.
You might also want to look into flash, I don't know how long it will be widely supported though.

- 1,152
- 8
- 21
-
Flash will only connect to third-party servers that serve a `crossdomain.xml`, so you won't be able to connect to arbitrary SMTP servers that way. TCPSocket isn't available from web pages, assuming that's what the OP is trying to do. – bobince Jul 15 '15 at 20:10
-
It's very much deliberate that you aren't allowed to do this. If you could, everyone's browsers would be turned into infernal spam-spewing abuse machines.
From a web page, you can only choose between client-side mail, letting the user confirm what they're sending (and losing some proportion of respondants who aren't in a position to send mail), or a web-accessible server-side mail forwarder (either a third-party service or your own server in which case you have to work out the security yourself).

- 528,062
- 107
- 651
- 834