无法使用nodemailer发送带有附件的电子邮件 [英] Can't send Emails with attachments with nodemailer

查看:287
本文介绍了无法使用nodemailer发送带有附件的电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有以下使用nodemailer发送带有附件的电子邮件的功能,但是有时它返回错误信息,即使文件路径存在也找不到.你能告诉我我的错误在哪里吗?

I have the following function for sending emails with attachments using nodemailer, but sometimes It returns error enoent, the file path can't be found even if it exists. Can you tell me where is my mistake?

function sendEmail(userEmail, htmlString, requestSnap, FIREBASE_WEB) {

 fileName ="test.pdf";
 folderName = "./" + uuid.v4();
 mkdirp(folderName, function(err) {
    if (err) console.error(err)
        else console.log(folderName + ' folder created!')
    });

pdf.create(htmlString + userEmail, options).toFile(folderName + '/' + fileName, function(err, res) { // if the file doesnt exist it will be created
    if (err) return console.log(err);
    console.log(res);
});

var transporter = nodemailer.createTransport(smtpTransport({
    service: 'Gmail',
    auth: {
        user: '...',
        pass: '...'
    }
}));

console.log("\nPATH " + folderName + "/" + fileName);

var mailOptions = {
    from: 'marija.lukaroska.cw@gmail.com',
    to: userEmail,
    subject: 'So mail vo pdf-ot',
    text: 'Hellow',
    attachments: [{
        path: folderName + "/" + fileName
    }]
};

transporter.sendMail(mailOptions, function(error, info) {
    if (error) {
        console.log("ERROR kkkk " + error);
    } else {
        console.log('Email sent: ' + info.response);
        console.log("REQUEST SNAP " + JSON.stringify(requestSnap));
    }

    deleteFolderRecursive(folderName);
});

}

错误日志:

  ERROR kkkk Error: ENOENT: no such file or directory, open 'C:\Users\asd\Documents\Projects\asd\asd\010a3e0f-2f16-4227-a886-873a8529737f\asd.pdf' 

路径存在

推荐答案

由于节点Js是单线程的,是事件驱动的,因此这似乎是正确链接函数的问题.

As node Js is single threaded, event driven, this seems to be an issue of chaining your functions appropriately.

您的PDF创建代码需要花费一些时间才能返回,但是到那时您的发送邮件代码已经被调用,并且发现尚未创建文件夹.

Your PDF creation code is taking time to return but by that time your send mail code is already called and it finds the folder is not yet created.

尝试一下:

function sendEmail(userEmail, htmlString, requestSnap, FIREBASE_WEB) {

fileName = "test.pdf";
folderName = "./" + uuid.v4();
mkdirp(folderName, function (err) {
    if (err) console.error(err)
    else console.log(folderName + ' folder created!')
});

pdf.create(htmlString + userEmail, options).toFile(folderName + '/' + fileName, function (err, res) { // if the file doesnt exist it will be created
    if (err) return console.log(err);
    console.log(res);

    var transporter = nodemailer.createTransport(smtpTransport({
        service: 'Gmail',
        auth: {
            user: '...',
            pass: '...'
        }
    }));
    console.log("\nPATH " + folderName + "/" + fileName);

    var mailOptions = {
        from: 'marija.lukaroska.cw@gmail.com',
        to: userEmail,
        subject: 'So mail vo pdf-ot',
        text: 'Hellow',
        attachments: [{
            path: folderName + "/" + fileName
        }]
    };

    transporter.sendMail(mailOptions, function (error, info) {
        if (error) {
            console.log("ERROR kkkk " + error);
        } else {
            console.log('Email sent: ' + info.response);
            console.log("REQUEST SNAP " + JSON.stringify(requestSnap));
        }

        deleteFolderRecursive(folderName);
    });
});
}

这篇关于无法使用nodemailer发送带有附件的电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆