forked from btzy/Simple-TCP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple_client_smtp.hpp
632 lines (565 loc) · 23.2 KB
/
simple_client_smtp.hpp
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
#pragma once
#include <ostream>
#include <unordered_map>
#include <memory>
#include <string>
#include <iterator>
#include <istream>
#include <ostream>
#include <sstream>
#include <stdexcept>
#include <type_traits>
#include <chrono>
#include <ctime>
#ifdef SIMPLE_TCP_PRINT_RECEIVED_RESPONSES_TO_STDERR
#include <iostream>
#endif
#include <boost/asio.hpp>
#include <boost/algorithm/string/join.hpp>
#include <boost/algorithm/string/split.hpp>
#include "simple_client_tcp.hpp"
namespace SimpleTCP {
class DefaultSMTPTransporter : public DefaultTransporter {
public:
template <typename Client>
void init(Client&) const {}
template <typename Client>
void connect(Client& client) const {
if (!client.socket || !client.socket->is_open()) {
{
std::unique_ptr<boost::asio::ip::tcp::resolver::query> query;
if (!client.config.has_proxy_server()) {
query = std::make_unique<boost::asio::ip::tcp::resolver::query>(client.host, std::to_string(client.port));
}
else {
auto proxy_host_port = parse_host_port(client.config.proxy_server(), 8080);
query = std::make_unique<boost::asio::ip::tcp::resolver::query>(proxy_host_port.first, std::to_string(proxy_host_port.second));
}
boost::asio::deadline_timer timer(client.io_service);
client.resolver.async_resolve(*query, [&client, &timer](const boost::system::error_code &ec, boost::asio::ip::tcp::resolver::iterator it) {
if (!ec) {
{
std::lock_guard<std::mutex> lock(client.socket_mutex);
client.socket = std::make_unique<socket_type>(client.io_service);
}
client.make_and_start_timeout_connect_timer(timer);
boost::asio::async_connect(*(client.socket), it, [&client, &timer](const boost::system::error_code &ec, boost::asio::ip::tcp::resolver::iterator /*it*/) {
timer.cancel();
if (!ec) {
boost::asio::ip::tcp::no_delay option(true);
client.socket->set_option(option);
}
else {
std::lock_guard<std::mutex> lock(client.socket_mutex);
client.socket = nullptr;
throw boost::system::system_error(ec);
}
});
}
else {
std::lock_guard<std::mutex> lock(client.socket_mutex);
client.socket = nullptr;
throw boost::system::system_error(ec);
}
});
client.io_service.reset();
client.io_service.run();
}
#ifdef SIMPLE_TCP_PRINT_RECEIVED_RESPONSES_TO_STDERR
cerr << client.get_host() << ":" << client.get_port() << endl;
#endif
{
boost::asio::streambuf read_buffer;
std::istream recv_stream(&read_buffer);
client.receive_until_noconnect(read_buffer, "\r\n", *client.socket);
SMTPCode code;
std::string str;
recv_stream >> code;
if (recv_stream.fail() || code != SMTPCode::READY)throw boost::system::system_error(boost::asio::error::operation_aborted);
getline(recv_stream, str, '\n');
if (str.empty())throw boost::system::system_error(boost::asio::error::operation_aborted);
#ifdef SIMPLE_TCP_PRINT_RECEIVED_RESPONSES_TO_STDERR
str.pop_back();
cerr << str << endl;
#endif
}
{
boost::asio::streambuf write_buffer;
std::ostream write_stream(&write_buffer);
write_stream << "HELO " << client.get_config().fully_qualified_domain_name() << "\r\n";
client.send_noconnect(write_buffer, *client.socket);
}
{
boost::asio::streambuf read_buffer;
std::istream recv_stream(&read_buffer);
client.receive_until_noconnect(read_buffer, "\r\n", *client.socket);
SMTPCode code;
std::string str;
recv_stream >> code;
if (recv_stream.fail() || code != SMTPCode::OK)throw boost::system::system_error(boost::asio::error::operation_aborted);
getline(recv_stream, str, '\n');
if (str.empty())throw boost::system::system_error(boost::asio::error::operation_aborted);
#ifdef SIMPLE_TCP_PRINT_RECEIVED_RESPONSES_TO_STDERR
str.pop_back();
cerr << str << endl;
#endif
}
}
}
};
template <typename Transporter = DefaultSMTPTransporter>
class DefaultSMTPConfig : public DefaultConfig<Transporter> {
public:
constexpr std::uint16_t default_port() const noexcept {
return 25;
}
constexpr const char* fully_qualified_domain_name() const noexcept {
return "222.164.227.88";
}
};
enum class RecipientType :std::uint8_t {
DEFAULT, FROM, TO, CC, BCC
};
struct Recipient {
std::string name, email;
RecipientType type;
Recipient() :type(RecipientType::DEFAULT) {}
Recipient(const Recipient&) = default;
Recipient(Recipient&&) = default;
Recipient& operator=(const Recipient&) = default;
Recipient& operator=(Recipient&&) = default;
Recipient(const RecipientType& type) :type(type) {}
template <typename TEmail>
Recipient(const RecipientType& type, TEmail&& email) : email(std::forward<TEmail>(email)), type(type) {}
template <typename TName, typename TEmail>
Recipient(const RecipientType& type, TName&& name, TEmail&& email) : name(std::forward<TName>(name)), email(std::forward<TEmail>(email)), type(type) {}
};
inline std::string escape(const std::string& str) {
std::string ret;
for (const char& c : str) {
if (c == '\"' || c == '\\') {
ret.push_back('\\');
}
ret.push_back(c);
}
return ret;
}
inline std::string stringify_recipient(const Recipient& recipient) {
if (recipient.name.empty()) {
return '\"' + recipient.email + '\"';
}
else {
return '\"' + escape(recipient.name) + '\"' + ' ' + '<' + recipient.email + '>';
}
}
enum class SMTPCode : std::uint16_t {
READY = 220,
CLOSE = 221,
AUTH_SUCCESS = 235,
OK = 250,
LOGIN = 334,
DATA = 354
};
inline static std::string to_string(const SMTPCode& x) {
return std::to_string(static_cast<std::uint16_t>(x));
}
inline std::ostream& operator<<(std::ostream& os, const SMTPCode& code) {
return os << static_cast<std::uint16_t>(code);
}
inline std::istream& operator>>(std::istream& os, SMTPCode& code) {
std::uint16_t x;
os >> x;
code = static_cast<SMTPCode>(x);
return os;
}
namespace EmailType {
struct base_tag {};
struct plain_tag :base_tag {};
struct html_tag :base_tag {};
};
template <typename Client>
class SMTPProcessor {
public:
public:
typedef Client client_type;
typedef typename Client::socket_type socket_type;
typedef typename Client::config_type config_type;
protected:
Client client;
public:
const config_type& get_config() const {
return client.get_config();
}
public:
SMTPProcessor(Client client) :client(client) {}
SMTPProcessor(const std::string& host_port, config_type config = config_type{}) :client(host_port, std::move(config)) {}
SMTPProcessor(std::string&& host_port, config_type config = config_type{}) :client(std::move(host_port), std::move(config)) {}
private:
SMTPCode receive_code_from_server() {
boost::asio::streambuf buf;
std::istream recv_stream(&buf);
client.receive_exactly(buf, 3);
std::uint16_t code;
recv_stream >> code;
if (recv_stream.fail())throw std::invalid_argument("Invalid code received.");
client.receive_until(buf, "\r\n");
#ifdef SIMPLE_TCP_PRINT_RECEIVED_RESPONSES_TO_STDERR
cerr << code << recv_stream.rdbuf();
#endif
return static_cast<SMTPCode>(code);
}
void send_string(const char* code, const std::string& str) {
boost::asio::streambuf write_buffer;
std::ostream write_stream(&write_buffer);
write_stream << code;
if (!str.empty()) {
write_stream << ' ' << str;
}
write_stream << "\r\n";
client.send(write_buffer);
}
void send_string(const char* code, const char* str) {
boost::asio::streambuf write_buffer;
std::ostream write_stream(&write_buffer);
write_stream << code;
if (str != nullptr) {
write_stream << ' ' << str;
}
write_stream << "\r\n";
client.send(write_buffer);
}
void send_string(const char* code) {
boost::asio::streambuf write_buffer;
std::ostream write_stream(&write_buffer);
write_stream << code << "\r\n";
client.send(write_buffer);
}
template <typename Callback>
void send_data_and_CRLF(const char* code, Callback&& callback) {
boost::asio::streambuf write_buffer;
std::ostream write_stream(&write_buffer);
write_stream << code << ' ';
std::forward<Callback>(callback)(write_stream);
write_stream << "\r\n";
client.send(write_buffer);
}
template <typename Callback>
void send_data(Callback&& callback) {
boost::asio::streambuf write_buffer;
std::ostream write_stream(&write_buffer);
std::forward<Callback>(callback)(write_stream);
client.send(write_buffer);
}
void send_CRLF() {
boost::asio::streambuf write_buffer;
std::ostream write_stream(&write_buffer);
write_stream << "\r\n";
client.send(write_buffer);
}
template <typename TKey, typename TValue>
void send_header(TKey&& key, TValue&& value) {
boost::asio::streambuf write_buffer;
std::ostream write_stream(&write_buffer);
write_stream << std::forward<TKey>(key) << ": " << std::forward<TValue>(value) << "\r\n";
client.send(write_buffer);
}
void append_from(const std::string& from) {
send_data_and_CRLF("MAIL", [&](std::ostream& write_stream) {
write_stream << "FROM:<" << from << ">";
});
SMTPCode ret = receive_code_from_server();
if (ret != SMTPCode::OK) throw std::invalid_argument("Invalid code received (" + to_string(ret) + ").");
}
void append_from(std::string&& from) {
send_data_and_CRLF("MAIL", [&](std::ostream& write_stream) {
write_stream << "FROM:<" << std::move(from) << ">";
});
SMTPCode ret = receive_code_from_server();
if (ret != SMTPCode::OK) throw std::invalid_argument("Invalid code received (" + to_string(ret) + ").");
}
void append_from(const char* from) {
send_data_and_CRLF("MAIL", [&](std::ostream& write_stream) {
write_stream << "FROM:<" << from << ">";
});
SMTPCode ret = receive_code_from_server();
if (ret != SMTPCode::OK) throw std::invalid_argument("Invalid code received (" + to_string(ret) + ").");
}
void append_to(const std::string& to) {
send_data_and_CRLF("RCPT", [&](std::ostream& write_stream) {
write_stream << "TO:<" << to << ">";
});
SMTPCode ret = receive_code_from_server();
if (ret != SMTPCode::OK) throw std::invalid_argument("Invalid code received (" + to_string(ret) + ").");
}
void append_to(std::string&& to) {
send_data_and_CRLF("RCPT", [&](std::ostream& write_stream) {
write_stream << "TO:<" << std::move(to) << ">";
});
SMTPCode ret = receive_code_from_server();
if (ret != SMTPCode::OK) throw std::invalid_argument("Invalid code received (" + to_string(ret) + ").");
}
void append_to(const char* to) {
send_data_and_CRLF("RCPT", [&](std::ostream& write_stream) {
write_stream << "TO:<" << to << ">";
});
SMTPCode ret = receive_code_from_server();
if (ret != SMTPCode::OK) throw std::invalid_argument("Invalid code received (" + to_string(ret) + ").");
}
template <typename T, typename A>
void append_to(const std::vector<T, A>& data) {
std::for_each(data.begin(), data.end(), [this](const T& to) {
append_to(to);
});
}
template <typename T, typename A>
void append_to(std::vector<T, A>&& data) {
std::for_each(std::make_move_iterator(data.begin()), std::make_move_iterator(data.end()), [this](T&& to) {
append_to(std::move(to));
});
}
private:
void add_sender(const Recipient& sender) {
append_from(sender.email);
}
void add_recipient_impl(const Recipient& recipient) {
append_to(recipient.email);
}
// from https://stackoverflow.com/a/7728728/1021959
/*template<typename T>
struct has_const_iterator {
private:
template<typename C> static char test(typename C::const_iterator*);
template<typename C> static int test(...);
public:
enum { value = sizeof(test<T>(0)) == sizeof(char) };
};*/
//typename std::enable_if<has_const_iterator<Container>::value>::type add_recipient(const Container &recipients)
template <typename Container>
std::void_t<decltype(&std::remove_reference_t<Container>::begin), decltype(&std::remove_reference_t<Container>::end)> add_recipient_impl(const Container& recipients) {
for (const auto& recipient : recipients) {
add_recipient_impl(recipient);
}
}
template <typename T, typename ...TT>
void add_recipient(T&& recipient, TT&& ...recipients) {
add_recipient_impl(std::forward<T>(recipient));
add_recipient(std::forward<TT>(recipients)...);
}
void add_recipient() {}
std::string make_TO_field_impl(const Recipient& recipient) {
return stringify_recipient(recipient);
}
template <typename...>
struct _std_string{
typedef std::string type;
};
template <typename...T>
using _std_string_t = typename _std_string<T...>::type;
template <typename Container>
_std_string_t<decltype(&std::remove_reference_t<Container>::begin), decltype(&std::remove_reference_t<Container>::end)> make_TO_field_impl(const Container& recipients) {
std::string ret;
for (auto it = recipients.begin(); it != recipients.end(); ++it) {
std::string ret2 = make_TO_field_impl(*it);;
if (!ret.empty() && !ret2.empty()) ret += ", ";
ret += std::move(ret2);
}
return ret;
}
template <typename T, typename ...TT>
std::string make_TO_field(T&& recipient, TT&& ...recipients) {
std::string ret = make_TO_field_impl(std::forward<T>(recipient));
std::string ret2 = make_TO_field(std::forward<TT>(recipients)...);
if (!ret.empty() && !ret2.empty()) ret += ", ";
ret += std::move(ret2);
return std::move(ret);
}
std::string make_TO_field() {
return "";
}
inline std::tm* safe_gmtime(const std::time_t* timer, std::tm* tm_struct) noexcept {
#ifdef _MSC_VER
gmtime_s(tm_struct, timer);
return tm_struct;
#else
return gmtime_r(timer, tm_struct);
#endif
}
inline std::string to_timestamp(const std::chrono::system_clock::time_point& timestamp) {
std::time_t upd_time = std::chrono::system_clock::to_time_t(timestamp);
char buf[128];
std::tm tm_struct;
safe_gmtime(&upd_time, &tm_struct);
//Tue, 15 Jan 2008 16:02:43 -0500
std::strftime(buf, sizeof buf, "%a, %e %b %Y %T +0000", &tm_struct);
return std::string(buf);
}
public:
// note: mailwriter must write a trailing \r\n
// also, the caller must guarantee that the mailwriter writes only proper characters fitting into 7 bit encoding
//std::void_t<decltype(std::remove_reference_t<TSubjectWriter>::operator()), decltype(std::remove_reference_t<TBodyWriter>::operator())>
template <typename TEmailTag, typename TSubjectWriter, typename TBodyWriter, typename TSender, typename... TRecipient>
std::enable_if_t<std::is_base_of_v<EmailType::base_tag, TEmailTag>, std::void_t<decltype(&std::remove_reference_t<TSubjectWriter>::operator()), decltype(&std::remove_reference_t<TBodyWriter>::operator())>> send_mail(TEmailTag, TSubjectWriter&& subjectwriter, TBodyWriter&& mailwriter, TSender&& sender, TRecipient&&... recipient) {
SMTPCode ret;
/*ret = receive_code_from_server();
if (ret != SMTPCode::READY) throw std::invalid_argument("Invalid code received (" + to_string(ret) + ").");
send_string("HELO", get_config().fully_qualified_domain_name());
ret = receive_code_from_server();
if (ret != SMTPCode::OK) throw std::invalid_argument("Invalid code received (" + to_string(ret) + ").");*/
add_sender(sender);
add_recipient(recipient...);
send_string("DATA");
ret = receive_code_from_server();
if (ret != SMTPCode::DATA) throw std::invalid_argument("Invalid code received (" + to_string(ret) + ").");
send_header("From", stringify_recipient(std::forward<TSender>(sender)));
send_header("To", make_TO_field(std::forward<TRecipient>(recipient)...));
send_data([&](std::ostream& write_stream) {
write_stream << "Subject: ";
std::forward<TSubjectWriter>(subjectwriter)(write_stream);
write_stream << "\r\n";
});
send_header("Date", to_timestamp(std::chrono::system_clock::now()));
if (std::is_same<std::decay_t<TEmailTag>, EmailType::html_tag>::value) {
send_header("MIME-Version", "1.0");
send_header("Content-Type", "text/html; charset=\"UTF-8\"");
send_header("Content-Transfer-Encoding", "quoted-printable");
}
send_CRLF();
send_data(std::forward<TBodyWriter>(mailwriter));
send_string(".");
ret = receive_code_from_server();
if (ret != SMTPCode::OK) throw std::invalid_argument("Invalid code received (" + to_string(ret) + ").");
send_string("QUIT");
ret = receive_code_from_server();
}
/*template <typename TSender, typename... TRecipient>
void send_mail(std::istream& subject, std::istream& body, TSender&& sender, TRecipient&&... recipient) {
send_mail([&](std::ostream& write_stream) {
write_stream << subject.rdbuf();
}, [&](std::ostream& write_stream) {
write_stream << body.rdbuf();
}, std::forward<TSender>(sender), std::forward<TRecipient>(recipient)...);
}
template <typename TSender, typename... TRecipient>
void send_mail(const std::string& subject, const std::string& body, TSender&& sender, TRecipient&&... recipient) {
send_mail([&](std::ostream& write_stream) {
write_stream << subject;
}, [&](std::ostream& write_stream) {
write_stream << body;
}, std::forward<TSender>(sender), std::forward<TRecipient>(recipient)...);
}*/
template <typename TEmailTag, typename TSubject, typename TBody, typename TSender, typename... TRecipient>
std::enable_if_t<std::conjunction_v<std::is_base_of<EmailType::base_tag, TEmailTag>, std::is_assignable<std::string, TSubject>, std::is_assignable<std::string, TBody>>> send_mail(TEmailTag, TSubject&& subject, TBody&& body, TSender&& sender, TRecipient&&... recipient) {
send_mail(TEmailTag{}, [&](std::ostream& write_stream) {
write_stream << std::forward<TSubject>(subject);
}, [&](std::ostream& write_stream) {
if (std::is_same<std::decay_t<TEmailTag>, EmailType::html_tag>::value) {
// https://gist.github.com/jprobinson/69a97de73f4a7b0445d2
std::string body_str = std::forward<TBody>(body);
int curr_line_length = 0;
for (auto it = body_str.cbegin(); it != body_str.cend(); ++it) {
const char& ch = *it;
if (curr_line_length > 72) {
// insert '=' if prev char exists and is not a space
if (it != body_str.cbegin() && *(it - 1) != ' ') {
write_stream << '=';
}
write_stream << "\r\n";
curr_line_length = 0;
}
if ((ch >= static_cast<char>(0x20)) && (ch <= static_cast<char>(0x7E)) && (ch != '=')) {
write_stream << ch;
// double escape newline periods
// http://tools.ietf.org/html/rfc5321#section-4.5.2
if (curr_line_length == 0 && ch == '.') {
write_stream << ".";
++curr_line_length;
}
}
else
{
write_stream << '=';
write_stream << uppercase << hex << ((static_cast<int>(ch) >> 4) & 0x0F);
write_stream << uppercase << hex << (static_cast<int>(ch) & 0x0F);
// 2 more chars bc hex and equals
curr_line_length += 2;
}
++curr_line_length;
}
write_stream << "\r\n";
}
else {
std::string body_str = std::forward<TBody>(body);
if (body_str.empty() || body_str.back() != '\n')body_str.push_back('\n');
for (auto it = body_str.cbegin(); it != body_str.cend(); ++it) {
const char& ch = *it;
if (ch == '\n' && (it == body_str.cbegin() || *(it - 1) != '\r')) {
write_stream << '\r';
}
else if (ch == '.' && (it == body_str.cbegin() || *(it - 1) == '\n')) {
write_stream << '.';
}
write_stream << ch;
}
}
}, std::forward<TSender>(sender), std::forward<TRecipient>(recipient)...);
}
/*template <typename TEmailType, typename TSender, typename... TRecipient>
std::enable_if_t<std::is_base_of_v<EmailType::base_tag, TEmailTag>> send_mail(TEmailType email_type, std::string&& subject, std::string&& body, TSender&& sender, TRecipient&&... recipient) {
send_mail([&](std::ostream& write_stream) {
write_stream << std::move(subject);
}, [&](std::ostream& write_stream) {
write_stream << std::move(body);
}, std::forward<TSender>(sender), std::forward<TRecipient>(recipient)...);
}
template <typename TEmailType, typename TSender, typename... TRecipient>
std::enable_if_t<std::is_base_of_v<EmailType::base_tag, TEmailTag>> send_mail(TEmailType email_type, const std::string& subject, const std::string& body, TSender&& sender, TRecipient&&... recipient) {
send_mail([&](std::ostream& write_stream) {
write_stream << subject;
}, [&](std::ostream& write_stream) {
write_stream << body;
}, std::forward<TSender>(sender), std::forward<TRecipient>(recipient)...);
}*/
template <typename TSubjectWriter, typename TBodyWriter, typename TSender, typename... TRecipient>
inline std::void_t<decltype(&std::remove_reference_t<TSubjectWriter>::operator()), decltype(&std::remove_reference_t<TBodyWriter>::operator())> send_mail(TSubjectWriter&& subjectwriter, TBodyWriter&& mailwriter, TSender&& sender, TRecipient&&... recipient) {
send_mail(EmailType::plain_tag{}, std::forward<TSubjectWriter>(subjectwriter), std::forward<TBodyWriter>(mailwriter), std::forward<TSender>(sender), std::forward<TRecipient>(recipient)...);
}
template <typename TSubject, typename TBody, typename TSender, typename... TRecipient>
inline std::enable_if_t<std::conjunction_v<std::is_assignable<std::string, TSubject>, std::is_assignable<std::string, TBody>>> send_mail(TSubject&& subject, TBody&& body, TSender&& sender, TRecipient&&... recipient) {
send_mail(EmailType::plain_tag{}, std::forward<TSubject>(subject), std::forward<TBody>(body), std::forward<TSender>(sender), std::forward<TRecipient>(recipient)...);
}
/*template <typename TFrom, typename TTo, typename TCc, typename TBcc>
void send_mail(TFrom&& from, TTo&& to, TCc&& cc, TBcc&& bcc, std::istream& body) {
SMTPCode ret;
ret = receive_code_from_server();
if (ret != SMTPCode::READY) throw std::invalid_argument("Invalid code received (" + to_string(ret) + ").");
send_string("HELO", get_config().fully_qualified_domain_name());
ret = receive_code_from_server();
if (ret != SMTPCode::OK) throw std::invalid_argument("Invalid code received (" + to_string(ret) + ").");
append_from(std::forward<TFrom>(from));
append_to(std::forward<TTo>(to));
append_to(std::forward<TCc>(cc));
append_to(std::forward<TBcc>(bcc));
send_string("DATA");
ret = receive_code_from_server();
if (ret != SMTPCode::DATA) throw std::invalid_argument("Invalid code received (" + to_string(ret) + ").");
send_data([&](std::ostream& write_stream) {
write_stream << body.rdbuf();
});
ret = receive_code_from_server();
if (ret != SMTPCode::OK) throw std::invalid_argument("Invalid code received (" + to_string(ret) + ").");
send_string("QUIT");
ret = receive_code_from_server();
}
template <typename TFrom, typename TTo>
void send_mail(TFrom&& from, TTo&& to, const std::string& body) {
send_mail(std::forward<TFrom>(from), std::forward<TTo>(to), vector<std::string>(), vector<std::string>(), std::istringstream(body));
}
template <typename TFrom, typename TTo>
void send_mail(TFrom&& from, TTo&& to, std::string&& body) {
send_mail(std::forward<TFrom>(from), std::forward<TTo>(to), vector<std::string>(), vector<std::string>(), std::istringstream(std::move(body)));
}*/
};
template <typename Config>
using SMTPClient = SMTPProcessor<TCPClient<Config>>;
typedef SMTPClient<DefaultSMTPConfig<>> DefaultSMTPClient;
}