Advertisement
opera

curl smtp

Oct 19th, 2011
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.50 KB | None | 0 0
  1. /***************************************************************************
  2.  *                                  _   _ ____  _
  3.  *  Project                     ___| | | |  _ \| |
  4.  *                             / __| | | | |_) | |
  5.  *                            | (__| |_| |  _ <| |___
  6.  *                             \___|\___/|_| \_\_____|
  7.  *
  8.  * Copyright (C) 1998 - 2011, Daniel Stenberg, <daniel@haxx.se>, et al.
  9.  *
  10.  * This software is licensed as described in the file COPYING, which
  11.  * you should have received as part of this distribution. The terms
  12.  * are also available at http://curl.haxx.se/docs/copyright.html.
  13.  *
  14.  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15.  * copies of the Software, and permit persons to whom the Software is
  16.  * furnished to do so, under the terms of the COPYING file.
  17.  *
  18.  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19.  * KIND, either express or implied.
  20.  *
  21.  ***************************************************************************/
  22.  
  23. #include <stddef.h>
  24. #include <stdlib.h>
  25. #include <curl/curl.h>
  26.  
  27. struct pair
  28. {
  29.     const char* content;
  30.     size_t size;
  31.     size_t written_bytes;
  32. };
  33.  
  34. struct data
  35. {
  36.   char trace_ascii;
  37. };
  38.  
  39. size_t smtp_read_callback(void *ptr,
  40.     size_t size,
  41.     size_t nmemb,
  42.     void *data);
  43.  
  44. void dump(const char *text,
  45.           FILE *stream, unsigned char *ptr, size_t size,
  46.           char nohex);
  47.  
  48. int my_trace(CURL *handle, curl_infotype type,
  49.              char *data, size_t size,
  50.              void *userp);
  51.  
  52. int main(void)
  53. {
  54.     const char* cacert_filepath = "";
  55.     int smtp_port = 25;
  56.     const char* smtp_host = "";
  57.     const char* smtp_login = "";
  58.     const char* smtp_password = "";
  59.     const char* sender_email = "";
  60.     const char* recipient = "";
  61.    
  62.     char *buffer = (char *)malloc((CURL_MAX_WRITE_SIZE + 3) * sizeof(char));
  63.     int line_length = 0;
  64.     int pos = 0;
  65.     while(pos != CURL_MAX_WRITE_SIZE)
  66.     {
  67.         if(line_length == 64)
  68.         {
  69.             buffer[pos] = '\r';
  70.             buffer[++pos] = '\n';
  71.             line_length = 0;
  72.         }
  73.         else
  74.         {
  75.             buffer[pos] = 'A';
  76.             ++line_length;
  77.         }
  78.         ++pos;
  79.     }
  80.     buffer[CURL_MAX_WRITE_SIZE - 1] = '\r';
  81.     buffer[CURL_MAX_WRITE_SIZE] = '\n';
  82.     buffer[CURL_MAX_WRITE_SIZE + 1] = 'B';
  83.     buffer[CURL_MAX_WRITE_SIZE + 2] = '\0';
  84.  
  85.     pair body;
  86.     body.content = buffer;
  87.     body.size = CURL_MAX_WRITE_SIZE + 2;
  88.     body.written_bytes = 0;
  89.  
  90.     data config;
  91.     config.trace_ascii = 1;
  92.  
  93.     curl_global_init(CURL_GLOBAL_ALL );
  94.     CURL* curl_ptr = curl_easy_init();
  95.     if(!curl_ptr)
  96.         return 1;
  97.  
  98.    
  99.     curl_easy_setopt(curl_ptr, CURLOPT_DEBUGFUNCTION, my_trace);
  100.     curl_easy_setopt(curl_ptr, CURLOPT_DEBUGDATA, &config);
  101.     curl_easy_setopt(curl_ptr, CURLOPT_VERBOSE, 1L);
  102.  
  103.     curl_easy_setopt(curl_ptr,CURLOPT_TIMEOUT,120);
  104.  
  105.     curl_easy_setopt(curl_ptr, CURLOPT_USE_SSL, CURLUSESSL_ALL);
  106.     curl_easy_setopt(curl_ptr, CURLOPT_SSL_VERIFYPEER,1);
  107.     curl_easy_setopt(curl_ptr, CURLOPT_SSL_VERIFYHOST, 2);
  108.     curl_easy_setopt(curl_ptr, CURLOPT_CAINFO, cacert_filepath);
  109.     curl_easy_setopt(curl_ptr, CURLOPT_SSLVERSION, CURL_SSLVERSION_DEFAULT);
  110.     curl_easy_setopt(curl_ptr, CURLOPT_SSL_SESSIONID_CACHE, 1);
  111.  
  112.     curl_easy_setopt(curl_ptr, CURLOPT_URL, smtp_host);
  113.     curl_easy_setopt(curl_ptr, CURLOPT_PORT, smtp_port);
  114.     curl_easy_setopt(curl_ptr, CURLOPT_USERNAME, smtp_login);
  115.     curl_easy_setopt(curl_ptr, CURLOPT_PASSWORD, smtp_password);
  116.     curl_easy_setopt(curl_ptr, CURLOPT_MAIL_FROM, sender_email);
  117.  
  118.     struct curl_slist* rcpt_list = NULL;
  119.     rcpt_list = curl_slist_append(rcpt_list, recipient);
  120.     curl_easy_setopt(curl_ptr, CURLOPT_MAIL_RCPT, rcpt_list);
  121.  
  122.     curl_easy_setopt(curl_ptr, CURLOPT_READFUNCTION, smtp_read_callback);
  123.     curl_easy_setopt(curl_ptr, CURLOPT_READDATA, &body);
  124.  
  125.     CURLcode error_code = curl_easy_perform(curl_ptr);
  126.     if(error_code != CURLE_OK)
  127.         return 1;
  128.  
  129.     curl_slist_free_all(rcpt_list);
  130.     curl_easy_cleanup(curl_ptr);
  131.     curl_global_cleanup();
  132.     free(buffer);
  133.     return 0;
  134. }
  135.  
  136. size_t smtp_read_callback(void *ptr,
  137.     size_t size,
  138.     size_t nmemb,
  139.     void *data)
  140. {
  141.     pair *body = (pair*)data;
  142.  
  143.     if (data)
  144.     {
  145.         int available = (body->size - body->written_bytes);
  146.         if (available > 0)
  147.         {
  148.             int written = min(size * nmemb,available);
  149.             memcpy((char *)ptr,
  150.                 body->content + body->written_bytes,
  151.                 written);
  152.             body->written_bytes += written;
  153.             return written;
  154.         }
  155.     }
  156.     else
  157.         return CURL_READFUNC_ABORT;
  158.     return 0;
  159. }
  160.  
  161.  
  162.  
  163. void dump(const char *text,
  164.           FILE *stream, unsigned char *ptr, size_t size,
  165.           char nohex)
  166. {
  167.   size_t i;
  168.   size_t c;
  169.  
  170.   unsigned int width=0x10;
  171.  
  172.   if(nohex)
  173.     /* without the hex output, we can fit more on screen */
  174.     width = 0x40;
  175.  
  176.   fprintf(stream, "%s, %10.10ld bytes (0x%8.8lx)\n",
  177.           text, (long)size, (long)size);
  178.  
  179.   for(i=0; i<size; i+= width) {
  180.  
  181.     fprintf(stream, "%4.4lx: ", (long)i);
  182.  
  183.     if(!nohex) {
  184.       /* hex not disabled, show it */
  185.       for(c = 0; c < width; c++)
  186.         if(i+c < size)
  187.           fprintf(stream, "%02x ", ptr[i+c]);
  188.         else
  189.           fputs("   ", stream);
  190.     }
  191.  
  192.     for(c = 0; (c < width) && (i+c < size); c++) {
  193.       /* check for 0D0A; if found, skip past and start a new line of output */
  194.       if (nohex && (i+c+1 < size) && ptr[i+c]==0x0D && ptr[i+c+1]==0x0A) {
  195.         i+=(c+2-width);
  196.         break;
  197.       }
  198.       fprintf(stream, "%c",
  199.               (ptr[i+c]>=0x20) && (ptr[i+c]<0x80)?ptr[i+c]:'.');
  200.       /* check again for 0D0A, to avoid an extra \n if it's at width */
  201.       if (nohex && (i+c+2 < size) && ptr[i+c+1]==0x0D && ptr[i+c+2]==0x0A) {
  202.         i+=(c+3-width);
  203.         break;
  204.       }
  205.     }
  206.     fputc('\n', stream); /* newline */
  207.   }
  208.   fflush(stream);
  209. }
  210.  
  211. int my_trace(CURL *handle, curl_infotype type,
  212.              char *data, size_t size,
  213.              void *userp)
  214. {
  215.   struct data *config = (struct data *)userp;
  216.   const char *text;
  217.   (void)handle; /* prevent compiler warning */
  218.  
  219.   switch (type) {
  220.   case CURLINFO_TEXT:
  221.     fprintf(stderr, "== Info: %s", data);
  222.   default: /* in case a new one is introduced to shock us */
  223.     return 0;
  224.  
  225.   case CURLINFO_HEADER_OUT:
  226.     text = "=> Send header";
  227.     break;
  228.   case CURLINFO_DATA_OUT:
  229.     text = "=> Send data";
  230.     break;
  231.   case CURLINFO_SSL_DATA_OUT:
  232.     text = "=> Send SSL data";
  233.     break;
  234.   case CURLINFO_HEADER_IN:
  235.     text = "<= Recv header";
  236.     break;
  237.   case CURLINFO_DATA_IN:
  238.     text = "<= Recv data";
  239.     break;
  240.   case CURLINFO_SSL_DATA_IN:
  241.     text = "<= Recv SSL data";
  242.     break;
  243.   }
  244.  
  245.   dump(text, stderr, (unsigned char *)data, size, config->trace_ascii);
  246.   return 0;
  247. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement