在发送邮件之前等待文件创建

时间:2019-04-25 21:05:25

标签: node.js nodemailer

我在使用nodemailer的功能时遇到一些问题,我需要从body req创建一个pdf,然后将其附加到邮件中并发送,这里的问题是我不知道如何等待文件完成保存后再进行操作发送邮件,并且由于邮件在系统可以创建文件甚至附加pdf文件之前就出现了错误,因此出现错误...

这个想法是:发送请求->获取请求正文->创建pdf->创建邮件选项(nodemailer)->发送邮件。

但是现在的代码是这样的:发送post req->获取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响应。...

任何帮助将不胜感激

1 个答案:

答案 0 :(得分:0)

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)
		}
    });