提前感谢您的回复。我已经编写了一些使用nodemailer 0.7.1的代码,它发送电子邮件并将pdf附加到电子邮件中。但是,.pdf附件在编码或截断时会损坏自身。我说这是因为附件前的文件(即我在本地有的)是512 1kb,而电子邮件中的附件只有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”,然后它根本没有发送任何附件。
我认为问题在于“附件”数组。也许字段不正确,或者我需要添加更多的信息。
如果有人可以告诉我,如果我需要流或其他东西,而不是我正在做什么,如果是的话,如何流的文件,这将是伟大的!
发布于 2016-05-16 16:50:24
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);
});发布于 2016-05-15 01:36:57
结果,我需要去掉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"
});
}
});
}
})
}
https://stackoverflow.com/questions/37232326
复制相似问题