【问题标题】:'from' parameter not supported c++ libcurl mailgun api Error 400'from' 参数不支持 C++ libcurl mailgun api 错误 400
【发布时间】:2020-12-19 23:49:41
【问题描述】:

我正在尝试使用 c++ libcurl 使用 mailgun api 发送电子邮件。 api返回错误"message": "'from' parameter is not a valid address. please check documentation"

代码

    CURL* curl;
    CURLcode res;
    std::string readBuffer;
    std::string user = "api:" + (std::string)"MY_API_KEY";
    std::string data = "from='Bob Marley <bob@host.com>'&to='MY_EMAIL@gmail.com'&subject='Hello'&text='hi'";

    curl = curl_easy_init();
    if (curl) {
        curl_easy_setopt(curl, CURLOPT_URL, "https://api.mailgun.net/v3/MY_SANDBOX_URL.mailgun.org/messages");
        curl_easy_setopt(curl, CURLOPT_USERPWD, user.c_str());
        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_easy_setopt(curl, CURLOPT_POST, 1L);
        curl_easy_setopt(curl, CURLOPT_COPYPOSTFIELDS, data.c_str());
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writer);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
        curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
        res = curl_easy_perform(curl);
        curl_easy_cleanup(curl);

        std::cout << readBuffer << std::endl;
    }
    return 0;

书写器功能

int writer(void* contents, size_t size, size_t nmemb, void* userp) {
    ((std::string*)userp)->append((char*)contents, size * nmemb);
    return size * nmemb;
}

输出

*   Trying 44.241.76.64:443...
* Connected to api.mailgun.net (44.241.76.64) port 443 (#0)
* Server auth using Basic with user 'api'
> POST /v3/sandbox7847fafcfb12470f8e94e86efad974b4.mailgun.org/messages HTTP/1.1
Host: api.mailgun.net
Authorization: Basic YXBpOmYyMmUxZWEyODgzMTc1YjliYTNmMWMzYTY1Y2JlMTM5LTQ4NzlmZjI3LWEyZjM5MDYw
Accept: */*
Content-Length: 89
Content-Type: application/x-www-form-urlencoded

* upload completely sent off: 89 out of 89 bytes
* Mark bundle as not supporting multiuse
< HTTP/1.1 400 BAD REQUEST //Error 400 is thrown
< Access-Control-Allow-Headers: Content-Type, x-requested-with, Authorization
< Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS
< Access-Control-Allow-Origin: *
< Access-Control-Max-Age: 600
< Cache-Control: no-store
< Content-Type: application/json
< Date: Tue, 15 Dec 2020 20:17:46 GMT
< Server: nginx
< Content-Length: 86
< Connection: keep-alive
<
* Connection #0 to host api.mailgun.net left intact
{
  "message": "'from' parameter is not a valid address. please check documentation"
}

输出中抛出错误 400。

文档 cURL 示例

curl -s --user 'api:YOUR_API_KEY' \
    https://api.mailgun.net/v3/YOUR_DOMAIN_NAME/messages \
    -F from='Excited User <mailgun@YOUR_DOMAIN_NAME>' \
    -F to=YOU@YOUR_DOMAIN_NAME \
    -F to=bar@example.com \
    -F subject='Hello' \
    -F text='Testing some Mailgun awesomeness!'

【问题讨论】:

  • 您查阅过文档吗?
  • @Chipster 是的,我什么也没找到
  • 您是否尝试过仅使用from 中的电子邮件?
  • 另外,为什么你在 bob marley 周围有双引号?
  • 发件人字段中的真实电子邮件地址如何?

标签: c++ email libcurl mailgun


【解决方案1】:

您不需要在发件人地址周围加上引号,并且必须对 lt/gt 符号进行编码:

"from='Bob Marley <bob@host.com>'&to='MY_EMAIL@gmail.com'&subject='Hello'&text='hi'";

你应该使用

"from=Bob Marley %3Cbob@host.com%3E&to='MY_EMAIL@gmail.com'&subject='Hello'&text='hi'";

【讨论】:

  • 我得到了它与您的百分比编码提示一起使用,但是我认为您有一个错字。 %3D 应该是 %3E 制作完整的字符串 "from=Bob Marley %3Cbob@host.com%3E&amp;to='MY_EMAIL@gmail.com'&amp;subject='Hello'&amp;text='hi'";
  • 你是对的,这是一个错字。感谢您的澄清,我编辑了我的答案,所以现在它是正确的。
猜你喜欢
  • 1970-01-01
  • 2017-12-14
  • 2016-08-23
  • 1970-01-01
  • 2020-11-10
  • 1970-01-01
  • 2019-12-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多