-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMailSending.cpp
377 lines (290 loc) · 8.58 KB
/
MailSending.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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/*
This file is part of the DSP-Crowd project
https://www.dsp-crowd.com
Author(s):
- Johannes Natter, [email protected]
File created on 10.01.2023
Copyright (C) 2023, Johannes Natter
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "MailSending.h"
#if 1
#define dGenMailSeStateString(s) #s,
dProcessStateStr(MailSeState);
#endif
using namespace std;
#define dMailSendTimeoutMs 1000
#define dSmtpCodeActionOkeyCompleted 250
mutex MailSending::mtxCurlMulti;
CURLM *MailSending::pCurlMulti = NULL;
MailSending::MailSending()
: Processing("MailSending")
, mState(MailSeStart)
, mStartMs(0)
, mServer("")
, mPort(465)
, mPassword("")
, mRecipientAddr("")
, mRecipientName("")
, mSenderAddr("")
, mSenderName("")
, mSubject("")
, mBody("")
, mpCurl(NULL)
, mpListRecipients(NULL)
, mNumSent(0)
, mDone(Pending)
, mCurlRes(CURLE_OK)
, mRespCode(0)
{}
MailSending::~MailSending()
{
if (mpListRecipients)
{
curl_slist_free_all(mpListRecipients);
mpListRecipients = NULL;
}
if (mpCurl)
{
curl_easy_cleanup(mpCurl);
mpCurl = NULL;
}
}
/* member functions */
void MailSending::serverSet(const string &server)
{
mServer = server;
}
void MailSending::passwordSet(const string &password)
{
mPassword = password;
}
void MailSending::recipientSet(const string &recipient)
{
nameAddrSplit(recipient, mRecipientName, mRecipientAddr);
#if 0
procWrnLog("Rec. addr: '%s'", mRecipientAddr.c_str());
procWrnLog("Rec. name: '%s'", mRecipientName.c_str());
#endif
}
void MailSending::senderSet(const string &sender)
{
nameAddrSplit(sender, mSenderName, mSenderAddr);
}
void MailSending::subjectSet(const string &subject)
{
mSubject = subject;
}
void MailSending::bodySet(const string &body)
{
mBody = body;
}
Success MailSending::process()
{
uint32_t curTimeMs = millis();
uint32_t diffMs = curTimeMs - mStartMs;
Success success;
#if 0
procWrnLog("mState = %s", MailSeStateString[mState]);
#endif
switch (mState)
{
case MailSeStart:
curlGlobalInit();
success = easyHandleCreate();
if (success != Positive)
return procErrLog(-1, "could not create curl easy handle");
success = curlEasyHandleBind();
if (success != Positive)
return procErrLog(-1, "could not bind curl easy handle");
multiProcess();
mStartMs = curTimeMs;
mState = MailSeDoneWait;
break;
case MailSeDoneWait:
multiProcess();
if (diffMs > dMailSendTimeoutMs)
return procErrLog(-1, "timeout sending mail");
if (mDone == Pending)
break;
if (mCurlRes != CURLE_OK)
return procErrLog(-1, "curl performing failed: %s (%d)", curl_easy_strerror(mCurlRes), mCurlRes);
procDbgLog("server returned status code %d", mRespCode);
if (mRespCode != dSmtpCodeActionOkeyCompleted)
return procErrLog(-1, "SMTP server did not return %d", dSmtpCodeActionOkeyCompleted);
return Positive;
break;
default:
break;
}
return Pending;
}
/*
Literature
- https://curl.haxx.se/libcurl/c/
- https://curl.haxx.se/libcurl/c/libcurl-easy.html
- https://curl.haxx.se/libcurl/c/multithread.html
- https://curl.haxx.se/libcurl/c/debug.html
- https://curl.se/libcurl/c/CURLOPT_URL.html
- https://curl.se/libcurl/c/CURLOPT_READFUNCTION.html
- https://curl.se/libcurl/c/CURLOPT_READDATA.html
- https://curl.se/libcurl/c/CURLOPT_UPLOAD_BUFFERSIZE.html
*/
Success MailSending::easyHandleCreate()
{
string strUrl, strUserPwd, strBodyPrefix;
Success success;
procDbgLog("Recipient");
procDbgLog(" Name = %s", mRecipientName.c_str());
procDbgLog(" Address = %s", mRecipientAddr.c_str());
#if 0
procDbgLog("Sender");
procDbgLog(" Name = %s", mSenderName.c_str());
procDbgLog(" Address = %s", mSenderAddr.c_str());
#endif
procDbgLog("Subject = %s", mSubject.c_str());
(void)success;
mpCurl = curl_easy_init();
if (!mpCurl)
return procErrLog(-1, "curl_easy_init() returned 0");
strUrl = "smtps://";
strUrl += mServer + ":" + to_string(mPort);
strUrl += "/target";
curl_easy_setopt(mpCurl, CURLOPT_URL, strUrl.c_str());
mpListRecipients = NULL;
mpListRecipients = curl_slist_append(mpListRecipients, mRecipientAddr.c_str());
curl_easy_setopt(mpCurl, CURLOPT_MAIL_RCPT, mpListRecipients);
strUserPwd = mSenderAddr + ":" + mPassword;
curl_easy_setopt(mpCurl, CURLOPT_USERPWD, strUserPwd.c_str());
curl_easy_setopt(mpCurl, CURLOPT_MAIL_FROM, mSenderAddr.c_str());
curl_easy_setopt(mpCurl, CURLOPT_USERAGENT, "TGSA");
curl_easy_setopt(mpCurl, CURLOPT_UPLOAD, 1L);
curl_easy_setopt(mpCurl, CURLOPT_MAXREDIRS, 50L);
curl_easy_setopt(mpCurl, CURLOPT_HTTP_VERSION, (long)CURL_HTTP_VERSION_2TLS);
curl_easy_setopt(mpCurl, CURLOPT_USE_SSL, (long)CURLUSESSL_ALL);
curl_easy_setopt(mpCurl, CURLOPT_FTP_SKIP_PASV_IP, 1L);
curl_easy_setopt(mpCurl, CURLOPT_TCP_KEEPALIVE, 1L);
curl_easy_setopt(mpCurl, CURLOPT_INFILESIZE_LARGE, (curl_off_t)(mBody.size() + 20));
curl_easy_setopt(mpCurl, CURLOPT_BUFFERSIZE, 59L);
curl_easy_setopt(mpCurl, CURLOPT_UPLOAD_BUFFERSIZE, 59L); // Not working. Minimum is 16k
strBodyPrefix += "To: ";
strBodyPrefix += mRecipientName;
strBodyPrefix += " <";
strBodyPrefix += mRecipientAddr;
strBodyPrefix += ">";
strBodyPrefix += "\r\n";
strBodyPrefix += "From: ";
strBodyPrefix += mSenderName;
strBodyPrefix += " <";
strBodyPrefix += mSenderAddr;
strBodyPrefix += ">";
strBodyPrefix += "\r\n";
strBodyPrefix += "Subject: ";
strBodyPrefix += mSubject;
strBodyPrefix += "\r\n";
strBodyPrefix += "\r\n";
mBody = strBodyPrefix + mBody;
curl_easy_setopt(mpCurl, CURLOPT_READFUNCTION, MailSending::stringToCurlDataRead);
curl_easy_setopt(mpCurl, CURLOPT_READDATA, this);
curl_easy_setopt(mpCurl, CURLOPT_PRIVATE, this);
//curl_easy_setopt(mpCurl, CURLOPT_VERBOSE, 1L);
return Positive;
}
Success MailSending::curlEasyHandleBind()
{
lock_guard<mutex> lock(mtxCurlMulti);
if (!pCurlMulti)
pCurlMulti = curl_multi_init();
if (!pCurlMulti)
return procErrLog(-1, "could not create curl multi handle");
CURLMcode code;
code = curl_multi_add_handle(pCurlMulti, mpCurl);
if (code != CURLM_OK)
return procErrLog(-1, "could not bind curl easy handle");
return Positive;
}
void MailSending::nameAddrSplit(const string &strIn, string &name, string &addr)
{
size_t pos;
name = "";
pos = strIn.find_last_of(' ');
if (pos != string::npos)
{
name = strIn.substr(0, pos);
++pos;
}
if (pos == string::npos)
pos = 0;
addr = strIn.substr(pos);
}
void MailSending::processInfo(char *pBuf, char *pBufEnd)
{
#if 1
dInfo("State\t\t\t%s\n", MailSeStateString[mState]);
#endif
}
/* static functions */
/*
Literature
- https://en.wikipedia.org/wiki/List_of_SMTP_server_return_codes#%E2%80%94_2yz_Positive_completion
*/
void MailSending::multiProcess()
{
lock_guard<mutex> lock(mtxCurlMulti);
int numRunningRequests, numMsgsLeft;
CURLMsg *curlMsg;
CURL *pCurl;
MailSending *pReq;
curl_multi_perform(pCurlMulti, &numRunningRequests);
while (curlMsg = curl_multi_info_read(pCurlMulti, &numMsgsLeft), curlMsg)
{
if (curlMsg->msg != CURLMSG_DONE)
continue;
pCurl = curlMsg->easy_handle;
curl_easy_getinfo(pCurl, CURLINFO_PRIVATE, &pReq);
pReq->mCurlRes = curlMsg->data.result;
curl_easy_getinfo(pCurl, CURLINFO_RESPONSE_CODE, &pReq->mRespCode);
curl_multi_remove_handle(pCurlMulti, pCurl);
if (pReq->mpListRecipients)
{
curl_slist_free_all(pReq->mpListRecipients);
pReq->mpListRecipients = NULL;
}
curl_easy_cleanup(pReq->mpCurl);
pReq->mpCurl = NULL;
pReq->mDone = Positive;
}
}
void MailSending::curlMultiDeInit()
{
lock_guard<mutex> lock(mtxCurlMulti);
if (!pCurlMulti)
return;
curl_multi_cleanup(pCurlMulti);
pCurlMulti = NULL;
dbgLog("MailSending(): multi curl cleanup done");
}
extern "C" size_t MailSending::stringToCurlDataRead(char *ptr, size_t size, size_t nmemb, MailSending *pReq)
{
size_t numReq = size * nmemb;
size_t numLeft = pReq->mBody.size() - pReq->mNumSent;
size_t numRead = MIN(numReq, numLeft);
if (!numRead)
return 0;
const char *pRead = pReq->mBody.c_str() + pReq->mNumSent;
memcpy(ptr, pRead, numRead);
pReq->mNumSent += numRead;
#if 0
wrnLog("Mail data sent: %d", pReq->mNumSent);
#endif
return numRead;
}