0

I'm doing the final project in my XML & JavaScript course and I'm having trouble with sending email via JavaScript. I found this page: http://smtpjs.com/. But it requires SMTP credentials, I don't know what SMTP credentials is. Who has used smtpjs.com or who has the solution please help me. P/s: Do I need to upload my code to a host to send email. I always open html file from my computer, I wonder whether it will be able to send email or not.

Sayo Babalola
  • 990
  • 14
  • 25
Nguyen Hoa
  • 55
  • 1
  • 3
  • 7
  • 1
    JavaScript alone can't send an email, it needs an email SMTP server which send the request to. – yuriy636 May 01 '17 at 10:43
  • SMTP is, put simply, "what you use to log in to your email account". – Niet the Dark Absol May 01 '17 at 10:45
  • 1
    Just as have been said above, you will need to connect to an SMTP server somehow to send emails from javascript. There's a problem however if you are going to use this script from a web browser. Your login credentials to that SMTP server will be visible to the user of the page if they know what they are doing. So its not secure. A more secure way is to set up the script to send the email using php/python etc on a server and trigger that function with an asynchronous call. Cheers. – Ayo Makanjuola May 01 '17 at 11:00
  • Email doesn't just "magicly" go from point A to point B. There's a server or "program" involved that does this. Any decent mail server requires you to login. Think about Gmail, Hotmail etc. That's essentially what SMTP is. You provide your credentials (login details) to the mail server so it know who's sending the email. – icecub May 01 '17 at 11:00
  • Maybe I didn't understood your question right but I think it should solve your problem https://stackoverflow.com/questions/13231125/automatically-open-default-email-client-and-pre-populate-content – Jerome May 01 '17 at 10:54

3 Answers3

0

First of all, I don't think you should run your code from the html file. You run it from a server to properly communicate with smtp.js

You can try this:

$('#form-submit').click(function () {
  submitForm();
 });

function submitForm() {
// Initiate Variables With Form Content
var name = $("#name").val();
var email = $("#email").val();
var message = $("#message").val();
if (name == "") {
    alert('Invalid Name');
    return;
}
if (message == "") {
    alert('Message cannot be empty');
    return;
}
var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
if (!emailReg.test(email)) {
    alert('Invalid Email');
    return;
}

Email.send("from@email.com",
    "to@email.com",
    "Message from " + name + " " + email,
    message,

    {
        token: "63cb3a19-2684-44fa-b76f-debf422d8b00",
        callback: function done(message) { alert("sent") }

    });

   }

I also used smtpjs and sendgrid. I hope this helps.

Sayo Babalola
  • 990
  • 14
  • 25
0

If you want to send an email in javascript, you should turn on allow secure app access in Gmail.

Google will not let you send emails if you do not turn on allow the less secure app in Gmail.

Here is the link to: Turn On Allow Secure App Access

Please note if you turn on the function in google, it is easy for attackers to find information about your Gmail account.

It is recommended to create an account just for development.

Not A Bot
  • 2,474
  • 2
  • 16
  • 33
Toby
  • 1
  • 1
-2

SMTP is the server that handles mails. Here is google's: 'smtp.gmail.com'. You can directly send an e-mail from javascript. I suggest you go for JSON.

Abir Imtiaz
  • 33
  • 1
  • 10
  • If I use smtp.gmail.com, the user name is my google's email, and the password is my gmail password, right? – Nguyen Hoa May 01 '17 at 11:00
  • @NguyenHoa Yes it is. But do **NOT**.. I repeat: **DO NOT!!!!** put your Gmail login credentials in Javascript. Anyone can read it! Anyone will be able to see your password! – icecub May 01 '17 at 11:02
  • just create an account for this, unless you project is meant for production. – Abir Imtiaz May 01 '17 at 11:27