如何修复err:nodemailer附件发送后无法加载PDF文档? [英] how to fix err:Failed to load PDF document once sent by nodemailer attachments?

查看:78
本文介绍了如何修复err:nodemailer附件发送后无法加载PDF文档?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,可以使用Nodemailer发送带有附件的电子邮件.首先,我使用multer将附件保存在服务器上(运行良好),因此我可以将文件的路径传递给nodemailer路由中的附件.电子邮件和附件已成功发送,但附件无法打开/阅读!

I got an app which sending emails with attachments using Nodemailer. First I'm using multer to save the attachment on the server (working well) so I can pass the path of the file to attachments in the nodemailer route. The email and attachment is successfully sent but the attachment failed to be opened/read!

这是nodemailer路由:

Here is the nodemailer route:

 app.post("/send",  function(req,res){

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

var quotename = req.params.id;
console.log(quotename);

var storage = multer.diskStorage({
    destination: function (req,file, callback){
        callback(null, './uploads');
    },

    filename: function (req, file, callback){
        callback(null, "Q" + quotename + ".pdf");
    }
});

var upload = multer ({storage : storage}).single("myFile");


upload(req, res, function(err){
    if(err){
        console.log(err);
    } else {        

        const mailOptions = {
          from: req.body.fr, // sender address
          to: req.body.to, // list of receivers
          bcc: req.body.fr,
          subject: req.body.subject, // Subject line
          html: '<h4>Dear ' + req.body.contname+ '</h4>' + '<p>'+ 
         req.body.message + '</p>' + '<p>Kind Regards</p>' + 
           req.body.user,// html body
          attachments: [  
                {   // filename and content type is derived from path
                  filePath: '/constimator/uploads/Q' + quotename + '.pdf',
                },   
            ], 
        };

        transporter.sendMail(mailOptions, function (err, info) {
           if(err){
             return console.log(err);
           }else{
            console.log(req.body.myFile)
            res.redirect("/submitted"); }
          });
        });
     });

我不知道这是由于文件路径/路径问题还是其他原因.我也尝试过path(而不是filePath),但是有同样的问题

I don't know if it's because of filepath/path issue or something else. I tried path(instead of filePath) as well but the same problem

推荐答案

经过大量的反复试验,谷歌搜索和思考,我自己解决了问题.为了将来的参考以及遇到同样问题的人们,我将我的回答发布如下:

I solve the problem myself after lots of trial and error, googling, thinking. For the future refrence and for the people with the same problem, I post my answer as follows:

我已对此附件进行了更改,并且效果很好:

I have changed the attachments to this and it worked perfectly:

    {   // filename and content type is derived from path
                  filename: "Q" + quotename + ".pdf",
                  contentType: 'application/pdf',
                  path: '/constimator/uploads/Q' + quotename + '.pdf',
                },   
            ],

这很简单,只需添加文件名,内容类型并将文件路径更改为路径,即可解决问题

It was very simple, by adding filename, content type and change filepath to path, probelm solved

这篇关于如何修复err:nodemailer附件发送后无法加载PDF文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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