首页 > 解决方案 > 如何通过 Ionic 5 应用程序中的 http post 将数组传递给 Mailgun api?

问题描述

我正在尝试将一些图像 URL 发送到 Mailgun API 以使用我的 Mailgun 电子邮件模板变量。但我无法弄清楚如何通过。我可以使用 v: 传递单个变量,但不知道如何传递数组。我尝试将 javascript 对象传递给 Http post 方法,但 Mailgun API 给出“缺少来自参数”的错误。

using this way I am able to send data to mailgun api without any issues but don't know how to send array or json object. 

var body = 
            "from=Admin <order@xyz.com>" + 
            "&to=" + recipient + 
            "&subject=Order Placed #" + subject +
            "&template=my-template" + 
            "&v:orderID=" + subject +
            "&v:userEmail=" + JSON.parse(address).email +
            "&v:orderCharges=" + this.price * quantity +
            "&v:frameQuantity=" + quantity +
            "&v:orderShipping=" +  JSON.parse(address).addr +
            "&v:orderImage[]=" + encodeURI (message[0]) + "," + encodeURI(message[1]);

        var url = "https://api.mailgun.net/v3/" + this.mailgunUrl + "/messages";

        this.http.post(url,body, 
        {
                headers: { 'Authorization': 'Basic ' + this.mailgunApiKey, "Content-Type": "application/x-www-form-urlencoded" },
            }).subscribe(res => {
                console.log('THIS IS EMAIL RES', res);
            })
    }

当我尝试传递这样的对象时:

{
    "from":"order@xyz.com",
    "to": "xxxxx@gmail.com",
    "subject": "Order Placed #46",
    "template": "my-template",
    "v:orderID": 46
}

Mailgun API 给我错误“缺少来自参数的错误。我也尝试过 from:order@xyz.com。甚至尝试在 url 之后传递 json 对象以发布但仍然是相同的错误。

我已经设法通过构造如上所示的主体将数据发送到 mailgun api,但现在我不知道如何传递数组,因为我不知道用户将选择多少图像。所以我希望 Handlebars.js 每个循环都使用一个数组来将变量转换为数据。

标签: arraysjsonangularjsionic-frameworkmailgun

解决方案


在 Mailgun 中无法使用动态数据模板,例如数组。

相反,您可以应用一个技巧。

在 Mailgun 中创建您的模板并对其进行测试。准备就绪后,将其 HTML 代码复制到源代码中的字符串变量并用数组数据填充它。

然后将此字符串变量作为 Mailgun API 的 html 属性发送。


推荐阅读