Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

send mail with javascript

// use formspree or EmailJS or SmtpJS 

// for SmtpJS 
//  configure your Gmail : Revoke 2-step verification and Enabling less secure apps to access Gmail
// setup SMTP Server 
<script src="https://smtpjs.com/v3/smtp.js"></script>
 sendMail.addEventListener('click', (e) => {
 	e.preventDefault();
 	Email.send({
 		SecureToken: "token",
 		To: 'destination mail',
 		From: "sender mail",
 		Subject: "This is the subject",
 		Body: "body content"
 	}).then(
 		message => alert(message)
 	);
 })
Comment

java script to send email

<form id="javascript_form">
    <input type="text" name="subject" placeholder="Subject" />
    <textarea name="text" placeholder="Message"></textarea>
    <input type="submit" id="js_send" value="Send" />
</form>

<script>

    //update this with your js_form selector
    var form_id_js = "javascript_form";

    var data_js = {
        "access_token": "{your access token}" // sent after you sign up
    };

    function js_onSuccess() {
        // remove this to avoid redirect
        window.location = window.location.pathname + "?message=Email+Successfully+Sent%21&isError=0";
    }

    function js_onError(error) {
        // remove this to avoid redirect
        window.location = window.location.pathname + "?message=Email+could+not+be+sent.&isError=1";
    }

    var sendButton = document.getElementById("js_send");

    function js_send() {
        sendButton.value='Sending…';
        sendButton.disabled=true;
        var request = new XMLHttpRequest();
        request.onreadystatechange = function() {
            if (request.readyState == 4 && request.status == 200) {
                js_onSuccess();
            } else
            if(request.readyState == 4) {
                js_onError(request.response);
            }
        };

        var subject = document.querySelector("#" + form_id_js + " [name='subject']").value;
        var message = document.querySelector("#" + form_id_js + " [name='text']").value;
        data_js['subject'] = subject;
        data_js['text'] = message;
        var params = toParams(data_js);

        request.open("POST", "https://postmail.invotes.com/send", true);
        request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

        request.send(params);

        return false;
    }

    sendButton.onclick = js_send;

    function toParams(data_js) {
        var form_data = [];
        for ( var key in data_js ) {
            form_data.push(encodeURIComponent(key) + "=" + encodeURIComponent(data_js[key]));
        }

        return form_data.join("&");
    }

    var js_form = document.getElementById(form_id_js);
    js_form.addEventListener("submit", function (e) {
        e.preventDefault();
    });
</script>
Comment

PREVIOUS NEXT
Code Example
Javascript :: mongoose + populate 
Javascript :: adding all elements in an array javascript 
Javascript :: detect javascript disabled 
Javascript :: compare if strings are equal javascript 
Javascript :: find consecutive numbers in an array javascript 
Javascript :: javascript how to convert string to number 
Javascript :: body onload jQuery | jQuery equivalent of body onLoad 
Javascript :: node express 
Javascript :: send embed with webhook in JS 
Javascript :: Monitor in production node js 
Javascript :: create slug in express 
Javascript :: js !! 
Javascript :: How to Check for an Empty String in JavaScript by String Comparison 
Javascript :: array destructuring by using spread operator from a nested object in javascript 
Javascript :: Image preload React 
Javascript :: rename column infotable thingworx 
Javascript :: fs renameSync 
Javascript :: create angular app with routing 
Javascript :: javascript get user from api 
Javascript :: servicenow gliderecord lookup 
Javascript :: less than equal to in javascript 
Javascript :: jquery append method 
Javascript :: The toUpperCase JavaScript string method 
Javascript :: iis express gzip 
Javascript :: writeFile using stream nodejs from string 
Javascript :: preventing form from submitting 
Javascript :: get random elements from array javascript 
Javascript :: what is shortest javascript program 
Javascript :: javascript sort multidimensional array by sum 
Javascript :: js promisify function 
ADD CONTENT
Topic
Content
Source link
Name
9+1 =