Send an Email Using React.js + Express.js

前端 未结 2 853
谎友^
谎友^ 2020-12-23 23:33

I have built a web app using React.js in ES6. I currently want to create a basic \"Contact Us\" page and want to send an email. I am new to React and just discovered that I

2条回答
  •  醉酒成梦
    2020-12-24 00:25

    When the button is clicked, execute an ajax POST request to your express server, i.e "/contactus". /contactus can fetch the email, subject, and content out of the post data and send to the mail function.

    In React:

    $.ajax({
        url: '/contactus',
        dataType: 'json',
        cache: false,
        success: function(data) {
            // Success..
        }.bind(this),
        error: function(xhr, status, err) {
            console.error(status, err.toString());
        }.bind(this)
    });
    

    In express add the nodemailer code within an express post handler:

    app.post('/contactus', function (req, res) {
        // node mailer code
    });
    

提交回复
热议问题