'How to add OAuth 2.0 authentication with smtp to send email using javascript?

I need to add oauth 2.0 security in my existing email submission form. There are some other methods that I found online but as I am using smtp, I just want to add the necessary code and tokens. But don't know how to code it right.

JAVASCRIPT

        function sendmail()

        {

        var name = $('#name').val();
        var email = $('#email').val();
        var subject = $('#subject').val();
        var message = $('#message').val();

        // var body = $('#body').val();

        var Body='name: '+name+'<br>email: '+email+'<br>subject: '+subject+'<br>message: '+message;
        //console.log(name, phone, email, message);

        Email.send({
            SecureToken : "",
            To : '',
            From : '',
            Subject: "New message on contact from "+name,
            Body: Body
        }).then(
            message =>{
                //console.log (message);
                if(message=='OK'){
                alert('E-mail has been sent successfully. Thank you ...');
                }
                else{
                    console.error (message);
                    alert('Error at sending message ...')
                    
                }

            }
        );

    }

Currently only this much javascript code is sending email using smtp protocol. But gmail is disabling access for less secure app. So what do I need to add to include oauth 2.0?



Sources

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

Source: Stack Overflow

Solution Source