Can't send Emails with attachments with nodemailer

心不动则不痛 提交于 2019-12-12 05:14:10

问题


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 log:

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

the path exists


回答1:


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

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.

Try this:

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


来源:https://stackoverflow.com/questions/44884455/cant-send-emails-with-attachments-with-nodemailer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!