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

« back to all changes in this revision

Viewing changes to docs/examples/smtp-tls.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
/* This is a simple example showing how to send mail using libcurl's SMTP
 
15
 * capabilities. It builds on the simplesmtp.c example, adding some
 
16
 * authentication and transport security.
 
17
 */
 
18
 
 
19
#define FROM    "<sender@example.org>"
 
20
#define TO      "<addressee@example.net>"
 
21
#define CC      "<info@example.org>"
 
22
 
 
23
static const char *payload_text[]={
 
24
  "Date: Mon, 29 Nov 2010 21:54:29 +1100\n",
 
25
  "To: " TO "\n",
 
26
  "From: " FROM "(Example User)\n",
 
27
  "Cc: " CC "(Another example User)\n",
 
28
  "Message-ID: <dcd7cb36-11db-487a-9f3a-e652a9458efd@rfcpedant.example.org>\n",
 
29
  "Subject: SMTP TLS example message\n",
 
30
  "\n", /* empty line to divide headers from body, see RFC5322 */
 
31
  "The body of the message starts here.\n",
 
32
  "\n",
 
33
  "It could be a lot of lines, could be MIME encoded, whatever.\n",
 
34
  "Check RFC5322.\n",
 
35
  NULL
 
36
};
 
37
 
 
38
struct upload_status {
 
39
  int lines_read;
 
40
};
 
41
 
 
42
static size_t payload_source(void *ptr, size_t size, size_t nmemb, void *userp)
 
43
{
 
44
  struct upload_status *upload_ctx = (struct upload_status *)userp;
 
45
  const char *data;
 
46
 
 
47
  if ((size == 0) || (nmemb == 0) || ((size*nmemb) < 1)) {
 
48
    return 0;
 
49
  }
 
50
 
 
51
  data = payload_text[upload_ctx->lines_read];
 
52
 
 
53
  if (data) {
 
54
    size_t len = strlen(data);
 
55
    memcpy(ptr, data, len);
 
56
    upload_ctx->lines_read ++;
 
57
    return len;
 
58
  }
 
59
  return 0;
 
60
}
 
61
 
 
62
 
 
63
int main(void)
 
64
{
 
65
  CURL *curl;
 
66
  CURLcode res;
 
67
  struct curl_slist *recipients = NULL;
 
68
  struct upload_status upload_ctx;
 
69
 
 
70
  upload_ctx.lines_read = 0;
 
71
 
 
72
  curl = curl_easy_init();
 
73
  if (curl) {
 
74
    /* This is the URL for your mailserver. Note the use of port 587 here,
 
75
     * instead of the normal SMTP port (25). Port 587 is commonly used for
 
76
     * secure mail submission (see RFC4403), but you should use whatever
 
77
     * matches your server configuration. */
 
78
    curl_easy_setopt(curl, CURLOPT_URL, "smtp://mainserver.example.net:587");
 
79
 
 
80
    /* In this example, we'll start with a plain text connection, and upgrade
 
81
     * to Transport Layer Security (TLS) using the STARTTLS command. Be careful
 
82
     * of using CURLUSESSL_TRY here, because if TLS upgrade fails, the transfer
 
83
     * will continue anyway - see the security discussion in the libcurl
 
84
     * tutorial for more details. */
 
85
    curl_easy_setopt(curl, CURLOPT_USE_SSL, CURLUSESSL_ALL);
 
86
 
 
87
    /* If your server doesn't have a valid certificate, then you can disable
 
88
     * part of the Transport Layer Security protection by setting the
 
89
     * CURLOPT_SSL_VERIFYPEER and CURLOPT_SSL_VERIFYHOST options to 0 (false).
 
90
     *   curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
 
91
     *   curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
 
92
     * That is, in general, a bad idea. It is still better than sending your
 
93
     * authentication details in plain text though.
 
94
     * Instead, you should get the issuer certificate (or the host certificate
 
95
     * if the certificate is self-signed) and add it to the set of certificates
 
96
     * that are known to libcurl using CURLOPT_CAINFO and/or CURLOPT_CAPATH. See
 
97
     * docs/SSLCERTS for more information.
 
98
     */
 
99
    curl_easy_setopt(curl, CURLOPT_CAINFO, "/path/to/certificate.pem");
 
100
 
 
101
    /* A common reason for requiring transport security is to protect
 
102
     * authentication details (user names and passwords) from being "snooped"
 
103
     * on the network. Here is how the user name and password are provided: */
 
104
    curl_easy_setopt(curl, CURLOPT_USERNAME, "user@example.net");
 
105
    curl_easy_setopt(curl, CURLOPT_PASSWORD, "P@ssw0rd");
 
106
 
 
107
    /* value for envelope reverse-path */
 
108
    curl_easy_setopt(curl, CURLOPT_MAIL_FROM, FROM);
 
109
    /* Add two recipients, in this particular case they correspond to the
 
110
     * To: and Cc: addressees in the header, but they could be any kind of
 
111
     * recipient. */
 
112
    recipients = curl_slist_append(recipients, TO);
 
113
    recipients = curl_slist_append(recipients, CC);
 
114
    curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, recipients);
 
115
 
 
116
    /* In this case, we're using a callback function to specify the data. You
 
117
     * could just use the CURLOPT_READDATA option to specify a FILE pointer to
 
118
     * read from.
 
119
     */
 
120
    curl_easy_setopt(curl, CURLOPT_READFUNCTION, payload_source);
 
121
    curl_easy_setopt(curl, CURLOPT_READDATA, &upload_ctx);
 
122
 
 
123
    /* Since the traffic will be encrypted, it is very useful to turn on debug
 
124
     * information within libcurl to see what is happening during the transfer.
 
125
     */
 
126
    curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
 
127
 
 
128
    /* send the message (including headers) */
 
129
    res = curl_easy_perform(curl);
 
130
 
 
131
    /* free the list of recipients and clean up */
 
132
    curl_slist_free_all(recipients);
 
133
    curl_easy_cleanup(curl);
 
134
  }
 
135
  return 0;
 
136
}