如何创建使用节点和 mongodb 发送邮件的 api

How to create an api that sends mails using node and mongodb

我正在尝试创建一个发送邮件的应用程序。 它获取用户输入(To,Subject,Message) onClick 表单按钮发送邮件,并将该邮件存储在 mongodb

前端

<form>
<span>
to :<input type='text' >
</span>

cc :<input type='text' >
</span>

bcc :<input type='text' >
</span>

<span>
subject :<input type='text' >
</span>

<span>
message :<input type='text' >
</span>
</form>

后端

to = 'sender@gmail.com',
cc = 'sender@gmail.com',
bcc = 'sender@gmail.com',
subject = 'A project proposal',
message = 'the body of your mail',
etc...
  1. 架构

    常量猫鼬=要求('mongoose');

    const UserSchema = new mongoose.Schema({

    至:{ 类型:字符串, }, 抄送:{ 类型:字符串, }, 密件抄送:{ 类型:字符串, }, 密件抄送:{ 类型:字符串, }, 学科: { 类型:字符串, }, 信息: { 类型:字符串, }, 附件: { 类型:字符串, }, 日期: { 类型:日期, 默认值:Date.now },

    });

    const Mail = mongoose.model('Mail', UserSchema);

    module.exports = 邮件;

A.p.i

const Mail = require('../models/Mail');

// Home Page
router.get('/', forwardAuthenticated, (req, res) => res.render('home'));

// Mail
router.get('/mail', ensureAuthenticated, (req, res) =>
  res.render('mail', {
    user: req.user,
    mail: req.mail

  })
);

router.post('/mail', (req, res) => {
  const { to, cc, bcc, subject, message, attachment, account } = req.body;
  let errors = [];

  if (!name || !subject || !message || !account) {
    errors.push({ msg: 'Please enter all fields' });
  }

  if (errors.length > 0) {
    res.render('register', {
      errors,
      name,
      subject,
      message,
      account
    });
  } else {
    const newMail = new Mail({
      to,
      cc,
      bcc,
      subject,
      message,
      attachment,
      account
    });

    newMail
      .save()
      .then(mail => {
        req.flash(
          'success_msg',
          'mail sent'
        );

      })
      .catch(err => console.log(err));

  }
})
module.exports = router;

我该如何处理?

我使用 'nodemailer' 模块,你可以阅读他们的文档,但我给你一个简单的例子:

var nodemailer = require ('nodemailer');
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
var transporter = nodemailer.createTransport ({ 
    service: 'gmail', 
    auth: { 
            user: 'youre@gmail.com', 
            pass: 'yourePassword' 
        } 
    });

 module.exports={
     sendAUTH:function(link, email){
        const mailOptions = { 
            from: 'youre@gmail.com',  
            to: email,  
            subject: 'Subject of your email', 
            html: `<a href='`+link+`'>link</a>` 
          };
          transporter.sendMail (mailOptions, function (err, info) { 
            if (err) 
              console.log (err) 
            else 
              console.log (info); 
         });
     }
 }

尝试 express-mailer 发送电子邮件。在此,您还可以通过逗号分隔列表或数组以 CC 和 Bcc 方式发送电子邮件。请仔细检查 express-mailer 文档。

var app = require('express')(),
var mailer = require('express-mailer')

mailer.extend(app, {
    emailFrom: "yourEmail@gmail.com", 
    host: 'smtp.gmail.com', // hostname
    secureConnection: true, // use SSL
    port: 465, // port for secure SMTP
    transportMethod: 'SMTP', // default is SMTP.
    auth: {
        user: 'yourEmail@gmail.com', // Your Email 
        pass: '*******' // Your Password
    }
});

app.mailer.send('../views/emailTemplate/emailTemplate.ejs', { to: 'reciverEmail@gmail.com', subject: 'Your Email Subject'}, function (err, message) {
    if (err) throw new Error(err);
    console.log(message);
    return;
});