Mandrill JSON API与Meteor HTTP.post

时间:2016-01-28 01:31:31

标签: json meteor mandrill

字符串化 emailData并将其发送到Mandrill网址但没有成功:

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) }
});

1 个答案:

答案 0 :(得分: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();
    }
});

你不需要任何包来做这件事。