我有新帖子时需要通过电子邮件通知所有用户
服务器/ methods.js
Meteor.methods({
sendNewsletter: function(doc) {
var pipeline = [
{$group: {_id:"$emails.address"}}
];
var to = Meteor.users.aggregate(pipeline);
var text = "Title: " + doc.title + "\n\n"
+ "Summary: " + doc.summary + "\n\n\n\n"
this.unblock();
// Send the e-mail
Email.send({
to: to,
from: "noreply@domain.com",
subject: "MyApp - " + doc.title,
text: text
});
}
});
当我在客户端调用sendNewsletter方法时,我在终端上收到以下警告:
I20151030-04:16:42.039(7)? Exception while invoking method 'sendNewsletter' RecipientError: Can't send mail - all recipients were rejected
I20151030-04:16:42.041(7)? at Object.Future.wait (/Users/user/.meteor/packages/meteor-tool/.1.1.9.1f0n2l1++os.osx.x86_64+web.browser+web.cordova/mt-os.osx.x86_64/dev_bundle/server-lib/node_modules/fibers/future.js:398:15)
I20151030-04:16:42.042(7)? at smtpSend (packages/email/email.js:86:1)
I20151030-04:16:42.042(7)? at Object.Email.send (packages/email/email.js:176:1)
I20151030-04:16:42.043(7)? at maybeAuditArgumentChecks (livedata_server.js:1692:12)
I20151030-04:16:42.043(7)? at livedata_server.js:708:19
I20151030-04:16:42.043(7)? at [object Object]._.extend.withValue (packages/meteor/dynamics_nodejs.js:56:1)
I20151030-04:16:42.044(7)? at livedata_server.js:706:40
I20151030-04:16:42.044(7)? at [object Object]._.extend.withValue (packages/meteor/dynamics_nodejs.js:56:1)
I20151030-04:16:42.044(7)? at livedata_server.js:704:46
I20151030-04:16:42.044(7)? - - - - -
I20151030-04:16:42.045(7)? at SMTPClient._actionRCPT (/Users/user/.meteor/packages/email/.1.0.7.11df8pa++os+web.browser+web.cordova/npm/node_modules/simplesmtp/lib/client.js:909:27)
I20151030-04:16:42.045(7)? at SMTPClient._onData (/Users/user/.meteor/packages/email/.1.0.7.11df8pa++os+web.browser+web.cordova/npm/node_modules/simplesmtp/lib/client.js:329:29)
I20151030-04:16:42.045(7)? at CleartextStream.emit (events.js:95:17)
I20151030-04:16:42.045(7)? at CleartextStream.<anonymous> (_stream_readable.js:765:14)
I20151030-04:16:42.046(7)? at CleartextStream.emit (events.js:92:17)
I20151030-04:16:42.046(7)? at emitReadable_ (_stream_readable.js:427:10)
I20151030-04:16:42.046(7)? at _stream_readable.js:420:7
I20151030-04:16:42.046(7)? at process._tickCallback (node.js:448:13)
有谁知道如何向多个收件人发送电子邮件?谢谢你,,,,
答案 0 :(得分:2)
重新编写server/methods.js
文件,让to
变量保存一组电子邮件地址。
由于Meteor不支持MongoDB(尚未),你可以简单地在mongo shell中使用db.users.distinct("emails.address")
,你可以改为
在underscore.js库中使用三种方法 map()
, flatten()
和 uniq()
从用户集合中获取不同的电子邮件地址。
首先使用光标上的 .find()
将 .fetch()
返回的Meteor光标转换为数组。例如:
var users = Users.find({}, { fields: {"emails": true} }).fetch();
然后使用 map()
方法,通过转换函数(iteratee)映射列表中的每个文档,在另一个数组中生成一个仅包含电子邮件地址值的新数组。定义转换函数如下
var callback = function(doc) {
var emailAddresses = [];
for (var i = 0; i < doc.emails.length; i++) {
emailAddresses.push(doc.emails[i].address);
}
return emailAddresses;
};
var emails = _.map(users, callback);
您还可以使用与 map()
兼容的mongo光标的 Array.map()
方法直接获取数据集:
var emails = Users.find({}).map(callback);
使用 flatten()
展平数组以获取可用数组,然后使用 uniq()
方法返回一堆唯一数组作为如下:
var uniqueEmails = _.uniq(_.flatten(emails));
您的代码现在应该有一系列电子邮件地址,然后您可以发送
Meteor.methods({
sendNewsletter: function(doc) {
var users = Users.find({}, { fields: {"emails": true} }).fetch();
var callback = function(doc) {
var emailAddresses = [];
for (var i = 0; i < doc.emails.length; i++) {
emailAddresses.push(doc.emails[i].address);
}
return emailAddresses;
};
var emails = _.map(users, callback);
var to = _.uniq(_.flatten(emails));
var text = "Title: " + doc.title + "\n\n"
+ "Summary: " + doc.summary + "\n\n\n\n"
this.unblock();
// Send the e-mail
Email.send({
to: to,
from: "noreply@domain.com",
subject: "MyApp - " + doc.title,
text: text
});
}
});
查看下面的演示。
var users = [
{
"_id" : "ukn9MLo3hRYEpCCty",
"emails" : [
{
"address" : "a@a.com",
"verified" : false
}
]
},
{
"_id" : "5SXRXraariyhRQACe",
"emails" : [
{
"address" : "b@a.com",
"verified" : false
}
]
},
{
"_id" : "WMHWxeymY4ATWLXjz",
"emails" : [
{
"address" : "c@a.com",
"verified" : false
}
]
}
];
var callback = function(d) {
var emailAddresses = [];
for (var i = 0; i < d.emails.length; i++) {
emailAddresses.push(d.emails[i].address);
}
return emailAddresses;
};
var emails = users.map(callback);
var to = _.uniq(_.flatten(emails));
pre.innerHTML = JSON.stringify(to, null, 4);
<script src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.7.0/underscore-min.js"></script>
<pre id="pre"></pre>