c++gmaillibcurlconst-char

Experiencing an issue with the payload text in libcurl


The objective of my program is to send the contents of a text file automatically to an email address. While the sending works perfectly fine, the issue I am having is that I cannot seem to find a way to get the string "line", in which I have stored the contents of the text file, into the payload_text constant char. I am an amateur programmer, and am very new to libcurl, but since this is part of a school project, any help would be greatly appreciated.

I will refrain from mentioning what I have attempted to do to solve this issue, since, in retrospect, all my potential solutions were dumb. You will find the code in question below.

#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;
}

Solution

  • Alright guys since no-one responded I figured it out on my own. If anyone experiences a similar issue, here is the solution. In order to access lines within the payload_text const char array, I accessed the array directly like so:

    payload_text[9] = line;
    

    However, as line is a string, it cannot be stored as a member of the payload_text array. To solve this, I simply converted it as such.

    payload_text[9] = line.c_str();
    

    Sorry for the sloppy explanation, as I said, Im an amateur and still at school, hope this helps someone.