【问题标题】:meteorjs: Am I using Email.send() correctly?meteorjs:我正确使用 Email.send() 吗?
【发布时间】:2012-12-23 01:48:41
【问题描述】:
var SITE_URL = Meteor.absoluteUrl();

function sendEmailNotification(type, sourceUser, recipients, notificationObject) {
    var emailFrom = 'server@example.com';
    var emailSubject = 'MyApp: ';
    var emailBody = '';
    $.each(recipients, function (index, recipient) {
        recipients[index] = recipient + '@example.com';
    });

    switch(type) {
        case 'item_assigned':
            emailSubject += notificationObject.item_title;
            emailBody += '<div style="padding:10px;">';
            emailBody += sourceUser;
            emailBody += ' has assigned you an action item';
            emailBody += '</div>'
            break;
        case 'list_shared':
            emailSubject += notificationObject.list_title;
            emailBody += '<div style="padding:10px;">';
            emailBody += sourceUser;
            emailBody += ' has shared a list with you: ';
            emailBody += '<a href="' + SITE_URL + '#' + notificationObject.list_id + '">' + notificationObject.list_title + '</a>';
            emailBody += '</div>'
            break;
    }
    if (Meteor.isServer) {
        // This function only runs on server
        Email.send({
            from: emailFrom,
            bcc: recipients,
            subject: emailSubject,
            html: emailBody
        });
    }
}

上述函数位于根目录下的 JS 文件中(因此其代码对客户端和服务器都可用)。但是当我在我的客户端代码中调用它时,什么也没有发生。我的应用程序中包含email 包。在我的本地机器(Windows 7)上,我没有设置 MAIL_URL 变量。因此,调用Email.send() 函数理想情况下应该在命令提示符下产生输出,但实际上没有输出。

在我们的生产服务器上,SMTP 已正确设置,其他应用程序能够发送具有相同设置的电子邮件。我已经在那里正确配置了MAIL_URL 环境变量,但仍然没有发送电子邮件。

谁能告诉我我的代码是否有问题?有什么我做的不对吗?

P.S.:我什至尝试像下面的代码一样直接调用 Email.send(),但仍然没有任何反应。

if (Meteor.isServer) {
    Email.send({
        from: 'server@example.com',
        to: 'my-gmail-id@gmail.com',
        subject: 'This is a test email',
        html: '<b>Congrats, it works!</b>'
    });
}



    }
});

【问题讨论】:

  • 您也可以发布您的解决方案作为您自己问题的答案,这既可以为您赢得声誉,也可以帮助未来的访问者!

标签: javascript email meteor


【解决方案1】:

几乎是Meteor's Email is undefined的复制品

示例代码请参见this pull request

澄清一下:Meteor 不会像那样按顺序执行客户端和服务器代码。您必须更明确地了解客户端与服务器上运行的内容。与其考虑沿 JavaScript 页面的线性执行,不如考虑每段 Meteor 代码作为事件的结果运行。如果某段代码没有运行,那是因为没有触发它的事件。

【讨论】:

  • 谢谢大卫。我通过使用Meteor.methods 在我的服务器端代码中创建一个新方法来解决它,然后在客户端代码中使用Method.call 调用该服务器方法。但我强烈认为电子邮件部分在流星的官方 API 文档中有点令人困惑。他们应该在他们的文档中提供一个更明确的例子。
【解决方案2】:

我通过使用Meteor.methods 创建一个服务器端方法并将上面的整个代码放入其中来解决它。

var SITE_URL = Meteor.absoluteUrl();

Meteor.methods({
    sendEmailNotification: function (type, sourceUser, recipients, notificationObject) {
        if (recipients.length > 0) {
            var emailFrom = 'app@example.com';
            var emailSubject = 'MyApp: ';
            var emailBody = '';
            for (var i = 0; i < recipients.length; i++) {
                recipients[i] = recipients[i] + '@example.com';
            }

            switch (type) {
                case 'item_assigned':
                    emailSubject += notificationObject.item_title;
                    emailBody += '<div style="padding:10px;">';
                    emailBody += sourceUser;
                    emailBody += ' has assigned you an action item';
                    emailBody += '</div>'
                    break;
                case 'list_shared':
                    emailSubject += notificationObject.list_title;
                    emailBody += '<div style="padding:10px;">';
                    emailBody += sourceUser;
                    emailBody += ' has shared a list with you: ';
                    emailBody += '<a href="' + SITE_URL + '#' + notificationObject.list_id + '">' + notificationObject.list_title + '</a>';
                    emailBody += '</div>'
                    break;
            }
            Email.send({
                from: emailFrom,
                bcc: recipients,
                subject: emailSubject,
                html: emailBody
            });
        }
    }
});

要在您的客户端代码中调用上述函数,请使用:

Meteor.call('sendEmailNotification', 'list_shared', Meteor.user().username, [sharedUserName], listDetails);

【讨论】:

    猜你喜欢
    • 2014-12-28
    • 2014-02-28
    • 2015-06-25
    • 2022-01-08
    • 2011-08-07
    • 2011-09-26
    • 2019-05-03
    • 2016-03-12
    • 1970-01-01
    相关资源
    最近更新 更多