Go to the documentation of this file.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 #define FROM "<sender@example.org>"
00040 #define TO "<addressee@example.net>"
00041 #define CC "<info@example.org>"
00042
00043 static const char *payload_text[] = {
00044 "Date: Mon, 29 Nov 2010 21:54:29 +1100\r\n",
00045 "To: " TO "\r\n",
00046 "From: " FROM "(Example User)\r\n",
00047 "Cc: " CC "(Another example User)\r\n",
00048 "Message-ID: <dcd7cb36-11db-487a-9f3a-e652a9458efd@"
00049 "rfcpedant.example.org>\r\n",
00050 "Subject: SMTP example message\r\n",
00051 "\r\n",
00052 "The body of the message starts here.\r\n",
00053 "\r\n",
00054 "It could be a lot of lines, could be MIME encoded, whatever.\r\n",
00055 "Check RFC5322.\r\n",
00056 NULL
00057 };
00058
00059 struct upload_status {
00060 int lines_read;
00061 };
00062
00063 static size_t payload_source(void *ptr, size_t size, size_t nmemb, void *userp)
00064 {
00065 struct upload_status *upload_ctx = (struct upload_status *)userp;
00066 const char *data;
00067
00068 if((size == 0) || (nmemb == 0) || ((size*nmemb) < 1)) {
00069 return 0;
00070 }
00071
00072 data = payload_text[upload_ctx->lines_read];
00073
00074 if(data) {
00075 size_t len = strlen(data);
00076 memcpy(ptr, data, len);
00077 upload_ctx->lines_read++;
00078
00079 return len;
00080 }
00081
00082 return 0;
00083 }
00084
00085 int main(void)
00086 {
00087 CURL *curl;
00088 CURLcode res = CURLE_OK;
00089 struct curl_slist *recipients = NULL;
00090 struct upload_status upload_ctx;
00091
00092 upload_ctx.lines_read = 0;
00093
00094 curl = curl_easy_init();
00095 if(curl) {
00096
00097 curl_easy_setopt(curl, CURLOPT_URL, "smtp://mail.example.com");
00098
00099
00100
00101
00102
00103
00104
00105
00106 curl_easy_setopt(curl, CURLOPT_MAIL_FROM, FROM);
00107
00108
00109
00110
00111 recipients = curl_slist_append(recipients, TO);
00112 recipients = curl_slist_append(recipients, CC);
00113 curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, recipients);
00114
00115
00116
00117
00118 curl_easy_setopt(curl, CURLOPT_READFUNCTION, payload_source);
00119 curl_easy_setopt(curl, CURLOPT_READDATA, &upload_ctx);
00120 curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
00121
00122
00123 res = curl_easy_perform(curl);
00124
00125
00126 if(res != CURLE_OK)
00127 fprintf(stderr, "curl_easy_perform() failed: %s\n",
00128 curl_easy_strerror(res));
00129
00130
00131 curl_slist_free_all(recipients);
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141 curl_easy_cleanup(curl);
00142 }
00143
00144 return (int)res;
00145 }