'How to send email using Javascript in a simplest way?

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.



Solution 1:[1]

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("[email protected]",
    "[email protected]",
    "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.

Solution 2:[2]

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.

Solution 3:[3]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Oluwasayo Babalola
Solution 2 Not A Bot
Solution 3 Abir Imtiaz