📌  相关文章
📜  使用 node.js 响应通过 Nodemailer 发送电子邮件 - 任何代码示例

📅  最后修改于: 2022-03-11 14:54:57.106000             🧑  作者: Mango

代码示例1
const express = require('express');
const mailerRouter = express.Router();
 const Nodemailer = require('nodemailer')



mailerRouter.post('/email', (req, res) => {

const data = req.body
console.log(`${data.name}, ${data.email}, ${data.subject}, ${data.messages}`)
const output = `Name: ${data.name} 
Email: ${data.email}

${data.messages}` // file attachment if (req.files === null) { return res.status(400).json({ msg: 'No file uploaded' }); } const file = req.files.file console.log(req.files) console.log(file) file.mv(`${__dirname}/../clients/public/images/${file.name}`, err => { if (err) { console.error(err); return res.status(500).send(err); } res.json({ fileName: file.name, filePath: `/images/${file.name}` }); }); // create reusable transporter object using the default SMTP transport let transporter = Nodemailer.createTransport({ service: 'Gmail', post: 587, secure: false, auth:{ user: 'your email', pass: "password" }, tls:{ rejectUnauthorized:false } }); // setup email data with unicode symbols let mailOptions ={ from: `Your name`, to: "list of recivers", name: `${data.name}`, html: output, attachments:[{ path: `${__dirname}/../clients/public/images/${file.name}`, }] }; // send mail with defined transport object transporter.sendMail(mailOptions, (err)=>{ if(err){ return console.log(err) }else{ console.log(`Success`) } }) transporter.close() }) module.exports = mailerRouter;