4
votes

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 : "[email protected]",
                to : "[email protected]",
                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!

2

2 Answers

2
votes
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: '[email protected]',
  to: '[email protected]',
  subject: 'Today Test',
  text: 'Sending Test',
  attachment: filepath
};

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

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: "[email protected]",
        to: "[email protected]",
        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"
          });
        }
      });
    }
  })
}