Is it possible to send emails using just javascript?
-
2You can make an AJAX call to a script on your server which does it for you... but that's not really "just javascript" :-) – Cameron Dec 15 '10 at 20:01
-
1Can marketing companies send spam using Javascript? – Harmen Dec 15 '10 at 20:02
-
Possible duplicate of [Sending emails with Javascript](https://stackoverflow.com/questions/271171/sending-emails-with-javascript) – Vivek Kumar Nov 13 '17 at 06:55
6 Answers
EDIT: [WARNING!] README:
It's a third party library that connects to an external server, take care with the information that you are sending.
Another solution on JS you can use a library named smtpjs
Add following library your html on header:
<script src="https://smtpjs.com/smtp.js"></script>
Use this without security:
Email.send("from@you.com",
"to@them.com",
"This is a subject",
"this is the body",
"smtp.yourisp.com",
"username",
"password");
Use this with security:
Email.send("from@you.com",
"to@them.com",
"This is a subject",
"this is the body",
{token: "63cb3a19-2684-44fa-b76f-debf422d8b00"});

- 6,814
- 1
- 22
- 23

- 770
- 14
- 20
It's actually possible and not all that difficult to build an SMTP client in Javascript.
But that SMTP client will still need to talk to an SMTP server for getting its emails delivered. And SMTP servers open to everyone are very rare nowadays (because they quickly become Spam conduits and then blocked and/or closed).
However, if the person using the client can supply an SMTP server and user credentials for it (just like with any other general purpose email client), then yes, you can send emails using just javascript.

- 6,814
- 1
- 22
- 23

- 342,105
- 78
- 482
- 720
-
1That's not really an SMTP client in JavaScript, it's a Mail User Agent sending mail over a PHP backend. The OP asked for "just javascript". – Axel Beckert Oct 08 '19 at 15:05
-
What @AxelBeckert said. Here's the link to the follow-up article that shows the PHP code calling the PHP `mail()` function to send an email, which is triggered by client-side AJAX: https://web.archive.org/web/20130718210439/http://www.devarticles.com/c/a/JavaScript/Sending-Email-with-an-SMTP-Client-Built-with-Prototype-and-PHP/ – Prid Mar 02 '23 at 00:32
Yes. Using a Webservice. You can make an AJAX call to the service. EmailYak is one such service (It's in a private beta now).
EDIT: This is still a server side solution, as the actual email is sent from the server. You are just communicating with a server via AJAX and telling it to send the email.

- 43,763
- 16
- 104
- 144
Note that smtpjs uses a service located at http://smtpjs. It's not truly a Javascript SMTP client. This "utility" means you are uploading your email credentials to the server smtpjs.com. Use with extreme caution.

- 9,895
- 3
- 50
- 69

- 53
- 7
You can redirect to a mailto:someone@example.com?cc=someone_else@example.com&subject=This%20is%20the%20subject&body=This%20is%20the%20body
address which tells the browser to fire up the mail client which then makes the mail ready to send - the user just has to hit "submit".
Code:
document.location="mailto:someone@example.com?cc=someone_else@example.com&"+
"subject=This%20is%20the%20subject&body=This%20is%20the%20body";

- 44,854
- 16
- 96
- 107
-
Actually, [you can do something like that in javascript](http://bytes.com/topic/javascript/answers/592325-can-i-send-email-javascript) without the user having to hit submit. But it assumes there is an email client such as Outlook present on the local machine. – DOK Dec 15 '10 at 20:07
-
@DOK: Sure? Can't see that anywhere in the page you linked. And it would be a big, fat security hole. – thejh Dec 15 '10 at 20:09
-
@thejh I think that code is pretty easy to find there. It's the first answer to the question. Do a search for "onclick" or "mailto:" – DOK Dec 15 '10 at 20:29
-
-
@thejh So basically you are raising arguments against the answer that you are proposing. – DOK Dec 15 '10 at 21:00
-
@DOK: Why not? It's not making it completely unusable, but I wanted to point that out. – thejh Dec 15 '10 at 21:16
If you want to send the message "silently" from an SMTP process, then this needs to be done on the server or by using a hosted service.
If you are happy to use the user's native email program, you could use an approach such as that described in this question.

- 1
- 1

- 16,748
- 10
- 99
- 155