-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
96 lines (72 loc) · 2.49 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*You will most likely need to go here and allow unsecure apps:
https://www.google.com/settings/security/lesssecureapps
*/
#include <curl/curl.h>
#include <stdio.h>
#include <string.h>
#define FROM_MAIL "<[email protected]>"
#define TO_MAIL "<[email protected]>"
static const char *payload_text =
"To: " TO_MAIL "\r\n"
"From: " FROM_MAIL "\r\n"
"Subject: SMTP example message\r\n"
"\r\n" /* empty line to divide headers from body, see RFC5322 */
"The body of the message starts here.\r\n"
"\r\n"
"It could be a lot of lines, could be MIME encoded, whatever.\r\n"
"Check RFC5322.\r\n";
struct upload_status {
size_t bytes_read;
};
static size_t payload_source(char *ptr, size_t size, size_t nmemb,
void *userp) {
struct upload_status *upload_ctx = (struct upload_status *)userp;
const char *data;
size_t room = size * nmemb;
if ((size == 0) || (nmemb == 0) || ((size * nmemb) < 1)) {
return 0;
}
data = &payload_text[upload_ctx->bytes_read];
if (data) {
size_t len = strlen(data);
if (room < len)
len = room;
memcpy(ptr, data, len);
upload_ctx->bytes_read += len;
return len;
}
return 0;
}
int main() {
CURL *curl;
CURLcode res = CURLE_OK;
struct curl_slist *recipients = NULL;
struct upload_status upload_ctx = {0};
curl = curl_easy_init();
if (curl) {
/* Set username and password */
curl_easy_setopt(curl, CURLOPT_USERNAME, "[email protected]");
curl_easy_setopt(curl, CURLOPT_PASSWORD, "yourmailpassword");
curl_easy_setopt(curl, CURLOPT_URL, "smtp://smtp.gmail.com:587");
curl_easy_setopt(curl, CURLOPT_USE_SSL, (long)CURLUSESSL_ALL);
// curl_easy_setopt(curl, CURLOPT_CAINFO, "/path/to/certificate.pem");
curl_easy_setopt(curl, CURLOPT_MAIL_FROM, FROM_MAIL);
recipients = curl_slist_append(recipients, TO_MAIL);
curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, recipients);
curl_easy_setopt(curl, CURLOPT_READFUNCTION, payload_source);
curl_easy_setopt(curl, CURLOPT_READDATA, &upload_ctx);
curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
/* Send the message */
res = curl_easy_perform(curl);
/* Check for errors */
if (res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
/* Free the list of recipients */
curl_slist_free_all(recipients);
/* Always cleanup */
curl_easy_cleanup(curl);
}
return (int)res;
}