简体   繁体   中英

400 Error when attempting to send PDF through NodeMailer Mailgun Transport

I'm working on a simple webhook in Express to send a PDF through Mailgun on a certain event. However, whenever I try to send it, I get this error: { [Error: 'from' parameter is missing] statusCode: 400 }. I've tested it with simple .txt files and it works fine, so I'm assuming it has something to do with the PDF attachment. My code is here:

var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var nodemailer = require('nodemailer');
var PDFDocument = require('pdfkit');
var mg = require('nodemailer-mailgun-transport');
var fs = require('fs');

app.use(bodyParser.json());

app.post('/', function(req, res) {
 generatePDF(req.body.line_items);
 if (req.body.line_items) {
   sendMail();
   res.send('OK');
 }

});

app.listen(3030, function() {
 console.log('test app listening on port 3030');
});

function generatePDF(lineItems) {
 var doc = new PDFDocument();
 var writeStream = fs.createWriteStream('mailer/output.pdf');
 doc.pipe(writeStream);
 for (item in lineItems) {
   for (key in item) {
     doc.text(item[key]);
   }
 }

 console.log("doc: " + doc);
 doc.end();
}

function sendMail() {
 var auth = {
   auth: {
     api_key: 'my-key',
     domain: 'my-domain'
   }
 }

 var transporter = nodemailer.createTransport(mg(auth));
   var mailOptions = {
     from: 'my-email',
     to: 'their-email',
     subject: 'test',
     html: '<b>this is a test</b>',
     attachments: [
       { 
        filename: 'output.pdf',
        path: 'mailer/output.pdf',
         content: 'output mailer'
       }
     ],
   }

   transporter.sendMail(mailOptions, function(error, info){
     console.log('mail sent');
     if (error) {
       return console.error(error)
     }
     console.log('success!', info);
   })
 }

Any help would be much appreciated!

Forgot to post my solution, but here it is belatedly for future people running into this problem. The writeStream wasn't completing before I was sending it, and nodemailer was getting very confused, so I inserted a condition that the PDF (or whatever document) finish writing before it's sent.

    var express = require('express');
    var app = express();
    var bodyParser = require('body-parser');
    var nodemailer = require('nodemailer');
    var PDFDocument = require('pdfkit');
    var mg = require('nodemailer-mailgun-transport');
    var fs = require('fs');
    var idStorage = [];
    app.use(bodyParser.json());

    app.post('/', function(req, res) {
     var JSON = req.body;
     console.log(idStorage);
     if (req.body.line_items) {
      res.sendStatus(200);
      generatePDF(JSON);
     }

    });

    app.listen(3030, function() {
     console.log('example app listening on port 3030');
    });




    function generatePDF(JSON) {

        //create doc and doc variables
       var doc = new PDFDocument();
       var writeStream = fs.createWriteStream('mailer/output.pdf');
       doc.pipe(writeStream);
      //write whatever you want to the PDF
       doc.save();
    //wait for the PDF to finish writing before you send it. 
       writeStream.on('finish', function() {

           sendMail(JSON);
        });
      }
       doc.end();
    }
    }

function sendMail(JSON) {
       var auth = {
         auth: {
           api_key: 'key',
           domain: 'domain.com'
         }
       }

       var transporter = nodemailer.createTransport(mg(auth));
         var mailOptions = {
           from: 'me',
           to: 'you',
           subject: JSON.id,
           html: '<b>Test</b>',
           attachments: [
             { 
              filename: 'output.pdf',
              path: 'mailer/output.pdf'    
             }
           ],
         }

         transporter.sendMail(mailOptions, function(error, info){
           console.log('mail sent');
           if (error) {
             return console.error(error)
           }
           console.log('success!', info);
         })
       }
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM