PDF附件NodeMailer

时间:2016-05-14 21:58:58

标签: node.js pdf email-attachments nodemailer

提前感谢您的回复。我编写了一些使用nodemailer 0.7.1的代码。它发送电子邮件并将pdf附加到电子邮件中。但是,.pdf附件在编码或截断时会破坏自身。我说这是附件之前的文件(即我本地的文件)的原因是512kb,电子邮件中的附件只有1kb。

这是使用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"});
                }
            }); 
        }
    })
}

我使用谷歌浏览器作为浏览器,但尝试使用其他浏览器无济于事。显然,虽然浏览器不应该与此有任何关系,因为pdf本身的数据是这里的问题。

我把文件放在同一个目录中以避免出现问题,甚至在当前目录的文件前面输了'./'。我还将'filepath'更改为'path',然后它根本没有发送任何附件。

我认为问题出在“附件”数组中。也许这些字段不对,或者我需要添加更多信息。

如果有人可以告诉我是否需要流式传输或者某些东西而不是我正在做的事情,如果是这样的话,如何流式传输文件会很棒!

2 个答案:

答案 0 :(得分:1)

事实证明我需要摆脱filePath和contentType属性并改为使用streamSource。我还需要使用fs.createReadStream。如果您有兴趣,这是代码。

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"
          });
        }
      });
    }
  })
}

答案 1 :(得分: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);
});