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 * Send e-mail with SMTP
25 * </DESC>
26 */
27
28 #include <stdio.h>
29 #include <string.h>
30 #include <curl/curl.h>
31
32 /*
33 * For an SMTP example using the multi interface please see smtp-multi.c.
34 */
35
36 /* The libcurl options want plain addresses, the viewable headers in the mail
37 * can very well get a full name as well.
38 */
39 #define FROM_ADDR "<sender@example.org>"
40 #define TO_ADDR "<addressee@example.net>"
41 #define CC_ADDR "<info@example.org>"
42
43 #define FROM_MAIL "Sender Person " FROM_ADDR
44 #define TO_MAIL "A Receiver " TO_ADDR
45 #define CC_MAIL "John CC Smith " CC_ADDR
46
47 static const char *payload_text =
48 "Date: Mon, 29 Nov 2010 21:54:29 +1100\r\n"
49 "To: " TO_MAIL "\r\n"
50 "From: " FROM_MAIL "\r\n"
51 "Cc: " CC_MAIL "\r\n"
52 "Message-ID: <dcd7cb36-11db-487a-9f3a-e652a9458efd@"
53 "rfcpedant.example.org>\r\n"
54 "Subject: SMTP example message\r\n"
55 "\r\n" /* empty line to divide headers from body, see RFC5322 */
56 "The body of the message starts here.\r\n"
57 "\r\n"
58 "It could be a lot of lines, could be MIME encoded, whatever.\r\n"
59 "Check RFC5322.\r\n";
60
61 struct upload_status {
62 size_t bytes_read;
63 };
64
payload_source(char * ptr,size_t size,size_t nmemb,void * userp)65 static size_t payload_source(char *ptr, size_t size, size_t nmemb, void *userp)
66 {
67 struct upload_status *upload_ctx = (struct upload_status *)userp;
68 const char *data;
69 size_t room = size * nmemb;
70
71 if((size == 0) || (nmemb == 0) || ((size*nmemb) < 1)) {
72 return 0;
73 }
74
75 data = &payload_text[upload_ctx->bytes_read];
76
77 if(data) {
78 size_t len = strlen(data);
79 if(room < len)
80 len = room;
81 memcpy(ptr, data, len);
82 upload_ctx->bytes_read += len;
83
84 return len;
85 }
86
87 return 0;
88 }
89
main(void)90 int main(void)
91 {
92 CURL *curl;
93 CURLcode res = CURLE_OK;
94 struct curl_slist *recipients = NULL;
95 struct upload_status upload_ctx = { 0 };
96
97 curl = curl_easy_init();
98 if(curl) {
99 /* This is the URL for your mailserver */
100 curl_easy_setopt(curl, CURLOPT_URL, "smtp://mail.example.com");
101
102 /* Note that this option isn't strictly required, omitting it will result
103 * in libcurl sending the MAIL FROM command with empty sender data. All
104 * autoresponses should have an empty reverse-path, and should be directed
105 * to the address in the reverse-path which triggered them. Otherwise,
106 * they could cause an endless loop. See RFC 5321 Section 4.5.5 for more
107 * details.
108 */
109 curl_easy_setopt(curl, CURLOPT_MAIL_FROM, FROM_ADDR);
110
111 /* Add two recipients, in this particular case they correspond to the
112 * To: and Cc: addressees in the header, but they could be any kind of
113 * recipient. */
114 recipients = curl_slist_append(recipients, TO_ADDR);
115 recipients = curl_slist_append(recipients, CC_ADDR);
116 curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, recipients);
117
118 /* We're using a callback function to specify the payload (the headers and
119 * body of the message). You could just use the CURLOPT_READDATA option to
120 * specify a FILE pointer to read from. */
121 curl_easy_setopt(curl, CURLOPT_READFUNCTION, payload_source);
122 curl_easy_setopt(curl, CURLOPT_READDATA, &upload_ctx);
123 curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
124
125 /* Send the message */
126 res = curl_easy_perform(curl);
127
128 /* Check for errors */
129 if(res != CURLE_OK)
130 fprintf(stderr, "curl_easy_perform() failed: %s\n",
131 curl_easy_strerror(res));
132
133 /* Free the list of recipients */
134 curl_slist_free_all(recipients);
135
136 /* curl won't send the QUIT command until you call cleanup, so you should
137 * be able to re-use this connection for additional messages (setting
138 * CURLOPT_MAIL_FROM and CURLOPT_MAIL_RCPT as required, and calling
139 * curl_easy_perform() again. It may not be a good idea to keep the
140 * connection open for a very long time though (more than a few minutes
141 * may result in the server timing out the connection), and you do want to
142 * clean up in the end.
143 */
144 curl_easy_cleanup(curl);
145 }
146
147 return (int)res;
148 }
149