LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Can't send file attachment with curl (https://www.linuxquestions.org/questions/linux-newbie-8/cant-send-file-attachment-with-curl-4175421473/)

patapout 08-10-2012 04:34 PM

Can't send file attachment with curl
 
Hi, I want to send a file in the seem directory of my program binary. When I receive the email, I can see a link to attachement myfile.txt but when click on it, I don't get the file content but the message body content instread. I am misssing something but don't know what it is.

Code:

curl_easy_setopt(hnd, CURLOPT_READFUNCTION, payload_source);
  curl_easy_setopt(hnd, CURLOPT_READDATA, &upload_ctx);
//...
static const char *payload_text[]={
    "To: <me@mail.com>\n",
    "From: <me@mail.com>(Example User)\n",
    "Content-Type: text/plain\n",
    "Content-Disposition: attachment; filename=\"myfile.txt\"\n",
    "Subject: Hello!\n",
    "\n",
    "Message sent\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;
}

Regards


All times are GMT -5. The time now is 04:10 AM.