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