1/*************************************************************************** 2 * _ _ ____ _ 3 * Project ___| | | | _ \| | 4 * / __| | | | |_) | | 5 * | (__| |_| | _ <| |___ 6 * \___|\___/|_| \_\_____| 7 * 8 * Copyright (C) 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 https://curl.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 * SPDX-License-Identifier: curl 22 * 23 ***************************************************************************/ 24 25/* <DESC> 26 * Send email with IMAP 27 * </DESC> 28 */ 29 30#include <stdio.h> 31#include <string.h> 32#include <curl/curl.h> 33 34/* This is a simple example showing how to send mail using libcurl's IMAP 35 * capabilities. 36 * 37 * Note that this example requires libcurl 7.30.0 or above. 38 */ 39 40#define FROM "<sender@example.org>" 41#define TO "<addressee@example.net>" 42#define CC "<info@example.org>" 43 44static const char *payload_text = 45 "Date: Mon, 29 Nov 2010 21:54:29 +1100\r\n" 46 "To: " TO "\r\n" 47 "From: " FROM "(Example User)\r\n" 48 "Cc: " CC "(Another example User)\r\n" 49 "Message-ID: " 50 "<dcd7cb36-11db-487a-9f3a-e652a9458efd@rfcpedant.example.org>\r\n" 51 "Subject: IMAP example message\r\n" 52 "\r\n" /* empty line to divide headers from body, see RFC 5322 */ 53 "The body of the message starts here.\r\n" 54 "\r\n" 55 "It could be a lot of lines, could be MIME encoded, whatever.\r\n" 56 "Check RFC 5322.\r\n"; 57 58struct upload_status { 59 size_t bytes_read; 60}; 61 62static size_t payload_source(char *ptr, size_t size, size_t nmemb, void *userp) 63{ 64 struct upload_status *upload_ctx = (struct upload_status *)userp; 65 const char *data; 66 size_t room = size * nmemb; 67 68 if((size == 0) || (nmemb == 0) || ((size*nmemb) < 1)) { 69 return 0; 70 } 71 72 data = &payload_text[upload_ctx->bytes_read]; 73 74 if(*data) { 75 size_t len = strlen(data); 76 if(room < len) 77 len = room; 78 memcpy(ptr, data, len); 79 upload_ctx->bytes_read += len; 80 81 return len; 82 } 83 84 return 0; 85} 86 87int main(void) 88{ 89 CURL *curl; 90 CURLcode res = CURLE_OK; 91 92 curl = curl_easy_init(); 93 if(curl) { 94 size_t filesize; 95 long infilesize = LONG_MAX; 96 struct upload_status upload_ctx = { 0 }; 97 98 /* Set username and password */ 99 curl_easy_setopt(curl, CURLOPT_USERNAME, "user"); 100 curl_easy_setopt(curl, CURLOPT_PASSWORD, "secret"); 101 102 /* This will create a new message in folder "Sent". */ 103 curl_easy_setopt(curl, CURLOPT_URL, "imap://imap.example.com/Sent"); 104 105 /* In this case, we are using a callback function to specify the data. You 106 * could just use the CURLOPT_READDATA option to specify a FILE pointer to 107 * read from. */ 108 curl_easy_setopt(curl, CURLOPT_READFUNCTION, payload_source); 109 curl_easy_setopt(curl, CURLOPT_READDATA, &upload_ctx); 110 curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L); 111 112 filesize = strlen(payload_text); 113 if(filesize <= LONG_MAX) 114 infilesize = (long)filesize; 115 curl_easy_setopt(curl, CURLOPT_INFILESIZE, infilesize); 116 117 /* Perform the append */ 118 res = curl_easy_perform(curl); 119 120 /* Check for errors */ 121 if(res != CURLE_OK) 122 fprintf(stderr, "curl_easy_perform() failed: %s\n", 123 curl_easy_strerror(res)); 124 125 /* Always cleanup */ 126 curl_easy_cleanup(curl); 127 } 128 129 return (int)res; 130} 131