【问题标题】:PDF Attachment NodeMailerPDF 附件 NodeMailer
【发布时间】:2016-05-14 21:58:58
【问题描述】:

提前感谢您的回复。我编写了一些使用 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 本身的数据是这里的问题。

为了避免出现问题,我将文件放在同一目录中,甚至在当前目录的文件之前做了'./'。我还将“文件路径”更改为“路径”,然后它根本没有发送任何附件。

我认为问题出在“附件”数组中。可能字段不正确,或者我需要添加更多信息。

如果有人可以告诉我是否需要流式传输或其他事情,而不是我在做什么,如果需要,如何流式传输文件会很棒!

【问题讨论】:

    标签: node.js pdf email-attachments nodemailer


    【解决方案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】:

      事实证明,我需要去掉 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"
                });
              }
            });
          }
        })
      }

      【讨论】:

        猜你喜欢
        • 2016-03-30
        • 2020-08-05
        • 2016-06-13
        • 1970-01-01
        • 2017-11-16
        • 1970-01-01
        • 1970-01-01
        • 2021-09-03
        • 2016-04-11
        相关资源
        最近更新 更多