我有这个系统尝试发送用NodeJS编写的批量电子邮件。
它使用2个模块:fs(构建到节点v0.8.18)和nodemailer(来自NPM,v0.3.42)。
现在使用node newsletter.js
执行时,它有时会完成并完成,但有时会随机失败,并出现以下错误:
stream.js:81
throw er; // Unhandled stream error in pipe.
^
Error: write EPIPE
at errnoException (net.js:770:11)
at Object.afterWrite (net.js:594:19)
据我所知,发生EPIPE错误是因为连接的另一端掉线然后我们尝试写入该连接。此EPIPE错误错误的所有现有报告都在打开http连接或类似连接的上下文中。
我在下面的内容中可能导致错误的两件事是NewsletterEmail中的fs.readFileSync
或NewsleterMailer中的mailer.send
。它更可能是错误发生在mailer.send和nodemailer在某处打开连接。但是,没有错误通过throw或callback错误参数传回,因此似乎无法查看导致错误的原因。
大多数人建议定义错误处理程序。但是,nodemailer或fs模块中没有任何内容允许我定义错误处理程序。来自mailer.send
调用的nodemailer中的回调确实在其回调中传递了一个错误参数,但是这个特定的错误并没有通过那里传递。
我尝试了以下事项:
fs.readFileSync
和nodemailer传输sendEmail调用周围添加try / catch。触发此错误的代码如下:
var nodemailer = require('nodemailer');
var fs = require('fs');
/**
* Provides a way to build newsletters when given a folder containing
* the relevant template and images in a standard format. This folder
* must contain a newsletter.html file, a newsletter.txt file and a images
* directory containing any images.
*
* @param {Object} settings The full folder path to the
*/
function NewsletterEmail(newsletterGroup, newsletterName)
{
var folder = '/var/newsletters/' + newsletterGroup + '/' + newsletterName;
this._html = fs.readFileSync(folder + '/newsletter.html', 'utf-8');
this._text = fs.readFileSync(folder + '/newsletter.txt', 'utf-8');
}
NewsletterEmail.prototype.getSubject = function()
{
return 'Testing';
}
/**
* Generates the HTML version of a newsletter.
*
* @return {String}
*/
NewsletterEmail.prototype.buildHTML = function(email)
{
var htmlPart = this._html;
return htmlPart;
}
/**
* Generates the text counterpart of a newsletter.
*
* @return {String}
*/
NewsletterEmail.prototype.buildText = function(email)
{
var textPart = this._text;
return textPart;
}
/**
* Creates a NewsletterEmail from the given folder.
*
* @param {String} folder The folder containing the html template, text template and images for a newsletter.
* @return {NewsletterEmail}
*/
NewsletterEmail.create = function(newsleterGroup, newsletterName) {
return new NewsletterEmail(newsleterGroup, newsletterName);
}
function NewsletterMailer(fromEmail)
{
this._from = fromEmail;
this._transport = nodemailer.createTransport('sendmail');
}
NewsletterMailer.prototype = {
send: function(email, newsletterEmail, callback) {
var mailOptions = {
to: email,
from: this._from,
subject: newsletterEmail.getSubject(),
html: newsletterEmail.buildHTML(email),
text: newsletterEmail.buildText(email)
};
this._transport.sendMail(mailOptions, callback);
},
close: function() {
this._transport.close();
}
}
function Newsletter()
{
this._id = 1;
this.countSent = 0;
this.emailsToSend = ['email1@example.com', 'email2@example.com', 'email3@example.com', 'email4@example.com', 'email5@example.com', 'email6@example.com'];
}
Newsletter.prototype.send = function() {
var newsletter = this;
var newsletterEmail = NewsletterEmail.create('company1', '2013-01-24-mynewsleter');
var mailer = new NewsletterMailer('company@example.com');
function sendEmail() {
var email = newsletter.emailsToSend.pop();
mailer.send(email, newsletterEmail, function(mailerErr) {
if (mailerErr) {
console.log('Mailer error: ', mailerErr);
}
newsletter.countSent++;
console.log('progress ' + newsletter.countSent);
if (newsletter.emailsToSend.length > 0) {
sendEmail();
}
else {
mailer.close();
console.log('complete');
}
});
}
sendEmail();
}
var nl = new Newsletter();
nl.send();
还有其他人遇到过类似的错误吗?您是否有任何调试提示或可能的解决方案。
现在,堆栈跟踪给出了一点死路。以下是strace的输出。似乎总是先死在邮件mimepart边界上:
futex(0x7f039c0008c8, FUTEX_WAKE_PRIVATE, 1) = 1
write(8, "------Nodemailer-0.3.42-?=_1-136"..., 131) = -1 EPIPE (Broken pipe)
--- SIGPIPE {si_signo=SIGPIPE, si_code=SI_USER, si_pid=13813, si_uid=0} ---
--- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=13818, si_status=0, si_utime=0, si_stime=0} ---
write(4, "\1\0\0\0\0\0\0\0", 8) = 8
rt_sigreturn() = -1 EPIPE (Broken pipe)
futex(0x7f039c0008c8, FUTEX_WAKE_PRIVATE, 1) = 1
write(8, "<!DOCTYPE HTML PUBLIC =22-//W3C/"..., 18098) = -1 EPIPE (Broken pipe)
--- SIGPIPE {si_signo=SIGPIPE, si_code=SI_USER, si_pid=13813, si_uid=0} ---
futex(0x7f039c0008c8, FUTEX_WAKE_PRIVATE, 1) = 1
close(8) = 0
epoll_wait(3, {{EPOLLIN|EPOLLHUP, {u32=9, u64=4294967305}}, {EPOLLIN|EPOLLHUP, {u32=11, u64=4294967307}}, {EPOLLIN, {u32=4, u64=4294967300}}}, 64, 0) = 3
epoll_ctl(3, EPOLL_CTL_MOD, 9, {EPOLLIN, {u32=9, u64=4294967305}}) = 0
epoll_ctl(3, EPOLL_CTL_MOD, 11, {EPOLLIN, {u32=11, u64=4294967307}}) = 0
read(4, "\1\0\0\0\0\0\0\0", 8) = 8
wait4(-1, [{WIFEXITED(s) && WEXITSTATUS(s) == 0}], WNOHANG|WSTOPPED|WCONTINUED, NULL) = 13818
ioctl(1, SNDCTL_TMR_TIMEBASE or SNDRV_TIMER_IOCTL_NEXT_DEVICE or TCGETS, 0x7fff2d6f4340) = -1 EINVAL (Invalid argument)
fstat(1, {st_mode=S_IFIFO|0600, st_size=0, ...}) = 0
write(1, "progress 1\n", 11progress 1
) = 11
socketpair(PF_FILE, SOCK_STREAM|SOCK_CLOEXEC, 0, [7, 8]) = 0
socketpair(PF_FILE, SOCK_STREAM|SOCK_CLOEXEC, 0, [10, 12]) = 0
socketpair(PF_FILE, SOCK_STREAM|SOCK_CLOEXEC, 0, [13, 14]) = 0
pipe2([15, 16], O_NONBLOCK|O_CLOEXEC) = 0
clone(child_stack=0, flags=CLONE_CHILD_CLEARTID|CLONE_CHILD_SETTID|SIGCHLD, child_tidptr=0x7f03a4b35a10) = 13820
close(16) = 0
poll([{fd=15, events=POLLIN|POLLHUP}], 1, -1) = 1 ([{fd=15, revents=POLLHUP}])
close(15) = 0
close(7) = 0
ioctl(8, FIONBIO, [1]) = 0
close(12) = 0
ioctl(10, FIONBIO, [1]) = 0
close(14) = 0
ioctl(13, FIONBIO, [1]) = 0
wait4(-1, 0x7fff2d6f529c, WNOHANG|WSTOPPED|WCONTINUED, NULL) = 0
futex(0x7f039c0008c8, FUTEX_WAKE_PRIVATE, 1) = 1
brk(0x932000) = 0x932000
read(11, "", 65536) = 0
close(11) = 0
read(9, "", 65536) = 0
futex(0x7f039c0008c8, FUTEX_WAKE_PRIVATE, 1) = 1
close(9) = 0
write(2, "\n", 1
) = 1
write(2, "events.js:71\n", 13events.js:71
答案 0 :(得分:5)
这是由nodemailer模块包裹75个字符的行引起的。一行正好是76个字符,最后有一个点。这个点被包裹在它自己的线上。
对于SMTP服务器,一行上的点表示消息的结束并且连接已关闭。这导致连接过早关闭,后续写入因EPIPE错误而失败。
在nodemailer维护者的帮助下,现在通过在sendmail调用中添加hte -i标志来修复问题0.3.43,该调用告诉sendmail允许带有单个点的行。
此处提供了更多详细信息:https://github.com/andris9/Nodemailer/issues/141。