如何使用sendgrid在reactjs应用程序中发送电子邮件

时间:2019-05-07 04:58:58

标签: node.js reactjs sendgrid

我正在使用sendgrid通过联系表单发送电子邮件。它可以在具有端口3001的localhost中正常运行。如何在具有端口值的访存中使用我的域。有人可以帮忙吗?

这是单击按钮的代码

        const { email } = this.state
        fetch(`http://domain.in:3001/contactus/send-email?recipient=${email.recipient}&sender=${email.sender}&topic=${email.subject}&text=${email.text}`)
            .catch(err => {
                console.log("error ", err);
            })
        this.setState({ email: { ...email, subject: '', sender: '', text: '' } })

    }````

--------> this is on server.js <-----------

const express = require('express');
const cors = require('cors');
const sgMail = require('@sendgrid/mail');

const app = express()
const port = process.env.PORT || 3001;

sgMail.setApiKey('*******************')
app.use(cors());

app.get('/', (req, res) => {
    res.send('Welcome to the Sendgrid Email server');
})
app.get('/contactus/send-email', (req, res) => {
    const { recipient, sender, topic, text } = req.query
    console.log("came ");

    const msg = {
        to: recipient,
        from: sender,
        subject: topic,
        text: text
    }
    sgMail.send(msg).then((msg) => {
        console.log(text);
    })
})
app.listen(port, () => console.log(`Listening on port ${port}`));

0 个答案:

没有答案