在发送邮件之前等待文件创建
wait file to create before send mail
我在使用 nodemailer 的功能时遇到了一些问题,我需要从 body req 创建一个 pdf,然后将其附加到邮件并发送,这里的问题是我不知道如何等待文件完成在发送邮件之前保存,我有错误,因为邮件在系统可以创建文件甚至附加 pdf 文件之前发送...
想法是:发送 post 请求 -> 获取 req.body -> 创建 pdf -> 创建邮件选项 (nodemailer) -> 发送邮件。
但现在的代码是这样的:发送 post 请求 -> 获取 req.body -> 创建 pdf -> 创建邮件选项 (nodemailer) -> 发送邮件 -> 系统
仍在制作 pdf -> 错误
我当前的代码是:
app.post('/send', function(req, res) {
if (req.body.email == "" || req.body.subject == "") {
res.send("Error: Email & Subject should not blank");
return false;
}
// Sending Emails with SMTP, Configuring SMTP settings
var smtpTransport = nodemailer.createTransport({
host: "mail.vyg.cl", // hostname
secureConnection: true, // use SSL
port: 465, // port for secure SMTP
auth: {
user: 'crm@vyg.cl',
pass: '******'
}
});
const pdfArchive = './server/' + req.body.subject + '.pdf';
if (fs.existsSync(pdfArchive)) {
fs.unlinkSync(pdfArchive);
}
pdf.create(req.body.description).toFile('./server/' + req.body.subject + '.pdf', function(err, res) {
if (err) {
console.log(err);
} else {
console.log(res)
}
});
const mailOptions = {
from: req.body.from, // sender address
to: req.body.to, // list of receivers
cc: req.body.cc,
subject: req.body.subject, // Subject line
//text: "Hello world ✔", // plaintext body
// html: req.body.description, // html body,
attachments: [{ // file on disk as an attachment
filename: req.body.subject + '.pdf',
path: './server/' + req.body.subject + '.pdf' // stream this file
}]
}
smtpTransport.sendMail(mailOptions, function(error, response) {
if (error) {
res.status(400).json({ message: "Email could not sent due to error: " + error });
} else {
res.status(200).json({ message: "Email has been sent successfully" });
}
});
if (fs.existsSync(pdfArchive)) {
fs.unlinkSync(pdfArchive);
}
});
我尝试在创建 pdf 时按条件发送邮件,但我没有收到 smtpTransport 响应....
任何帮助将不胜感激
app.post('/send', async function(req, res) {
if (req.body.email == "" || req.body.subject == "") {
res.send("Error: Email & Subject should not blank");
return false;
}
// Sending Emails with SMTP, Configuring SMTP settings
var smtpTransport = nodemailer.createTransport({
host: "mail.vyg.cl", // hostname
secureConnection: true, // use SSL
port: 465, // port for secure SMTP
auth: {
user: 'crm@vyg.cl',
pass: '*******'
}
});
const mailOptions = {
from: req.body.from, // sender address
to: req.body.to, // list of receivers
cc: req.body.cc,
subject: req.body.subject, // Subject line
//text: "Hello world ✔", // plaintext body
// html: req.body.description, // html body,
attachments: [{ // file on disk as an attachment
filename: req.body.subject + '.pdf',
path: './server/' + req.body.subject + '.pdf' // stream this file
}]
}
const pdfArchive = './server/' + req.body.subject + '.pdf';
if (fs.existsSync(pdfArchive)) {
await fs.unlinkSync(pdfArchive);// Add wait here
}
try{
var result = await pdf.create(req.body.description).toFile('./server/' + req.body.subject + '.pdf') // capture result if you need to
try{
await smtpTransport.sendMail(mailOptions) // wrap this in a try catch if you want to capture email sending specific erorrs else outer try catch will work
res.status(200).json({ message: "Email has been sent successfully" });
}
catch(error)
{
res.status(400).json({ message: "Email could not sent due to error: " + error });
}
}
catch(error)
{
console.log(err);
res.send(error)
}
});
我在使用 nodemailer 的功能时遇到了一些问题,我需要从 body req 创建一个 pdf,然后将其附加到邮件并发送,这里的问题是我不知道如何等待文件完成在发送邮件之前保存,我有错误,因为邮件在系统可以创建文件甚至附加 pdf 文件之前发送...
想法是:发送 post 请求 -> 获取 req.body -> 创建 pdf -> 创建邮件选项 (nodemailer) -> 发送邮件。
但现在的代码是这样的:发送 post 请求 -> 获取 req.body -> 创建 pdf -> 创建邮件选项 (nodemailer) -> 发送邮件 -> 系统 仍在制作 pdf -> 错误
我当前的代码是:
app.post('/send', function(req, res) {
if (req.body.email == "" || req.body.subject == "") {
res.send("Error: Email & Subject should not blank");
return false;
}
// Sending Emails with SMTP, Configuring SMTP settings
var smtpTransport = nodemailer.createTransport({
host: "mail.vyg.cl", // hostname
secureConnection: true, // use SSL
port: 465, // port for secure SMTP
auth: {
user: 'crm@vyg.cl',
pass: '******'
}
});
const pdfArchive = './server/' + req.body.subject + '.pdf';
if (fs.existsSync(pdfArchive)) {
fs.unlinkSync(pdfArchive);
}
pdf.create(req.body.description).toFile('./server/' + req.body.subject + '.pdf', function(err, res) {
if (err) {
console.log(err);
} else {
console.log(res)
}
});
const mailOptions = {
from: req.body.from, // sender address
to: req.body.to, // list of receivers
cc: req.body.cc,
subject: req.body.subject, // Subject line
//text: "Hello world ✔", // plaintext body
// html: req.body.description, // html body,
attachments: [{ // file on disk as an attachment
filename: req.body.subject + '.pdf',
path: './server/' + req.body.subject + '.pdf' // stream this file
}]
}
smtpTransport.sendMail(mailOptions, function(error, response) {
if (error) {
res.status(400).json({ message: "Email could not sent due to error: " + error });
} else {
res.status(200).json({ message: "Email has been sent successfully" });
}
});
if (fs.existsSync(pdfArchive)) {
fs.unlinkSync(pdfArchive);
}
});
我尝试在创建 pdf 时按条件发送邮件,但我没有收到 smtpTransport 响应....
任何帮助将不胜感激
app.post('/send', async function(req, res) {
if (req.body.email == "" || req.body.subject == "") {
res.send("Error: Email & Subject should not blank");
return false;
}
// Sending Emails with SMTP, Configuring SMTP settings
var smtpTransport = nodemailer.createTransport({
host: "mail.vyg.cl", // hostname
secureConnection: true, // use SSL
port: 465, // port for secure SMTP
auth: {
user: 'crm@vyg.cl',
pass: '*******'
}
});
const mailOptions = {
from: req.body.from, // sender address
to: req.body.to, // list of receivers
cc: req.body.cc,
subject: req.body.subject, // Subject line
//text: "Hello world ✔", // plaintext body
// html: req.body.description, // html body,
attachments: [{ // file on disk as an attachment
filename: req.body.subject + '.pdf',
path: './server/' + req.body.subject + '.pdf' // stream this file
}]
}
const pdfArchive = './server/' + req.body.subject + '.pdf';
if (fs.existsSync(pdfArchive)) {
await fs.unlinkSync(pdfArchive);// Add wait here
}
try{
var result = await pdf.create(req.body.description).toFile('./server/' + req.body.subject + '.pdf') // capture result if you need to
try{
await smtpTransport.sendMail(mailOptions) // wrap this in a try catch if you want to capture email sending specific erorrs else outer try catch will work
res.status(200).json({ message: "Email has been sent successfully" });
}
catch(error)
{
res.status(400).json({ message: "Email could not sent due to error: " + error });
}
}
catch(error)
{
console.log(err);
res.send(error)
}
});