Node.js并表达:nodemailer上的附件

时间:2017-01-17 09:25:32

标签: node.js express nodemailer

我想使用Nodemailer在电子邮件中附加文件。我按照本指南尝试了几种方法:https://nodemailer.com/using-attachments/,.pdf,.txt ......但没有任何效果。 有任何想法吗? 这是我最后一次尝试的代码:

router.post('/profilocliente/:id', function(req, res, next) {
  Customer.findById(req.params.id, function(err, customer){
    if(err) throw err;
    var mailOptions={
      to : customer.email,
      subject : req.body.oggetto,
      text : req.body.testo,
      attachments: [{path: 'C:/Users/Angelo/Desktop/test.txt'}]
    }
    console.log(mailOptions);
    smptTransport.sendMail(mailOptions, function(error, response){
      if(error){
        console.log(error);
        res.end("error");
      }else{
        console.log("Message sent: " + response.message);
        res.redirect('/clienti/profilocliente/'+customer._id);
      }
    })
  })
});

1 个答案:

答案 0 :(得分:0)

我有同样的问题,不知道为什么不能从桌面发送文件。将它保存在D驱动器中,从下面的项目目录中向外是我的代码

exports.sendmail=function (to,subject,text,html,info) {


var mailOptions = {
    attachments: [

        {   // file on disk as an attachment
            filename: 'ab.txt',
            path: 'D:/ab.txt' // stream this file
            //below one not working some time dont know why but its work for me now
             path: 'C:/Users/Me/Desktop/ab.txt'
            //this also work foo me
            path: 'D:/myproject/files/ab.txt'
        }],
    from: '"Testing Mail" <sender@gmail.com>', // sender address
    to: to, // list of receivers
    subject: subject, // Subject line
    text: text, // plaintext body
    html: html  // html body
};
transporter.sendMail(mailOptions, function(error, infoo){
    var res={
        responsecode:''
    }
    if(error){
        res.responsecode=401;
        return info(new Error(res.responsecode),null);
    }
    res.responsecode=200;
    info(null,res.responsecode+infoo.response);
});}

enter image description here