1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2011, Daniel Stenberg, <daniel@haxx.se>, et al.
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.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
24 #include <curl/curl.h>
30 struct curl_slist *recipients = NULL;
32 /* value for envelope reverse-path */
33 static const char *from = "<bradh@example.com>";
35 /* this becomes the envelope forward-path */
36 static const char *to = "<bradh@example.net>";
38 curl = curl_easy_init();
40 /* this is the URL for your mailserver - you can also use an smtps:// URL
42 curl_easy_setopt(curl, CURLOPT_URL, "smtp://mail.example.net.");
44 /* Note that this option isn't strictly required, omitting it will result in
45 * libcurl will sent the MAIL FROM command with no sender data. All
46 * autoresponses should have an empty reverse-path, and should be directed
47 * to the address in the reverse-path which triggered them. Otherwise, they
48 * could cause an endless loop. See RFC 5321 Section 4.5.5 for more details.
50 curl_easy_setopt(curl, CURLOPT_MAIL_FROM, from);
52 /* Note that the CURLOPT_MAIL_RCPT takes a list, not a char array. */
53 recipients = curl_slist_append(recipients, to);
54 curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, recipients);
56 /* You provide the payload (headers and the body of the message) as the
57 * "data" element. There are two choices, either:
58 * - provide a callback function and specify the function name using the
59 * CURLOPT_READFUNCTION option; or
60 * - just provide a FILE pointer that can be used to read the data from.
61 * The easiest case is just to read from standard input, (which is available
62 * as a FILE pointer) as shown here.
64 curl_easy_setopt(curl, CURLOPT_READDATA, stdin);
66 /* send the message (including headers) */
67 res = curl_easy_perform(curl);
68 /* Check for errors */
70 fprintf(stderr, "curl_easy_perform() failed: %s\n",
71 curl_easy_strerror(res));
73 /* free the list of recipients */
74 curl_slist_free_all(recipients);
76 /* curl won't send the QUIT command until you call cleanup, so you should be
77 * able to re-use this connection for additional messages (setting
78 * CURLOPT_MAIL_FROM and CURLOPT_MAIL_RCPT as required, and calling
79 * curl_easy_perform() again. It may not be a good idea to keep the
80 * connection open for a very long time though (more than a few minutes may
81 * result in the server timing out the connection), and you do want to clean
84 curl_easy_cleanup(curl);