【问题标题】:Mandrill JSON API with Meteor HTTP.postMandrill JSON API 与 Meteor HTTP.post
【发布时间】:2016-05-05 05:32:09
【问题描述】:

我正在字符串化 emailData 并将其发送到 Mandrill URL,但没有成功:

Meteor.methods({
    'sendProjectApprovedEmail'(projectId){
        check(projectId, String);

        let project = Projects.findOne({_id: projectId});
        let user = Meteor.users.findOne({_id: project['userId']});

        let message = `<p>Hola ${user['services']['facebook']['first_name']}, enviamos este correo para notificarte que tu proyecto:</p>`;

        let emailData = {
            "key": Meteor.settings.mandrillApiKey,
            "message": [{
                "html": message,
                "subject": "Tu proyecto ha sido aprobado",
                "from_email": "mail@mail.com",
                "from_name": "name.com",
                "to": [{
                  "email": user['services']['facebook']['email'],
                  "name": user['services']['facebook']['first_name'],
                  "type": "to"
                }],
                "headers": {
                  "Reply-To": "mail@mail.com"
                },
                "track_opens": true,
            "track_clicks": true,
            "inline_css": true
            }],
            "async": false
        };

        HTTP.post('https://mandrillapp.com/api/1.0/messages/send.json', {
            data: JSON.stringify(emailData),
            headers: {
                'Content-Type': 'application/json; charset=utf-8'
            }
        }, function(error, data){
            if (error) { console.log(error) }
            else { console.log(data) }
        });
    }
});

JSON.stringify 输出如下:

console.log(JSON.stringify(emailData));

{"key":"XXXXXX...","message":[{"html":"<p>Hola Gustavo, enviamos este correo para notificarte que tu proyecto:</p>","subject":"Tu proyecto ha sido aprobado","from_email":"mail@mail.com","from_name":"DeveloperFullstack.com","to":[{"email":"mail@gmail.com","name":"Gustavo","type":"to"}],"headers":{"Reply-To":"contacto@developerfullstack.com"},"track_opens":true,"track_clicks":true,"inline_css":true}],"async":false}

但响应返回 500 错误:

{ [Error: failed [500] {"status":"error","code":-1,"name":"ValidationError","message":"You must specify a key value"}]

我也试过了:

  • let emailData = [{//...}]
  • let emailData = {"key": "XXX...", "message": {//...}}

正如其他 SO 答案所暗示的那样,没有成功。

更新

为了记录,使它与:

let emailData = {
    "key": Meteor.settings.mandrillApiKey,
    "message": {
        "html": message,
        "subject": "Tu proyecto ha sido aprobado",
        "from_email": "mail@mail.com",
        "from_name": "name.com",
        "to": [{
          "email": user['services']['facebook']['email'],
          "name": user['services']['facebook']['first_name'],
          "type": "to"
        }],
        "headers": {
          "Reply-To": "mail@mail.com"
        },
        "track_opens": true,
        "track_clicks": true,
        "inline_css": true
    },
    "async": false
};

HTTP.post('https://mandrillapp.com/api/1.0/messages/send.json', {
    data: emailData,
    headers: {
        'User-Agent': 'Meteor app DevFS@1.0.0'
    }
}, function(error, data){
    if (error) { console.log(error) }
    else { console.log(data) }
});

【问题讨论】:

    标签: json meteor mandrill


    【解决方案1】:

    好吧,我将向您展示我是如何使用 Mandrill 成功发送电子邮件的:

    http = Npm.require("https");
    
    Meteor.methods({
    
        sendEmail: function(data) {
    
            var message = {
                'subject': data.subject,
                'text': data.text,
                'html': data.html,
                'from_email': "contact@mydomain.com",
                'from_name': "MyDomain",
                'to': [{ 'email': data.email, 'name': data.name, 'type': "to" }],
                'headers': { 'Reply-To': "contact@mydomain.com" },
                'important': false,
                'track_opens': false,
                'track_clicks': false,
                'auto_text': true
            };
    
            var options = {
                "method": "POST",
                "hostname": "mandrillapp.com",
                "port": null,
                "path": "/api/1.0/messages/send.json",
                "headers": {
                    "content-type": "text/html; charset=utf-8"
                }
            };
    
            var req = http.request(options, function(res) {
                var chunks = [];
                res.on("data", function(chunk) {
                    chunks.push(chunk);
                });
                res.on("end", function() {
                    var body = Buffer.concat(chunks);
                    console.log(body.toString()); // response from Mandrill
                });
            });
    
            req.write(JSON.stringify({
                'key': Meteor.settings.mandrill.apiKey,
                'message': message,
                'async': false
            }));
    
            req.end();
        }
    });
    

    您不需要任何软件包来执行此操作。

    【讨论】:

    • 我尝试过类似的方法,但 Npm.require 在部署时没有好运。你是如何部署的?
    • 我正在使用 Meteor Up 部署到 AWS 的 EC2 机器上:github.com/arunoda/meteor-up
    猜你喜欢
    • 2014-04-18
    • 2015-01-01
    • 2016-01-09
    • 2015-06-21
    • 2016-09-16
    • 2013-02-12
    • 2017-03-29
    • 1970-01-01
    • 2014-07-10
    相关资源
    最近更新 更多