'from' 参数不支持 c++ libcurl mailgun api 错误 400
'from' parameter not supported c++ libcurl mailgun api Error 400
我正在尝试使用 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!'
您不需要在发件人地址周围加上引号,您必须对 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'";
我正在尝试使用 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!'
您不需要在发件人地址周围加上引号,您必须对 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'";