AngularJS - 运行mandrill服务

时间:2015-02-20 14:04:45

标签: angularjs firebase mandrill

我正在构建一个小应用程序,需要每小时左右通过mandrill发送邮件。我想将执行此操作的函数绑定到应用程序中的正确位置。

基本上,我有一个带有日期/时间的Firebase参考,每小时,我需要Mandrill来运行Firebase阵列并相应地发送电子邮件。我有一个简单的Mandrill服务,我可以使用,只需要知道放置实际发送逻辑的最佳位置。

基本Mandrill服务:

.factory('mandrill', function() {
       return {
         initMandrill: function() {
           return new mandrill.Mandrill('XXX', true);
         }
       };
     });

1 个答案:

答案 0 :(得分:4)

您可以使用Mandrill JSON API执行以下操作。您可以从工厂对您的Firebase进行调用,传入您要分发的对象数据。

.factory('Mandrill', ['$http',
    function($http) {

      /*=======================*/
      /*  Insert Emails Here
      /*=======================*/

      var fromEmail = 'email';
      var fromName = 'email_name';
      var replyTo = 'email';

      return {
        messageWork: function(resp) {
          return $http.post('https://mandrillapp.com/api/1.0//messages/send.json', {
            'key': mandrillkey,
            'message': {
              'html': '<p>Unknown Message</p><p>' + resp + '</p><p>Code:' + resp.messagetext + '</p>',
              'text': resp,
              'subject': 'Unknown Message',
              'from_email': fromEmail,
              'from_name': fromName,
              'to': [
              {
                'email': resp.toEmail,
                'name': resp.toName,
                'type': 'to'
              }
              ],
              'headers': {
                'Reply-To': replyTo
              }
            }
          })
          .success(function(data, status, headers, config){
            // log success
          });
        }
      };
  }]);

我想知道Angular是否是此类工作的最佳用例,原因是a)您规定的时间限制(每小时)和b)您的Mandrill密钥在客户端代码中的暴露。 If you look at the integration options,他们提供了一些后端解决方案。

我建议您考虑使用Node进行此类工作,node-schedulelater会处理您的时间模式,而Mandrill的node APIdoes Firebase 。只是一个想法。