1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2021, 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 ***************************************************************************/
22
23 /* <DESC>
24 * SMTP example using the multi interface
25 * </DESC>
26 */
27
28 #include <string.h>
29 #include <curl/curl.h>
30
31 /* This is an example showing how to send mail using libcurl's SMTP
32 * capabilities. It builds on the smtp-mail.c example to demonstrate how to use
33 * libcurl's multi interface.
34 */
35
36 #define FROM_MAIL "<sender@example.com>"
37 #define TO_MAIL "<recipient@example.com>"
38 #define CC_MAIL "<info@example.com>"
39
40 static const char *payload_text =
41 "Date: Mon, 29 Nov 2010 21:54:29 +1100\r\n"
42 "To: " TO_MAIL "\r\n"
43 "From: " FROM_MAIL "\r\n"
44 "Cc: " CC_MAIL "\r\n"
45 "Message-ID: <dcd7cb36-11db-487a-9f3a-e652a9458efd@"
46 "rfcpedant.example.org>\r\n"
47 "Subject: SMTP example message\r\n"
48 "\r\n" /* empty line to divide headers from body, see RFC5322 */
49 "The body of the message starts here.\r\n"
50 "\r\n"
51 "It could be a lot of lines, could be MIME encoded, whatever.\r\n"
52 "Check RFC5322.\r\n";
53
54 struct upload_status {
55 size_t bytes_read;
56 };
57
payload_source(char * ptr,size_t size,size_t nmemb,void * userp)58 static size_t payload_source(char *ptr, size_t size, size_t nmemb, void *userp)
59 {
60 struct upload_status *upload_ctx = (struct upload_status *)userp;
61 const char *data;
62 size_t room = size * nmemb;
63
64 if((size == 0) || (nmemb == 0) || ((size*nmemb) < 1)) {
65 return 0;
66 }
67
68 data = &payload_text[upload_ctx->bytes_read];
69
70 if(data) {
71 size_t len = strlen(data);
72 if(room < len)
73 len = room;
74 memcpy(ptr, data, len);
75 upload_ctx->bytes_read += len;
76
77 return len;
78 }
79
80 return 0;
81 }
82
main(void)83 int main(void)
84 {
85 CURL *curl;
86 CURLM *mcurl;
87 int still_running = 1;
88 struct curl_slist *recipients = NULL;
89 struct upload_status upload_ctx = { 0 };
90
91 curl_global_init(CURL_GLOBAL_DEFAULT);
92
93 curl = curl_easy_init();
94 if(!curl)
95 return 1;
96
97 mcurl = curl_multi_init();
98 if(!mcurl)
99 return 2;
100
101 /* This is the URL for your mailserver */
102 curl_easy_setopt(curl, CURLOPT_URL, "smtp://mail.example.com");
103
104 /* Note that this option isn't strictly required, omitting it will result in
105 * libcurl sending the MAIL FROM command with empty sender data. All
106 * autoresponses should have an empty reverse-path, and should be directed
107 * to the address in the reverse-path which triggered them. Otherwise, they
108 * could cause an endless loop. See RFC 5321 Section 4.5.5 for more details.
109 */
110 curl_easy_setopt(curl, CURLOPT_MAIL_FROM, FROM_MAIL);
111
112 /* Add two recipients, in this particular case they correspond to the
113 * To: and Cc: addressees in the header, but they could be any kind of
114 * recipient. */
115 recipients = curl_slist_append(recipients, TO_MAIL);
116 recipients = curl_slist_append(recipients, CC_MAIL);
117 curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, recipients);
118
119 /* We're using a callback function to specify the payload (the headers and
120 * body of the message). You could just use the CURLOPT_READDATA option to
121 * specify a FILE pointer to read from. */
122 curl_easy_setopt(curl, CURLOPT_READFUNCTION, payload_source);
123 curl_easy_setopt(curl, CURLOPT_READDATA, &upload_ctx);
124 curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
125
126 /* Tell the multi stack about our easy handle */
127 curl_multi_add_handle(mcurl, curl);
128
129 do {
130 CURLMcode mc = curl_multi_perform(mcurl, &still_running);
131
132 if(still_running)
133 /* wait for activity, timeout or "nothing" */
134 mc = curl_multi_poll(mcurl, NULL, 0, 1000, NULL);
135
136 if(mc)
137 break;
138
139 } while(still_running);
140
141 /* Free the list of recipients */
142 curl_slist_free_all(recipients);
143
144 /* Always cleanup */
145 curl_multi_remove_handle(mcurl, curl);
146 curl_multi_cleanup(mcurl);
147 curl_easy_cleanup(curl);
148 curl_global_cleanup();
149
150 return 0;
151 }
152