00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #include <stdio.h>
00029 #include <string.h>
00030 #include <curl/curl.h>
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 #define FROM "<sender@example.org>"
00041 #define TO "<addressee@example.net>"
00042 #define CC "<info@example.org>"
00043
00044 static const char *payload_text[] = {
00045 "Date: Mon, 29 Nov 2010 21:54:29 +1100\r\n",
00046 "To: " TO "\r\n",
00047 "From: " FROM "(Example User)\r\n",
00048 "Cc: " CC "(Another example User)\r\n",
00049 "Message-ID: <dcd7cb36-11db-487a-9f3a-e652a9458efd@"
00050 "rfcpedant.example.org>\r\n",
00051 "Subject: SMTP SSL example message\r\n",
00052 "\r\n",
00053 "The body of the message starts here.\r\n",
00054 "\r\n",
00055 "It could be a lot of lines, could be MIME encoded, whatever.\r\n",
00056 "Check RFC5322.\r\n",
00057 NULL
00058 };
00059
00060 struct upload_status {
00061 int lines_read;
00062 };
00063
00064 static size_t payload_source(void *ptr, size_t size, size_t nmemb, void *userp)
00065 {
00066 struct upload_status *upload_ctx = (struct upload_status *)userp;
00067 const char *data;
00068
00069 if((size == 0) || (nmemb == 0) || ((size*nmemb) < 1)) {
00070 return 0;
00071 }
00072
00073 data = payload_text[upload_ctx->lines_read];
00074
00075 if(data) {
00076 size_t len = strlen(data);
00077 memcpy(ptr, data, len);
00078 upload_ctx->lines_read++;
00079
00080 return len;
00081 }
00082
00083 return 0;
00084 }
00085
00086 int main(void)
00087 {
00088 CURL *curl;
00089 CURLcode res = CURLE_OK;
00090 struct curl_slist *recipients = NULL;
00091 struct upload_status upload_ctx;
00092
00093 upload_ctx.lines_read = 0;
00094
00095 curl = curl_easy_init();
00096 if(curl) {
00097
00098 curl_easy_setopt(curl, CURLOPT_USERNAME, "user");
00099 curl_easy_setopt(curl, CURLOPT_PASSWORD, "secret");
00100
00101
00102
00103 curl_easy_setopt(curl, CURLOPT_URL, "smtps://mainserver.example.net");
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113 #ifdef SKIP_PEER_VERIFICATION
00114 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
00115 #endif
00116
00117
00118
00119
00120
00121 #ifdef SKIP_HOSTNAME_VERIFICATION
00122 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
00123 #endif
00124
00125
00126
00127
00128
00129
00130
00131
00132 curl_easy_setopt(curl, CURLOPT_MAIL_FROM, FROM);
00133
00134
00135
00136
00137 recipients = curl_slist_append(recipients, TO);
00138 recipients = curl_slist_append(recipients, CC);
00139 curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, recipients);
00140
00141
00142
00143
00144 curl_easy_setopt(curl, CURLOPT_READFUNCTION, payload_source);
00145 curl_easy_setopt(curl, CURLOPT_READDATA, &upload_ctx);
00146 curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
00147
00148
00149
00150
00151 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
00152
00153
00154 res = curl_easy_perform(curl);
00155
00156
00157 if(res != CURLE_OK)
00158 fprintf(stderr, "curl_easy_perform() failed: %s\n",
00159 curl_easy_strerror(res));
00160
00161
00162 curl_slist_free_all(recipients);
00163
00164
00165 curl_easy_cleanup(curl);
00166 }
00167
00168 return (int)res;
00169 }