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