cURL / Mailing Lists / curl-library / Single Mail

curl-library

Need help using CURL to send an email

From: Greg Stevens via curl-library <curl-library_at_cool.haxx.se>
Date: Wed, 31 Aug 2016 17:29:48 -0700

Hi,

I am following the sample code at
https://curl.haxx.se/libcurl/c/smtp-ssl.html to try to send an email
from a C program that I am writing, but it is not working. I am using
the same curl calls to do other stuff in the same program (HTTP POST,
etc.) and they work just fine. Here is the DEBUG info:

* STATE: INIT => CONNECT handle 0x8006bcc0; line 1402 (connection #-5000)
* Rebuilt URL to: smtp://smtp.gmail.com:465/
* Added connection 0. The cache now contains 1 members
[New Thread 7524.0x3cb4]
* Trying 74.125.25.109...
* STATE: CONNECT => WAITCONNECT handle 0x8006bcc0; line 1455 (connection #0)
* Connected to smtp.gmail.com (74.125.25.109) port 465 (#0)
* STATE: WAITCONNECT => SENDPROTOCONNECT handle 0x8006bcc0; line 1562
(connection #0)
* SMTP 0x8005cd88 state change from STOP to SERVERGREET
* STATE: SENDPROTOCONNECT => PROTOCONNECT handle 0x8006bcc0; line 1576
(connection #0)
* response reading failed
* multi_done
* Closing connection 0
* The cache now contains 0 members
curl_easy_perform() failed: Failure when receiving data from the peer

My source code is below.

Any advise?

Thanks everyone!

--
Greg
#define EMAIL "gstev46412_at_gmail.com"
static const char *payload_text_on[] = {
   "Date: Mon, 29 Nov 2010 21:54:29 +1100\r\n",
   "To: " EMAIL "\r\n",
   "From: " EMAIL "(Greg Stevens)\r\n",
   "Message-ID: <dcd7cb36-11db-487a-9f3a-e652a9458efd@"
   "rfcpedant.example.org>\r\n",
   "Subject: EV Charger Turned On\r\n",
   "\r\n", /* empty line to divide headers from body, see RFC5322 */
   "Turned the car charger on as the solar panels are generating more 
than the house usage plus the car charger usage.\r\n",
   "\r\n",
   NULL
};
struct upload_status {
   int lines_read;
};
static size_t payload_source_on(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_on[upload_ctx->lines_read];
   if(data) {
     size_t len = strlen(data);
     memcpy(ptr, data, len);
     upload_ctx->lines_read++;
     return len;
   }
   return 0;
}
void sendmail(int event) {
   CURL *curl;
   CURLcode res = CURLE_OK;
   struct curl_slist *recipients = NULL;
   struct upload_status upload_ctx;
   upload_ctx.lines_read = 0;
   curl_global_init(CURL_GLOBAL_ALL);
   curl = curl_easy_init();
   if (curl) {
     /* Set the URL for the gmail mailserver */
     curl_easy_setopt(curl, CURLOPT_URL, "smtp://smtp.gmail.com:465");
     /* Set username and password */
     curl_easy_setopt(curl, CURLOPT_USERNAME, EMAIL);
     curl_easy_setopt(curl, CURLOPT_PASSWORD, "foobar");
     /* Set from address */
     curl_easy_setopt(curl, CURLOPT_MAIL_FROM, EMAIL);
     /* Set to use SSL */
     curl_easy_setopt(curl, CURLOPT_USE_SSL, (long)CURLUSESSL_ALL);
     /* Set the recipient(s) */
     recipients = curl_slist_append(recipients, EMAIL);
     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). */
     curl_easy_setopt(curl, CURLOPT_READFUNCTION, payload_source_on);
     curl_easy_setopt(curl, CURLOPT_READDATA, &upload_ctx);
     curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
    if (DEBUG) {
       curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
     } else {
       curl_easy_setopt(curl, CURLOPT_VERBOSE, 0);
     }
     /* Send the message */
     res = curl_easy_perform(curl);
     /* Check for errors */
     if(res != CURLE_OK)
       fprintf(stderr, "curl_easy_perform() failed: %s\n",
               curl_easy_strerror(res));
     /* Free the list of recipient(s) */
     curl_slist_free_all(recipients);
     /* Clean up */
     curl_easy_cleanup(curl);
     curl_global_cleanup();
   }
}
--
Greg

-------------------------------------------------------------------
List admin: https://cool.haxx.se/list/listinfo/curl-library
Etiquette: https://curl.haxx.se/mail/etiquette.html
Received on 2016-09-01