1
/*****************************************************************************
3
* Project ___| | | | _ \| |
5
* | (__| |_| | _ <| |___
6
* \___|\___/|_| \_\_____|
12
#include <curl/curl.h>
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.
19
#define FROM "<sender@example.org>"
20
#define TO "<addressee@example.net>"
21
#define CC "<info@example.org>"
23
static const char *payload_text[]={
24
"Date: Mon, 29 Nov 2010 21:54:29 +1100\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",
33
"It could be a lot of lines, could be MIME encoded, whatever.\n",
38
struct upload_status {
42
static size_t payload_source(void *ptr, size_t size, size_t nmemb, void *userp)
44
struct upload_status *upload_ctx = (struct upload_status *)userp;
47
if ((size == 0) || (nmemb == 0) || ((size*nmemb) < 1)) {
51
data = payload_text[upload_ctx->lines_read];
54
size_t len = strlen(data);
55
memcpy(ptr, data, len);
56
upload_ctx->lines_read ++;
67
struct curl_slist *recipients = NULL;
68
struct upload_status upload_ctx;
70
upload_ctx.lines_read = 0;
72
curl = curl_easy_init();
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");
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);
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.
99
curl_easy_setopt(curl, CURLOPT_CAINFO, "/path/to/certificate.pem");
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");
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
112
recipients = curl_slist_append(recipients, TO);
113
recipients = curl_slist_append(recipients, CC);
114
curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, recipients);
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
120
curl_easy_setopt(curl, CURLOPT_READFUNCTION, payload_source);
121
curl_easy_setopt(curl, CURLOPT_READDATA, &upload_ctx);
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.
126
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
128
/* send the message (including headers) */
129
res = curl_easy_perform(curl);
131
/* free the list of recipients and clean up */
132
curl_slist_free_all(recipients);
133
curl_easy_cleanup(curl);