df85162424347e692d10cd0436b650ea6e0ad63a
[platform/upstream/curl.git] / docs / examples / simplesmtp.c
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2011, 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 int main(void)
27 {
28   CURL *curl;
29   CURLcode res;
30   struct curl_slist *recipients = NULL;
31
32   /* value for envelope reverse-path */
33   static const char *from = "<bradh@example.com>";
34
35   /* this becomes the envelope forward-path */
36   static const char *to = "<bradh@example.net>";
37
38   curl = curl_easy_init();
39   if(curl) {
40     /* this is the URL for your mailserver - you can also use an smtps:// URL
41      * here */
42     curl_easy_setopt(curl, CURLOPT_URL, "smtp://mail.example.net.");
43
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.
49      */
50     curl_easy_setopt(curl, CURLOPT_MAIL_FROM, from);
51
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);
55
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.
63      */
64     curl_easy_setopt(curl, CURLOPT_READDATA, stdin);
65
66     /* send the message (including headers) */
67     res = curl_easy_perform(curl);
68     /* Check for errors */
69     if(res != CURLE_OK)
70       fprintf(stderr, "curl_easy_perform() failed: %s\n",
71               curl_easy_strerror(res));
72
73     /* free the list of recipients */
74     curl_slist_free_all(recipients);
75
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
82      * up in the end.
83      */
84     curl_easy_cleanup(curl);
85   }
86   return 0;
87 }