Brandon
Brandon

Reputation: 23485

Libcurl won't send SMTP quit command

#include <curl/curl.h>
#include <stdio.h>
#include <stdlib.h>

#define server "smtps://smtp.gmail.com"
#define to "[email protected]"
#define from "[email protected]"
#define pwd "****"

typedef struct
{
    char** mem;
    size_t size;
} payload_data;

size_t reader(void *ptr, size_t size, size_t nmemb, void *userp)
{
    char* str = NULL;
    payload_data *data = (payload_data *)userp;

    if((size == 0) || (nmemb == 0) || ((size * nmemb) < 1))
        return 0;

    str = data->mem[data->size];

    if (str)
    {
        size_t len = strlen(str);
        memcpy(ptr, str, len);
        ++data->size;
        return len;
    }

    return 0;
}

void TestCurl()
{
    static const char *info[] =
    {
        "To: <"to">\r\n"
        "From: <"from">\r\n",
        "Subject: TestCurl\r\n",
        "\r\n",
        "Messaging with libcurl..\r\n",
        "\r\n",//"\r\n.\r\nQUIT\r\n",
        NULL
    };

    CURLcode res = CURLE_OK;
    payload_data data = {0};
    CURL* curl = curl_easy_init();
    struct curl_slist* recipients = NULL;

    if (curl)
    {
        data.mem = info;
        curl_easy_setopt(curl, CURLOPT_URL, server);
        curl_easy_setopt(curl, CURLOPT_USERNAME, from);
        curl_easy_setopt(curl, CURLOPT_PASSWORD, pwd);
        curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1L);
        curl_easy_setopt(curl, CURLOPT_MAIL_FROM, from);
        recipients = curl_slist_append(recipients, to);
        curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, recipients);
        curl_easy_setopt(curl, CURLOPT_READFUNCTION, reader);
        curl_easy_setopt(curl, CURLOPT_READDATA, &data);
        curl_easy_setopt(curl, CURLOPT_USE_SSL, CURLUSESSL_ALL);
        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 2L);
        curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
        curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L); //DOES NOT WORK WITHOUT THIS!
        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);
    }
}

and the result is:

* Rebuilt URL to: smtps://smtp.gmail.com/
* timeout on name lookup is not supported
* Hostname was NOT found in DNS cache
*   Trying 74.125.69.108...
* Connected to smtp.gmail.com (74.125.69.108) port 465 (#0)
* SSL connection using TLSv1.2 / ECDHE-RSA-AES128-GCM-SHA256
* Server certificate:
*        subject: C=US; ST=California; L=Mountain View; O=Google Inc; CN=smtp.gm
ail.com
*        start date: 2014-07-15 08:40:38 GMT
*        expire date: 2015-04-04 15:15:55 GMT
*        subjectAltName: smtp.gmail.com matched
*        issuer: C=US; O=Google Inc; CN=Google Internet Authority G2
*        SSL certificate verify result: unable to get local issuer certificate (
20), continuing anyway.
< 220 mx.google.com ESMTP 141sm5490110ioz.39 - gsmtp
> EHLO Kira
< 250-mx.google.com at your service, [my_ip_address]
< 250-SIZE 35882577
< 250-8BITMIME
< 250-AUTH LOGIN PLAIN XOAUTH XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER
< 250-ENHANCEDSTATUSCODES
< 250-PIPELINING
< 250-CHUNKING
< 250 SMTPUTF8
> AUTH LOGIN
< 334 VXNlcm5hbWU6
> encrypted_username
< 334 UGFzc3dvcmQ6
> encrypted_password
< 235 2.7.0 Accepted
> MAIL FROM:<[email protected]>
< 250 2.1.0 OK 141sm5490110ioz.39 - gsmtp
> RCPT TO:<[email protected]>
< 250 2.1.5 OK 141sm5490110ioz.39 - gsmtp
> DATA
< 354  Go ahead 141sm5490110ioz.39 - gsmtp
< 250 2.0.0 OK 1413861121 141sm5490110ioz.39 - gsmtp
* Connection #0 to host smtp.gmail.com left intact

Process returned 0 (0x0)   execution time : 1.524 s
Press any key to continue.

I can't get it to send QUIT. I tried adding QUIT to the "info" array but it just adds it as part of the message. I tried terminating with \r\n\r\n.\r\n but no cigar..

The email gets sent successfully.. I just can't get it to quit. Every time I send a new email, it increases the "Connection #X to host smtp.gmail.com left intact".. Even though I did the curl cleanup..

Any ideas how I can get it to quit and stop leaving the host intact? Is it leaking somewhere? Why does it do this?

Upvotes: 0

Views: 1100

Answers (1)

Daniel Stenberg
Daniel Stenberg

Reputation: 58114

First, it actually closes the connection when you close the easy handle - since that's where the connection cache is kept when using the easy interface. Depending on your libcurl version it may not send a QUIT and you may not see it in the verbose output (since the closing is done after the handle in which you set CURLOPT_VERBOSE to TRUE in is already gone).

The libcurl way to force a closure of a connection after the specific transfer is done, is to set CURLOPT_FORBID_REUSE. In a similar spirit you can set CURLOPT_FRESH_CONNECT to force using a new connection instead of re-using an old.

Upvotes: 1

Related Questions