在lambda函数中调用外部邮件api

时间:2019-04-23 12:53:50

标签: amazon-web-services api aws-lambda

我使用claudiajs apibuilder开发一些REST API。我想在此api中使用pepipost电子邮件api发送交易电子邮件。在测试期间,该API在我的lambda函数之外运行良好。当我在lambda中包含Epi时,它们将停止工作,并且不会引发任何错误。

有一些权限可以设置吗?或调用外部api所需的其他设置?

我试图清理该功能,只保留发送邮件以更好地对其进行调试

这是测试目的

api.post('/users/test', function(request) {

    return new Promise((resolve, reject) => {

        let eTo = "email@email.com", 
            eSubject = 'Activate your account',
            eAttributes =  {
                            THE_NAME            :   "userData.name",
                            THE_ACTIVATION_URL  :   "config.activationUrl"+"/123456",
                            THE_ACTIVATION_LINK :   "config.activationLink",
                            THE_ACTIVATION_CODE :   "aToken"
                        },
            eType = 'eTActivationEN',
            eTags = 'activation';

        sendemail( eTo, eSubject, eAttributes, eType, eTags );
        resolve( { "success" : "works" } );


    });

},{ success : { code : 200}, error : { code : 401 } });

这是发送电子邮件的模块:

var http = require("https");
var e = require('./../config.json');


module.exports.send = ( eTo, eSubject, eAttributes, eType, eTags ) => {
    return new Promise( (resolve, reject) => {
        var options = {
          "method": "POST",
          "hostname": "api.pepipost.com",
          "port": null,
          "path": "/v2/sendEmail",
          "headers": {
            "content-type": "application/json",
            "api_key": e.ePepipostToken
          }
        };

        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);
            output = body.toString();
            resolve ( output );
          });
        });

        req.write(JSON.stringify({ 
            personalizations: [ 
                { 
                    recipient: eTo,
                    attributes: eAttributes
                } 
            ],
            from: { fromEmail: e.eFromAddress, fromName: e.eFromName },
            subject: eSubject,
            templateId: 18924,
            tags: eTags,
            content: "content" }));
        req.end();

    }) // end Promise
}

相同的模块在lambda外部运行良好,但在内部不运行。 当我尝试呼叫端点时,我收到{“ success”:“ works”},但没有电子邮件。且在cloudwatch控制台中也没有错误。

2 个答案:

答案 0 :(得分:0)

更新:

如果我包含所有发送电子邮件代码,则可以使用:

api.post('/users/test2', function(request) {

    return new Promise((resolve, reject) => {

var http = require("https");


            var options = {
              "method": "POST",
              "hostname": "api.pepipost.com",
              "port": null,
              "path": "/v2/sendEmail",
              "headers": {
                "content-type": "application/json",
                "api_key": config.ePepipostToken
              }
            };

            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);
                output = body.toString();
                resolve ( output );
              });
            });

            eAttributes =  {
                THE_NAME            :   "userData.name",
                THE_ACTIVATION_URL  :   "config.activationUrl"+"/123456",
                THE_ACTIVATION_LINK :   "config.activationLink",
                THE_ACTIVATION_CODE :   "aToken"
            }

            req.write(JSON.stringify({ 
                personalizations: [ 
                    { 
                        recipient: "email@email.com",
                        attributes: eAttributes
                    } 
                ],
                from: { fromEmail: "devm425tj@pepisandbox.com", fromName: "me" },
                subject: "eSubject",
                templateId: 18924,
                tags: "activation",
                content: "content" }));
            req.end();


    });

},{ success : { code : 200}, error : { code : 401 } });

问题是我无法为需要发送的每封电子邮件都包含相同的代码。我需要让它从外部模块开始工作。

答案 1 :(得分:0)

第二次更新:代码运行良好,问题出在caludiajs apibuilder中,我无法从api.post(“ something”)块中调用外部函数,我将尝试说明如何操作。.