How to use JavaScript to create a client side email?

Sometimes, we want to use JavaScript to create a client side email.

In this article, we’ll look at how to use JavaScript to create a client side email.

How to use JavaScript to create a client side email?

To use JavaScript to create a client side email, we can create a mailto URL.

For instance, we write:

const addresses = "[email protected]";
const body = "hello"
const subject = "subject"
const href = `mailto:${addresses}?subject=${subject}&body=${body}`
const wndMail = window.open(href, "_blank", "scrollbars=yes,resizable=yes,width=10,height=10");

to create the href mailto URL.

We combine the addresses to send to, email subject and email body into one string.

And then we call window.open with href to open the URL, which opens the default mail client with the compose email window.

Conclusion

To use JavaScript to create a client side email, we can create a mailto URL.