I'm trying to send an Email within my JavaScript automatically if an if statement turns into an else. I have an SMTP-Server, but I really dont know how to implement that. Already tried everything I found. I dont want to use node.js, ajax or something else.
Asked
Active
Viewed 9,095 times
-5
-
"I dont want to use node.js" — If you aren't using Node.js to run your JS what **are** you using? Ruling things out without explanation doesn't help us answer you. Tell us what you can use! – Quentin Jan 16 '18 at 14:23
-
Possible duplicate of https://stackoverflow.com/questions/7381150/how-to-send-an-email-from-javascript – a-- Jan 16 '18 at 14:23
-
"I dont want to use ajax" — Ajax is for making HTTP requests. You can't contact an SMTP server with Ajax. – Quentin Jan 16 '18 at 14:23
-
"I dont want to use something else" — Well, that rules out absolutely every other option! – Quentin Jan 16 '18 at 14:24
-
Well, I should ask my question in a different way. Is it possible to send Emails from a JavaScript automatically without to open the users Email-Client, through an SMTP-Server? – Flacko Jan 16 '18 at 14:27
-
@Flacko — Maybe answer the specific questions that people are seeking clarity on too. – Quentin Jan 16 '18 at 14:29
-
(It sounds like the answer to "what are you using?" is "Browser-side JS", in which case see the question Morgan flagged as a duplicate. – Quentin Jan 16 '18 at 14:31
-
As you can see, I just can use a SMTP-Server. Dont know why you are that mean. Its my first post in here so I'm not that experienced and I dont have that much experience with JavaScript either. – Flacko Jan 16 '18 at 14:32
-
I've updated my answer - you don't need anything else except that library, you simply have to include the script in your code and then you can call it. No additional servers need to be set up, you don't need to use node.js or write any ajax calls. – Michael Curry Jan 16 '18 at 14:35
1 Answers
1
You can use this tool:
It allows you to encrypt your SMTP credentials when you call it so you don't expose them to the client side.
Include this script:
<script src="https://smtpjs.com/v2/smtp.js"></script>
And then you can call the service like this:
Email.send("from@you.com",
"to@them.com",
"This is a subject",
"this is the body",
"smtp.yourisp.com",
"username",
"password"
);
If you don't want to expose your credentials you can generate a secure token using the website I linked above.
Email.send("from@you.com",
"to@them.com",
"This is a subject",
"this is the body",
{token: "63cb3a19-2684-44fa-b76f-debf422d8b00"}
);
And call it like this.

Michael Curry
- 991
- 8
- 20
-
1If you are just providing a link this should be a comment instead of an answer. – takendarkk Jan 16 '18 at 14:28
-
1
-
2Please [avoid link only answers](http://meta.stackoverflow.com/tags/link-only-answers/info). Answers that are "barely more than a link to an external site” [may be deleted](http://stackoverflow.com/help/deleted-answers). – Quentin Jan 16 '18 at 14:28
-
-
Doesnt it pressupose to be a form? Because I want the script to call it automatically in an If-Else-Statement. – Flacko Jan 16 '18 at 14:39
-
No, it doesn't need to be a form. You can simply call `Email.send(...)` from your code wherever you please as long as you include the `` in your HTML. – Michael Curry Jan 16 '18 at 14:39
-
@Quentin OP was asking for a way to do it without him having to write any Ajax, not without using Ajax at all. Badly written question. – Michael Curry Jan 16 '18 at 14:42
-
-
@Flacko — If you're running a public facing SMTP server with no authentication then you are running a spam relay and almost nobody will be accepting email from your SMTP server anyway. – Quentin Jan 16 '18 at 14:48
-
"Whoa!, wait a minute. I don't want my SMTP credentials visible to the world!" — Yeah, I don't want my SMTP credentials visible to some company I've never heard of with whom I have no real business relationship. What's in it for them? Other than my password. – Quentin Jan 16 '18 at 14:50
-
@Quentin its within a company, thats why you dont have an authentication – Flacko Jan 16 '18 at 14:52
-
@Flacko — Well, if its an internal server then the tiny Irish company who wants your SMTP password won't be able to send requests to it from their web server anyway, so the service won't work. – Quentin Jan 16 '18 at 14:53
-
-
I should have never answered this question... OP, ideally you need your own server so that you can send an Ajax request to it when you need to send your email, your own server would handle things more safely and you wouldn't be exposing anything to anyone - there are a ton of threads out there about how to do so in PHP/node.js. You're saying you don't want to use these but I think it's because you are afraid to learn to use these -- you'll save yourself a lot of time and effort by just doing it the good old fashioned way, and as pointed out by @Quentin, probably your credentials as well. – Michael Curry Jan 16 '18 at 14:59
-
@Flacko — https://stackoverflow.com/questions/7381150/how-to-send-an-email-from-javascript – Quentin Jan 16 '18 at 14:59
-
@MichaelCurry I dont want to use them because of user limitations, but still ty for your help – Flacko Jan 16 '18 at 15:01
-
1@Flacko what user limitations could possibly prevent you from using server-side code to send emails? Without credentials to your SMTP server I don't see how you're going to send any emails anyway. – Michael Curry Jan 16 '18 at 15:02
-
-
@MichaelCurry — The SMTP server is an open relay, it is protected by not having an Internet facing IP address instead of with credentials. You're right about user limitations not preventing the user of server side code though. – Quentin Jan 16 '18 at 15:05
-
The only other way I could see to do this would be to use a service like https://www.mailgun.com/ and call it with Ajax. You'll struggle to hide your API key from users though, so I don't know how you'd do that effectively, and you'd be using Ajax - which you don't want to do. @Quentin that makes more sense to me now – Michael Curry Jan 16 '18 at 15:07
-
1@Flacko — Either (1) configure npm to use the proxy (2) download the packages manually instead of using npm (3) download the packages onto a vm outside your network (4) use a different server side environment that doesn't expect you to be able to use common package management tools. – Quentin Jan 16 '18 at 15:10
-
Ty guys, I will be using php cause as you told me it wont work with JS only – Flacko Jan 16 '18 at 15:11
-
You can use node.js - node.js is different to client side JS and works like PHP. Use whatever you're more comfortable with though, you could even use Python if you wanted. – Michael Curry Jan 16 '18 at 15:16