【问题标题】:Meteor : Email a Template in Client using MailgunMeteor:使用 Mailgun 在客户端中通过电子邮件发送模板
【发布时间】:2015-03-29 21:08:51
【问题描述】:

我在客户端有一个模板

<template name="sendThis">
<img src="logo.png"><br>
<h3>Welcome to Meteor NewBie</h3>
Dear {{name}},
<p>You received this Email because you have subscribed to http://www.example.com</p>
</template>

我想将此模板(sendThis)作为我的电子邮件中的 HTML 正文发送给订阅者。

我使用 Mailgun 作为我的电子邮件客户端。当订阅者单击 ID 为“订阅”的按钮时,我应该采取哪些步骤来实现这一点。

PS:我在这个模板中有多个助手,在某种意义上说是 20 多个。

提前致谢。 马赫什 B.

【问题讨论】:

    标签: email templates meteor server mailgun


    【解决方案1】:

    解决此问题的一种方法是使用Blaze.toHTMLWithData 将您的模板(带有上下文)呈现为 HTML 字符串。然后,您可以在服务器上调用一个方法,该方法通过电子邮件向用户发送适当的主题和地址。这是一个例子:

    客户

    var sendSignupEmail = function() {
      // This assumes this first email address is the one you want.
      // In some cases you may want the first verified email, but not
      // during signup.
      var address = Meteor.user().emails[0].address;
    
      var subject = 'Thanks for signing up!';
    
      // Here I used username - replace this with the appropriate data
      // like Meteor.user().profile.firstName or something.
      var body = Blaze.toHTMLWithData(Template.sendThis, {
        name: Meteor.user().username
      });
      Meteor.call('sendEmail', address, subject, body);
    };
    

    服务器

    Meteor.methods({
      sendEmail: function(to, subject, html) {
        check([to, subject, html], [String]);
        this.unblock();
    
        return Email.send({
          to: to,
          from: 'something@example.com',
          subject: subject,
          html: html
        });
      }
    });
    

    还要确保您的 MAIL_URL 环境变量已定义。

    【讨论】:

    • 我们如何给这个var body = Blaze.toHTMLWithData(Template.sendThis, { name: Meteor.user().username });带来一些帮助,因为我们在这里有名字,我们可以在里面调用一些函数吗?
    • 我不太确定你在问什么。你可以说得更详细点吗?如果你用上面的代码做一个简单的测试,它对你有用吗?
    • 我在这里有 name 作为变量。这是 Meteor.user().username 。我想在这个模板中添加更多变量,其中一些是从其他模板或 Mongo DB 计算出来的值。我的问题是,当我们将name 添加到电子邮件模板时,我们可以将数据库中的其他所需数据添加到此模板吗?我想这个调用应该发生在 Blaze.toHTMLWithDate() 中希望你明白我的想法,否则我可以分享更多我拥有的代码
    • 它和其他模板一样 - 您只是将其呈现为字符串而不是页面正文。这意味着您可以传递您喜欢的任何上下文,使用帮助程序等。例如,您可以创建一个新项目,单击hello 模板中的按钮,然后调用Blaze.toHTML(Template.hello),您将看到一个包含'&lt;p&gt;You've pressed the button 1 times.&lt;/p&gt;'.
    猜你喜欢
    • 1970-01-01
    • 2022-01-13
    • 1970-01-01
    • 1970-01-01
    • 2019-04-28
    • 2016-10-06
    • 1970-01-01
    • 1970-01-01
    • 2013-09-30
    相关资源
    最近更新 更多