PDF Attachment NodeMailer

半世苍凉 提交于 2019-12-23 20:32:12

问题


thanks in advance for your responses. I have written some code that uses nodemailer 0.7.1.It sends the email and attaches a pdf to the email. However,the .pdf attachment either corrupts itself when encoding or truncates or something. The reason I say this is the file before attachment(i.e. the one I have locally) is 512kb and the attachment in the email is only 1kb.

This is the code that uses nodemailer:

var nodemailer = require("nodemailer");
var util = require("./util");
var env = require('./environment');

var smtpTransport = nodemailer.createTransport("SMTP",{
    service: env.service,
    auth: {
        user: env.user,
        pass: env.password
    }
});

exports.sendAttachment = function(info, callback, debug) {
    util.validatInput(info, ["body"] , function(err, info){
        if(err){
            util.errPrint(err, "serverUtil/nodemailer.sendAttachment", 1, function(message){callback(err);});
        }else {
            var mailOptions={
                from : "noreply@idasurance.com",
                to : "tgraham@maurasoftware.com",
                subject : "Application from " + info.userEmail,
                text : info.body,
                attachments: [
                    {
                        fileName: 'file.pdf',               //This needs to be the link to the form, or the actual form
                        filePath: 'file.pdf',
                        contentType: "application/pdf"
                    }
                ]
            }

            smtpTransport.sendMail(mailOptions, function(error, response){
                
                if(error){
                    console.log(error);
                    callback(err);
                }
                else{
                    console.log("Message sent: " + response.message);
                    callback({msg: "form sent"});
                }
            }); 
        }
    })
}

I am using google chrome as a browser but have tried with other browsers to no avail. Obviously though browsers shouldn't have anything to do with this as the data of the pdf itself is the issue here.

I put the file in the same directory to avoid problems and even did './'before the file for current directory. I also changed 'filepath' to 'path' and then it didn't send any attachment at all.

I think the problem is in the 'attachments' array. Maybe the fields aren't right or I need to add some more information.

If anyone can tell me if I need to stream or something rather than what I am doing and if so how to stream the file that would be great!


回答1:


var api_key = 'key-6b6987887a1aa9489958a5f280645f8b';
var domain = 'sandboxcd1a6d15d41541f38519af3f5ee93190.mailgun.org';
var mailgun = require('mailgun-js')({apiKey: api_key,domain:domain});
var path = require("path");

var filepath = path.join(__dirname, 'wacc.pdf');

var data = {
  from: 'me@gmail.com',
  to: 'you@gmail.com',
  subject: 'Today Test',
  text: 'Sending Test',
  attachment: filepath
};

mailgun.messages().send(data, function (error, body) {
  console.log(body);
});



回答2:


It turned out I needed to get rid of the filePath and contentType attributes and put streamSource instead. I also needed to use fs.createReadStream. Here is the code if you are interested.

var nodemailer = require("nodemailer");
var util = require("./util");
var env = require('./environment');
var fs = require('fs');
var path = require('path');
var smtpTransport = nodemailer.createTransport("SMTP", {
  service: env.service,
  auth: {
    user: env.user,
    pass: env.password
  }
});

exports.sendAttachment = function(info, callback, debug) {
  util.validatInput(info, ["body"], function(err, info) {
    if (err) {
      util.errPrint(err, "serverUtil/nodemailer.sendAttachment", 1, function(message) {
        callback(err);
      });
    } else {
      var filePath = path.join(__dirname, 'file.pdf');

      var mailOptions = {
        from: "noreply@idasurance.com",
        to: "tgraham@maurasoftware.com",
        subject: "Application from " + info.userEmail,
        text: info.body,
        attachments: [{
          fileName: 'file.pdf', //This needs to be the link to the form, or the actual form
          // filePath: './file.pdf',
          streamSource: fs.createReadStream(filePath)
            // , contentType: "application/pdf"
        }]
      }

      smtpTransport.sendMail(mailOptions, function(error, response) {

        if (error) {
          console.log(error);
          callback(err);
        } else {
          console.log("Message sent: " + response.message);
          callback({
            msg: "form sent"
          });
        }
      });
    }
  })
}


来源:https://stackoverflow.com/questions/37232326/pdf-attachment-nodemailer

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