~ubuntu-branches/ubuntu/saucy/curl/saucy-201307251546

« back to all changes in this revision

Viewing changes to docs/examples/simplesmtp.c

  • Committer: Bazaar Package Importer
  • Author(s): Ramakrishnan Muthukrishnan
  • Date: 2011-02-28 19:35:36 UTC
  • mto: (3.6.1 experimental) (1.3.1)
  • mto: This revision was merged to the branch mainline in revision 47.
  • Revision ID: james.westby@ubuntu.com-20110228193536-p3a9jawxxofcsz7o
Tags: upstream-7.21.4
ImportĀ upstreamĀ versionĀ 7.21.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*****************************************************************************
 
2
 *                                  _   _ ____  _
 
3
 *  Project                     ___| | | |  _ \| |
 
4
 *                             / __| | | | |_) | |
 
5
 *                            | (__| |_| |  _ <| |___
 
6
 *                             \___|\___/|_| \_\_____|
 
7
 *
 
8
 */
 
9
 
 
10
#include <stdio.h>
 
11
#include <string.h>
 
12
#include <curl/curl.h>
 
13
 
 
14
int main(void)
 
15
{
 
16
  CURL *curl;
 
17
  CURLcode res;
 
18
  struct curl_slist *recipients = NULL;
 
19
 
 
20
  /* value for envelope reverse-path */
 
21
  static const char *from = "<bradh@example.com>";
 
22
 
 
23
  /* this becomes the envelope forward-path */
 
24
  static const char *to = "<bradh@example.net>";
 
25
 
 
26
  curl = curl_easy_init();
 
27
  if(curl) {
 
28
    /* this is the URL for your mailserver - you can also use an smtps:// URL
 
29
     * here */
 
30
    curl_easy_setopt(curl, CURLOPT_URL, "smtp://mail.example.net.");
 
31
 
 
32
    /* Note that this option isn't strictly required, omitting it will result in
 
33
     * libcurl will sent the MAIL FROM command with no sender data. All
 
34
     * autoresponses should have an empty reverse-path, and should be directed
 
35
     * to the address in the reverse-path which triggered them. Otherwise, they
 
36
     * could cause an endless loop. See RFC 5321 Section 4.5.5 for more details.
 
37
     */
 
38
    curl_easy_setopt(curl, CURLOPT_MAIL_FROM, from);
 
39
 
 
40
    /* Note that the CURLOPT_MAIL_RCPT takes a list, not a char array.  */
 
41
    recipients = curl_slist_append(recipients, to);
 
42
    curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, recipients);
 
43
 
 
44
    /* You provide the payload (headers and the body of the message) as the
 
45
     * "data" element. There are two choices, either:
 
46
     * - provide a callback function and specify the function name using the
 
47
     * CURLOPT_READFUNCTION option; or
 
48
     * - just provide a FILE pointer that can be used to read the data from.
 
49
     * The easiest case is just to read from standard input, (which is available
 
50
     * as a FILE pointer) as shown here.
 
51
     */
 
52
    curl_easy_setopt(curl, CURLOPT_READDATA, stdin);
 
53
 
 
54
    /* send the message (including headers) */
 
55
    res = curl_easy_perform(curl);
 
56
 
 
57
    /* free the list of recipients */
 
58
    curl_slist_free_all(recipients);
 
59
 
 
60
    /* curl won't send the QUIT command until you call cleanup, so you should be
 
61
     * able to re-use this connection for additional messages (setting
 
62
     * CURLOPT_MAIL_FROM and CURLOPT_MAIL_RCPT as required, and calling
 
63
     * curl_easy_perform() again. It may not be a good idea to keep the
 
64
     * connection open for a very long time though (more than a few minutes may
 
65
     * result in the server timing out the connection), and you do want to clean
 
66
     * up in the end.
 
67
     */
 
68
    curl_easy_cleanup(curl);
 
69
  }
 
70
  return 0;
 
71
}