How to send an email from JavaScript
You can't send an email directly with javascript.
You can, however, open the user's mail client:
window.open('mailto:test@example.com');
There are also some parameters to pre-fill the subject and the body:
window.open('mailto:test@example.com?subject=subject&body=body');
Another solution would be to do an ajax call to your server, so that the server sends the email. Be careful not to allow anyone to send any email through your server.
Sending email in Javascript
The query string should be start with ? not &
change &subject to ?subject
it should be //abc@example.com?subject=blahblahblah&body=testtest
Sending emails with Javascript
The way I'm doing it now is basically like this:
The HTML:
<textarea id="myText">
Lorem ipsum...
</textarea>
<button onclick="sendMail(); return false">Send</button>
The Javascript:
function sendMail() {
var link = "mailto:me@example.com"
+ "?cc=myCCaddress@example.com"
+ "&subject=" + encodeURIComponent("This is my subject")
+ "&body=" + encodeURIComponent(document.getElementById('myText').value)
;
window.location.href = link;
}
This, surprisingly, works rather well. The only problem is that if the body is particularly long (somewhere over 2000 characters), then it just opens a new email but there's no information in it. I suspect that it'd be to do with the maximum length of the URL being exceeded.
Related Topics
Addeventlistener Calls the Function Without Me Even Asking It To
Array.Prototype.Fill() With Object Passes Reference and Not New Instance
JavaScript Set Object Key by Variable
How to Check If an Object Is an Array
How to Get Jquery to Perform a Synchronous, Rather Than Asynchronous, Ajax Request
Chrome Extension Message Passing: Response Not Sent
How to Send a Cross-Domain Post Request Via JavaScript
What Is the Temporal Dead Zone
Create and Save a File With JavaScript
How to Find the Sum of an Array of Numbers
Generate Random String/Characters in JavaScript
Resolve Promises One After Another (I.E. in Sequence)
How to Empty an Array in JavaScript
Get JavaScript Object from Array of Objects by Value of Property
How to Access React Instance (This) Inside Event Handler