Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /***************************************************************************
- * _ _ ____ _
- * Project ___| | | | _ \| |
- * / __| | | | |_) | |
- * | (__| |_| | _ <| |___
- * \___|\___/|_| \_\_____|
- *
- * Copyright (C) 1998 - 2011, Daniel Stenberg, <daniel@haxx.se>, et al.
- *
- * This software is licensed as described in the file COPYING, which
- * you should have received as part of this distribution. The terms
- * are also available at http://curl.haxx.se/docs/copyright.html.
- *
- * You may opt to use, copy, modify, merge, publish, distribute and/or sell
- * copies of the Software, and permit persons to whom the Software is
- * furnished to do so, under the terms of the COPYING file.
- *
- * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
- * KIND, either express or implied.
- *
- ***************************************************************************/
- #include <stddef.h>
- #include <stdlib.h>
- #include <curl/curl.h>
- struct pair
- {
- const char* content;
- size_t size;
- size_t written_bytes;
- };
- struct data
- {
- char trace_ascii;
- };
- size_t smtp_read_callback(void *ptr,
- size_t size,
- size_t nmemb,
- void *data);
- void dump(const char *text,
- FILE *stream, unsigned char *ptr, size_t size,
- char nohex);
- int my_trace(CURL *handle, curl_infotype type,
- char *data, size_t size,
- void *userp);
- int main(void)
- {
- const char* cacert_filepath = "";
- int smtp_port = 25;
- const char* smtp_host = "";
- const char* smtp_login = "";
- const char* smtp_password = "";
- const char* sender_email = "";
- const char* recipient = "";
- char *buffer = (char *)malloc((CURL_MAX_WRITE_SIZE + 3) * sizeof(char));
- int line_length = 0;
- int pos = 0;
- while(pos != CURL_MAX_WRITE_SIZE)
- {
- if(line_length == 64)
- {
- buffer[pos] = '\r';
- buffer[++pos] = '\n';
- line_length = 0;
- }
- else
- {
- buffer[pos] = 'A';
- ++line_length;
- }
- ++pos;
- }
- buffer[CURL_MAX_WRITE_SIZE - 1] = '\r';
- buffer[CURL_MAX_WRITE_SIZE] = '\n';
- buffer[CURL_MAX_WRITE_SIZE + 1] = 'B';
- buffer[CURL_MAX_WRITE_SIZE + 2] = '\0';
- pair body;
- body.content = buffer;
- body.size = CURL_MAX_WRITE_SIZE + 2;
- body.written_bytes = 0;
- data config;
- config.trace_ascii = 1;
- curl_global_init(CURL_GLOBAL_ALL );
- CURL* curl_ptr = curl_easy_init();
- if(!curl_ptr)
- return 1;
- curl_easy_setopt(curl_ptr, CURLOPT_DEBUGFUNCTION, my_trace);
- curl_easy_setopt(curl_ptr, CURLOPT_DEBUGDATA, &config);
- curl_easy_setopt(curl_ptr, CURLOPT_VERBOSE, 1L);
- curl_easy_setopt(curl_ptr,CURLOPT_TIMEOUT,120);
- curl_easy_setopt(curl_ptr, CURLOPT_USE_SSL, CURLUSESSL_ALL);
- curl_easy_setopt(curl_ptr, CURLOPT_SSL_VERIFYPEER,1);
- curl_easy_setopt(curl_ptr, CURLOPT_SSL_VERIFYHOST, 2);
- curl_easy_setopt(curl_ptr, CURLOPT_CAINFO, cacert_filepath);
- curl_easy_setopt(curl_ptr, CURLOPT_SSLVERSION, CURL_SSLVERSION_DEFAULT);
- curl_easy_setopt(curl_ptr, CURLOPT_SSL_SESSIONID_CACHE, 1);
- curl_easy_setopt(curl_ptr, CURLOPT_URL, smtp_host);
- curl_easy_setopt(curl_ptr, CURLOPT_PORT, smtp_port);
- curl_easy_setopt(curl_ptr, CURLOPT_USERNAME, smtp_login);
- curl_easy_setopt(curl_ptr, CURLOPT_PASSWORD, smtp_password);
- curl_easy_setopt(curl_ptr, CURLOPT_MAIL_FROM, sender_email);
- struct curl_slist* rcpt_list = NULL;
- rcpt_list = curl_slist_append(rcpt_list, recipient);
- curl_easy_setopt(curl_ptr, CURLOPT_MAIL_RCPT, rcpt_list);
- curl_easy_setopt(curl_ptr, CURLOPT_READFUNCTION, smtp_read_callback);
- curl_easy_setopt(curl_ptr, CURLOPT_READDATA, &body);
- CURLcode error_code = curl_easy_perform(curl_ptr);
- if(error_code != CURLE_OK)
- return 1;
- curl_slist_free_all(rcpt_list);
- curl_easy_cleanup(curl_ptr);
- curl_global_cleanup();
- free(buffer);
- return 0;
- }
- size_t smtp_read_callback(void *ptr,
- size_t size,
- size_t nmemb,
- void *data)
- {
- pair *body = (pair*)data;
- if (data)
- {
- int available = (body->size - body->written_bytes);
- if (available > 0)
- {
- int written = min(size * nmemb,available);
- memcpy((char *)ptr,
- body->content + body->written_bytes,
- written);
- body->written_bytes += written;
- return written;
- }
- }
- else
- return CURL_READFUNC_ABORT;
- return 0;
- }
- void dump(const char *text,
- FILE *stream, unsigned char *ptr, size_t size,
- char nohex)
- {
- size_t i;
- size_t c;
- unsigned int width=0x10;
- if(nohex)
- /* without the hex output, we can fit more on screen */
- width = 0x40;
- fprintf(stream, "%s, %10.10ld bytes (0x%8.8lx)\n",
- text, (long)size, (long)size);
- for(i=0; i<size; i+= width) {
- fprintf(stream, "%4.4lx: ", (long)i);
- if(!nohex) {
- /* hex not disabled, show it */
- for(c = 0; c < width; c++)
- if(i+c < size)
- fprintf(stream, "%02x ", ptr[i+c]);
- else
- fputs(" ", stream);
- }
- for(c = 0; (c < width) && (i+c < size); c++) {
- /* check for 0D0A; if found, skip past and start a new line of output */
- if (nohex && (i+c+1 < size) && ptr[i+c]==0x0D && ptr[i+c+1]==0x0A) {
- i+=(c+2-width);
- break;
- }
- fprintf(stream, "%c",
- (ptr[i+c]>=0x20) && (ptr[i+c]<0x80)?ptr[i+c]:'.');
- /* check again for 0D0A, to avoid an extra \n if it's at width */
- if (nohex && (i+c+2 < size) && ptr[i+c+1]==0x0D && ptr[i+c+2]==0x0A) {
- i+=(c+3-width);
- break;
- }
- }
- fputc('\n', stream); /* newline */
- }
- fflush(stream);
- }
- int my_trace(CURL *handle, curl_infotype type,
- char *data, size_t size,
- void *userp)
- {
- struct data *config = (struct data *)userp;
- const char *text;
- (void)handle; /* prevent compiler warning */
- switch (type) {
- case CURLINFO_TEXT:
- fprintf(stderr, "== Info: %s", data);
- default: /* in case a new one is introduced to shock us */
- return 0;
- case CURLINFO_HEADER_OUT:
- text = "=> Send header";
- break;
- case CURLINFO_DATA_OUT:
- text = "=> Send data";
- break;
- case CURLINFO_SSL_DATA_OUT:
- text = "=> Send SSL data";
- break;
- case CURLINFO_HEADER_IN:
- text = "<= Recv header";
- break;
- case CURLINFO_DATA_IN:
- text = "<= Recv data";
- break;
- case CURLINFO_SSL_DATA_IN:
- text = "<= Recv SSL data";
- break;
- }
- dump(text, stderr, (unsigned char *)data, size, config->trace_ascii);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement