Using libcurl to send a gmail message

4,157 views
Skip to first unread message

Keith Patella

unread,
Feb 5, 2013, 2:55:06 PM2/5/13
to curlpp...@googlegroups.com
Hi,
  I'm able to send a gmail message using the following curl statement.

curl smtps://smtp.gmail.com:465 -v --mail-from "xxx...@gmail.com" --mail -rcpt "xxx...@gmail.com" --ssl -u xxx...@gmail.com:xxxxxxx -T "test.txt" -k  --anyauth

I'm trying to mimic this in C++ using libcurl using an example I found online but I receive a CURLE_SSL_CACERT error response in curl_easy_perform. 

I'm also pretty sure we are building libcurl with SSL, but I'm not sure exactly how to verify that.

#define FROM    "<xxx...@gmail.com>"
#define TO      "<xxx...@gmail.com>"
#define CC      "<xxx...@gmail.com>"

static const char *payload_text[]={
"Date: Mon, 29 Nov 2010 21:54:29 +1100\n",
"To: " TO "\n",
"From: " FROM "(Example User)\n",
"Cc: " CC "(Another example User)\n",
"Subject: SMTP TLS example message\n",
"\n", /* empty line to divide headers from body, see RFC5322 */ 
"The body of the message starts here.\n",
"\n",
"It could be a lot of lines, could be MIME encoded, whatever.\n",
"Check RFC5322.\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;
}


bool SendMessage() 
{
struct curl_slist *recipients = NULL;
struct upload_status upload_ctx;

upload_ctx.lines_read = 0;

//curl smtps://smtp.gmail.com:465 -v --mail-from "xxx...@gmail.com" --mail -rcpt "xxx...@gmail.com" --ssl -u xxx...@gmail.com:xxxxxxx -T "test.txt" -k  --anyauth
CURL* handle = ::curl_easy_init( ); 

curl_easy_setopt(handle, CURLOPT_URL, "smtps://smtp.gmail.com:465");
curl_easy_setopt(handle, CURLOPT_USE_SSL, CURLUSESSL_ALL);
curl_easy_setopt(handle, CURLOPT_USERNAME, "xxx...@gmail.com");
curl_easy_setopt(handle, CURLOPT_PASSWORD, "xxxxxxx");
curl_easy_setopt(handle, CURLOPT_MAIL_FROM, FROM);
recipients = curl_slist_append(recipients, TO);
recipients = curl_slist_append(recipients, CC);
curl_easy_setopt(handle, CURLOPT_MAIL_RCPT, recipients);
curl_easy_setopt(handle, CURLOPT_READFUNCTION, payload_source);
curl_easy_setopt(handle, CURLOPT_READDATA, &upload_ctx);
curl_easy_setopt(handle, CURLOPT_VERBOSE, 1);

CURLcode code = ::curl_easy_perform(handle);

curl_slist_free_all(recipients);
curl_easy_cleanup(handle);
}

Thanks for any help.

Keith

Daniel Stenberg

unread,
Feb 6, 2013, 7:30:26 AM2/6/13
to curlpp...@googlegroups.com
On Tue, 5 Feb 2013, Keith Patella wrote:

> curl smtps://smtp.gmail.com:465 -v --mail-from "xxx...@gmail.com" --mail
> -rcpt "xxx...@gmail.com" --ssl -u xxx...@gmail.com:xxxxxxx -T "test.txt" -k
> --anyauth
>
> I'm trying to mimic this in C++ using libcurl using an example I found
> online but I receive a CURLE_SSL_CACERT error response in curl_easy_perform.

I didn't see any code doing the libcurl version of -k ? Try using "--libcurl
testcode.c" on your command line to get a decent start...

And for asking about libcurl, curl-library is really *the* place to get
help...

--

/ daniel.haxx.se

Daniel

unread,
Feb 14, 2013, 2:04:22 PM2/14/13
to curlpp...@googlegroups.com
Hi,

I am trying Google Voice C++ API: https://github.com/mastermind202/GoogleVoice, using libcurl on Linux.
when trying login, I got error message: "Your browser's cookie functionality is turned off. Please turn
it on." I have compared with wget on same box, the cookie it sent out is not right. Can anybody help? I
can provide more debug information if needed.

Thanks,

Daniel
Reply all
Reply to author
Forward
0 new messages