通过smtp发送电子邮件时libcurl内存错误

时间:2014-04-22 09:21:02

标签: c email curl smtp libcurl

我遇到了一个奇怪的问题。

我开发了一个与huawei usb stick交互的库。在这个库中,我想从棒中读出一些信息,并通过电子邮件发送这些信息。为此,我使用C的curl库。

我已经开发了我的程序,而不是MAX OS X.9,它在那里运行得很好。

但我想在Rasberry pi上使用我的代码,使用基于debian的操作系统(raspian)。

但是这里我在执行程序时遇到内存错误:

Start sending message...
* About to connect() to XXXXXXX.kasserver.com port 587 (#0)
*   Trying XX.XX.XX.XX...
* connected
* Connected to XXXXXXX.kasserver.com (XX.XX.XX.XX) port 587 (#0)
* SMTP 0x1ec1d88 state change from STOP to SERVERGREET
< 220 dd7300.kasserver.com ESMTP
> EHLO XXX
* SMTP 0x1ec1d88 state change from SERVERGREET to EHLO
< 250-XXX.kasserver.com
< 250-PIPELINING
< 250-SIZE 102400000
< 250-VRFY
< 250-ETRN
< 250-STARTTLS
< 250-AUTH PLAIN LOGIN
< 250-AUTH=PLAIN LOGIN
< 250-ENHANCEDSTATUSCODES
< 250-8BITMIME
< 250 DSN
> STARTTLS
* SMTP 0x1ec1d88 state change from EHLO to STARTTLS
< 220 2.0.0 Ready to start TLS
*** glibc detected *** ./test2: free(): invalid next size (fast): 0x01ec5db8 ***
Aborted

最奇怪的是,我可以编译并运行curl网站上的示例代码而不会出现任何问题:

curl smtp-tsl example

我的代码与示例没什么不同。对于调试,我使用了示例字符串数组以及相同的配置。我目前没有使用通过函数参数传递的电子邮件文本:

char *payload_text[] = {
  "Date: Mon, 29 Nov 2010 21:54:29 +1100\r\n",
  "To: " mail_to "\r\n",
  "From: " mail_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 SSL 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
};

char **payLoadSMTP  = NULL;

struct upload_status {
    int lines_read;
};

/**
 *  helper function for curl email send process
 *
 *  @param ptr   pointer to target structure for curl
 *  @param size  size of source
 *  @param nmemb not quite clear
 *  @param userp pointer to source
 *
 *  @return size_t length of data
 */
/*******************************************************/
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;
    printf("size: %i, nmemb: %i \n", size, nmemb);
    if((size == 0) || (nmemb == 0) || ((size*nmemb) < 1)) {
        return 0;
    }

    data = payload_text[upload_ctx->lines_read];
    //data = payLoadSMTP[upload_ctx->lines_read];

    if(data) {
        size_t len = strlen(data);
        memcpy(ptr, data, len);
        printf("data[%i]: %s\n", upload_ctx->lines_read, data);
        upload_ctx->lines_read++;

        return len;

    }

    return 0;
}

/**
 *  send email with before created payload char array
 *
 *  @param payloadText array over strings
 *
 *  @return true, if playload was correct; falso if payload was empty
 */
/*******************************************************/
bool sendEmail(char **payloadText)
/*******************************************************/
{

    CURL *curl;
    CURLcode res = CURLE_OK;
    struct curl_slist *recipients = NULL;
    struct upload_status upload_ctx;

    upload_ctx.lines_read = 0;

    payLoadSMTP = payloadText;


    if(payLoadSMTP == NULL)
    {
        return false;
    }

    curl = curl_easy_init();
    if(curl) {
        /* Set username and password */
        curl_easy_setopt(curl, CURLOPT_USERNAME, mail_user);
        curl_easy_setopt(curl, CURLOPT_PASSWORD, mail_pwd);

        /* This is the URL for your mailserver. Note the use of port 587 here,
         * instead of the normal SMTP port (25). Port 587 is commonly used for
         * secure mail submission (see RFC4403), but you should use whatever
         * matches your server configuration. */
        curl_easy_setopt(curl, CURLOPT_URL, mail_url);

        /* In this example, we'll start with a plain text connection, and upgrade
         * to Transport Layer Security (TLS) using the STARTTLS command. Be careful
         * of using CURLUSESSL_TRY here, because if TLS upgrade fails, the transfer
         * will continue anyway - see the security discussion in the libcurl
         * tutorial for more details. */
        curl_easy_setopt(curl, CURLOPT_USE_SSL, (long)CURLUSESSL_ALL);

        /* If your server doesn't have a valid certificate, then you can disable
         * part of the Transport Layer Security protection by setting the
         * CURLOPT_SSL_VERIFYPEER and CURLOPT_SSL_VERIFYHOST options to 0 (false).
         *   curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
         *   curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
         * That is, in general, a bad idea. It is still better than sending your
         * authentication details in plain text though.
         * Instead, you should get the issuer certificate (or the host certificate
         * if the certificate is self-signed) and add it to the set of certificates
         * that are known to libcurl using CURLOPT_CAINFO and/or CURLOPT_CAPATH. See
         * docs/SSLCERTS for more information. */


        //curl_easy_setopt(curl, CURLOPT_CAINFO, "/etc/ssl/certs/ca-certificates.crt");
        //curl_easy_setopt(curl, CURLOPT_CAPATH, "/etc/ssl/certs/");

        /* Note that this option isn't strictly required, omitting it will result in
         * libcurl sending the MAIL FROM command with empty sender data. All
         * autoresponses should have an empty reverse-path, and should be directed
         * to the address in the reverse-path which triggered them. Otherwise, they
         * could cause an endless loop. See RFC 5321 Section 4.5.5 for more details.
         */
        curl_easy_setopt(curl, CURLOPT_MAIL_FROM, mail_from);

        /* Add two recipients, in this particular case they correspond to the
         * To: and Cc: addressees in the header, but they could be any kind of
         * recipient. */
         recipients = curl_slist_append(recipients, mail_to);
        //recipients = curl_slist_append(recipients, CC);
        curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, recipients);

         /* We're using a callback function to specify the payload (the headers and
         * body of the message). You could just use the CURLOPT_READDATA option to
         * specify a FILE pointer to read from. */
        curl_easy_setopt(curl, CURLOPT_READFUNCTION, payload_source);
        curl_easy_setopt(curl, CURLOPT_READDATA, &upload_ctx);
        curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);

        /* Since the traffic will be encrypted, it is very useful to turn on debug
         * information within libcurl to see what is happening during the transfer.
         */
        curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);

        printf("\nStart sending message...\n");
        /* Send the message */
        res = curl_easy_perform(curl);

        printf("...Finished sending message.\n");

        /* Check for errors */
        if(res != CURLE_OK)
            fprintf(stderr, "curl_easy_perform() failed: %s\n",
                    curl_easy_strerror(res));

        printf("recipients cleanup\n");
        /* Free the list of recipients */
        curl_slist_free_all(recipients);

        printf("curl cleanup\n");
        /* Always cleanup */
        curl_easy_cleanup(curl);



        printf("cleanup finished\n");
        payLoadSMTP = NULL;

        return true;
    }


    return false;

}

用户名,密码,网址等参数定义了#define常量。

Doe的任何人都对我的问题有所了解?

0 个答案:

没有答案