在 libcurl 中遇到负载文本问题
Experiencing an issue with the payload text in libcurl
我的程序objective是将一个文本文件的内容自动发送到一个email地址。虽然发送工作完全正常,但我遇到的问题是我似乎无法找到一种方法来将字符串 "line" 存储到 payload_text 中,我在其中存储了文本文件的内容常量字符。
我是一名业余程序员,对 libcurl 还很陌生,但由于这是学校项目的一部分,我们将不胜感激。
我将避免提及我为解决此问题所做的尝试,因为回想起来,我所有可能的解决方案都是愚蠢的。您将在下面找到有问题的代码。
#include <stdio.h>
#include <string.h>
#include <curl.h>
#include <iostream>
#include <fstream>
using namespace std;
#define FROM "<example@gmail.com>"
#define TO "example@gmail.com>"
#define CC "<example@gmail.com>"
static const char *payload_text[] = {
"Date: Mon, 29 Nov 2010 21:54:29 +1100\r\n",
"To: " TO "\r\n",
"From: " FROM "(Example User)\r\n",
"Cc: " CC "(Another example User)\r\n",
"Message-ID: <dcd7cb36-11db-487a-9f3a-e652a9458efd@"
"rfcpedant.example.org>\r\n",
"Subject: SMTP TLS example message\r\n",
"\r\n", /* empty line to divide headers from body, see RFC5322 */
"The body of the message starts here.\r\n",
"\r\n",
"It could be a lot of lines, could be MIME encoded, whatever.\r\n",
"Check RFC5322.\r\n",
NULL
};
struct upload_status {
int lines_read;
};
static size_t payload_source(void *ptr, size_t size, size_t nmemb, void *userp)
{
struct upload_status *upload_ctx = (struct upload_status *)userp;
const char *data;
if((size == 0) || (nmemb == 0) || ((size*nmemb) < 1)) {
return 0;
}
data = payload_text[upload_ctx->lines_read];
if(data) {
size_t len = strlen(data);
memcpy(ptr, data, len);
upload_ctx->lines_read++;
return len;
}
return 0;
}
int main(void)
{
string line;
ifstream myfile ("log.txt");
if (myfile.is_open())
{
while ( getline (myfile,line) )
{
cout << line << '\n';
}
myfile.close();
}
const char * lchr = line.c_str();
CURL *curl;
CURLcode res = CURLE_OK;
struct curl_slist *recipients = NULL;
struct upload_status upload_ctx;
upload_ctx.lines_read = 0;
curl = curl_easy_init();
if(curl) {
/* Set username and password */
curl_easy_setopt(curl, CURLOPT_USERNAME, "example@gmail.com");
curl_easy_setopt(curl, CURLOPT_PASSWORD, "********");
curl_easy_setopt(curl, CURLOPT_URL, "smtp://smtp.gmail.com:587");
curl_easy_setopt(curl, CURLOPT_USE_SSL, (long)CURLUSESSL_ALL);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
curl_easy_setopt(curl, CURLOPT_MAIL_FROM, FROM);
recipients = curl_slist_append(recipients, TO);
recipients = curl_slist_append(recipients, CC);
curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, recipients);
curl_easy_setopt(curl, CURLOPT_READFUNCTION, payload_source);
curl_easy_setopt(curl, CURLOPT_READDATA, &upload_ctx);
curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
res = curl_easy_perform(curl);
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
curl_slist_free_all(recipients);
curl_easy_cleanup(curl);
}
return (int)res;
}
好的伙计们,因为没有人回应我自己想出来了。
如果有人遇到类似的问题,这里是解决方案。
为了访问 payload_text const char 数组中的行,我像这样直接访问数组:
payload_text[9] = line;
然而,由于 line 是一个字符串,它不能存储为 payload_text 数组的成员。为了解决这个问题,我简单地把它转换成了这样。
payload_text[9] = line.c_str();
抱歉我的解释很草率,正如我所说,我是业余爱好者,还在上学,希望这对某人有所帮助。
- 编辑 - 请记住数组中的最后一行必须始终为 NULL,否则程序将崩溃。
我的程序objective是将一个文本文件的内容自动发送到一个email地址。虽然发送工作完全正常,但我遇到的问题是我似乎无法找到一种方法来将字符串 "line" 存储到 payload_text 中,我在其中存储了文本文件的内容常量字符。 我是一名业余程序员,对 libcurl 还很陌生,但由于这是学校项目的一部分,我们将不胜感激。
我将避免提及我为解决此问题所做的尝试,因为回想起来,我所有可能的解决方案都是愚蠢的。您将在下面找到有问题的代码。
#include <stdio.h>
#include <string.h>
#include <curl.h>
#include <iostream>
#include <fstream>
using namespace std;
#define FROM "<example@gmail.com>"
#define TO "example@gmail.com>"
#define CC "<example@gmail.com>"
static const char *payload_text[] = {
"Date: Mon, 29 Nov 2010 21:54:29 +1100\r\n",
"To: " TO "\r\n",
"From: " FROM "(Example User)\r\n",
"Cc: " CC "(Another example User)\r\n",
"Message-ID: <dcd7cb36-11db-487a-9f3a-e652a9458efd@"
"rfcpedant.example.org>\r\n",
"Subject: SMTP TLS example message\r\n",
"\r\n", /* empty line to divide headers from body, see RFC5322 */
"The body of the message starts here.\r\n",
"\r\n",
"It could be a lot of lines, could be MIME encoded, whatever.\r\n",
"Check RFC5322.\r\n",
NULL
};
struct upload_status {
int lines_read;
};
static size_t payload_source(void *ptr, size_t size, size_t nmemb, void *userp)
{
struct upload_status *upload_ctx = (struct upload_status *)userp;
const char *data;
if((size == 0) || (nmemb == 0) || ((size*nmemb) < 1)) {
return 0;
}
data = payload_text[upload_ctx->lines_read];
if(data) {
size_t len = strlen(data);
memcpy(ptr, data, len);
upload_ctx->lines_read++;
return len;
}
return 0;
}
int main(void)
{
string line;
ifstream myfile ("log.txt");
if (myfile.is_open())
{
while ( getline (myfile,line) )
{
cout << line << '\n';
}
myfile.close();
}
const char * lchr = line.c_str();
CURL *curl;
CURLcode res = CURLE_OK;
struct curl_slist *recipients = NULL;
struct upload_status upload_ctx;
upload_ctx.lines_read = 0;
curl = curl_easy_init();
if(curl) {
/* Set username and password */
curl_easy_setopt(curl, CURLOPT_USERNAME, "example@gmail.com");
curl_easy_setopt(curl, CURLOPT_PASSWORD, "********");
curl_easy_setopt(curl, CURLOPT_URL, "smtp://smtp.gmail.com:587");
curl_easy_setopt(curl, CURLOPT_USE_SSL, (long)CURLUSESSL_ALL);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
curl_easy_setopt(curl, CURLOPT_MAIL_FROM, FROM);
recipients = curl_slist_append(recipients, TO);
recipients = curl_slist_append(recipients, CC);
curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, recipients);
curl_easy_setopt(curl, CURLOPT_READFUNCTION, payload_source);
curl_easy_setopt(curl, CURLOPT_READDATA, &upload_ctx);
curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
res = curl_easy_perform(curl);
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
curl_slist_free_all(recipients);
curl_easy_cleanup(curl);
}
return (int)res;
}
好的伙计们,因为没有人回应我自己想出来了。 如果有人遇到类似的问题,这里是解决方案。 为了访问 payload_text const char 数组中的行,我像这样直接访问数组:
payload_text[9] = line;
然而,由于 line 是一个字符串,它不能存储为 payload_text 数组的成员。为了解决这个问题,我简单地把它转换成了这样。
payload_text[9] = line.c_str();
抱歉我的解释很草率,正如我所说,我是业余爱好者,还在上学,希望这对某人有所帮助。
- 编辑 - 请记住数组中的最后一行必须始终为 NULL,否则程序将崩溃。