Я использую рассылочную электронную почту, чтобы отправлять веб-сообщения API с помощью nodeJS. Хотя я могу успешно отправлять письма, но они попадают в папку со спамом.
Ранее я пытался отправлять электронные письма с php с помощью функции phpmailer, и эти письма успешно доставляются в почтовый ящик пользователя.
Ниже мой код:
const express = require('express');
const router = express.Router();
const bodyParser = require('body-parser');
const MongoClient = require('mongodb').MongoClient;
const env = require('dotenv').config();
const sgMail = require('@sendgrid/mail');
var dburl = process.env.URL;
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
router.use(bodyParser.json());
router.use(bodyParser.urlencoded({extended:true}));
router.post('/users', (req,res) => {
var data = {
_id:req.body.id,
Name:req.body.username,
Email:req.body.email
};
const msg = {
to: 'example@gmail.com',
from: 'test@gmail.com',
subject: 'Sending with Twilio SendGrid is Fun',
text: 'and easy to do anywhere, even with Node.js',
html: '<strong>and easy to do anywhere, even with Node.js</strong>'
};
MongoClient.connect(dburl,{ useNewUrlParser: true }, (err,client) => {
if(err){
console.log('Unable to connect with database'.red +err);
}
else{
client.db("Example").collection("Users").findOne({_id:req.body.id},function(err,user) {
if(err){
console.log("Error:".red +err);
}
if(user){
res.send("User exists");
}
else{
var collection = client.db("Example").collection("Users");
collection.insertOne(data,(err,resp) => {
if(err){
console.log("Insertion error".red +err);
}
else{
res.send("User created");
sgMail.send(msg,(err) => {
if(err){
console.log("Error",err);
}else{
console.log("Mail sent");
}
});
}
});
}
client.close();
});
}
});
});
module.exports = router;
Кто-то дал мне знать, как я могу доставлять электронные письма на входящие сообщения пользователя. Любая помощь будет признательна.
СПАСИБО