So you have adopted the example from
libcurl example - smtp-mail.c[
^] and modified it.
One of your modifications is related to the mail content which is created by the callback function. So the problem is obviously located there. A candidate would be the
lines_read
variable. But you did not show the definition of that variable and where it is initialised with which value.
Callback functions must be static and support therefore usually an pointer argument to allow access to non-static parts of your application. And so it is here. But you did not use this mechanism (removed the corresponding handling provided by the example code). So setting the
CURLOPT_READDATA
option is useless in your code and also wrong (the argument must be a pointer but you are passing
lines_read
which is probably an
int
).
With C++ classes, usually a pointer to the class intance is passed to the callback function. Then you have access to the class members from within the static callback function. Adopting the example code for your case might be done in this way:
class Process
{
protected:
int lines_read;
static size_t processmail(void *ptr, size_t size, size_t nmemb, void *userp);
};
size_t Process::processmail(void *ptr, size_t size, size_t nmemb, void *userp)
{
Process *self = reinterpret_cast<Process*>(userp);
const char *data = payload_text[self->lines_read];
size_t len = data ? strlen(data) : 0;
if (len > nmemb * size)
len = 0;
if (len)
{
memcpy(ptr, data, len);
self->lines_read++;
}
return len;
}
void Process::sendmail(std::string mail)
{
lines_read = 0;
curl_easy_setopt(curl, CURLOPT_READDATA, this);
}
[EDIT]
Answering below comment and extending solution about an additional bug I did not recognised initially.
The line
std::string &statistics = *(static_cast<std::string*>(userp));
is not working with the above solution and wasn't working with the initial code. The casting must be according to the type passed with
CURLOPT_READDATA
.
When using my solution passing the class pointer it should be something like:
std::string &statistics = self->statistics;
when
statistics
is a
Process
class member of type
string
.
[/EDIT]