1+ #include < bloomail/base_clients/smtp_client.h>
2+
3+ bloomail::BaseClient::Smtp::Smtp (const std::string &host, int port, bool debug)
4+ : tcp(host, port), tls(tcp), debug(debug) {
5+ this ->recv ();
6+ this ->send (" EHLO localhost" );
7+ this ->recv ();
8+ return ;
9+ }
10+
11+ bloomail::BaseClient::Smtp::~Smtp () { return ; }
12+
13+ void bloomail::BaseClient::Smtp::login (const std::string &username,
14+ const std::string &password) {
15+ this ->username = brewtils::base64::encode (username);
16+ this ->password = brewtils::base64::encode (password);
17+ this ->startTls ();
18+ this ->authenticate ();
19+ this ->quit ();
20+ }
21+
22+ void bloomail::BaseClient::Smtp::send (const std::string &cmd) {
23+ if (this ->tlsConnected ) {
24+ this ->tls .write (cmd + " \r\n " );
25+ } else {
26+ this ->tcp .write (cmd + " \r\n " );
27+ }
28+ }
29+
30+ std::string bloomail::BaseClient::Smtp::recv () {
31+ std::string response =
32+ this ->tlsConnected ? this ->tls .read () : this ->tcp .read ();
33+ if (this ->debug ) {
34+ logger::debug (response);
35+ }
36+ return response;
37+ }
38+
39+ void bloomail::BaseClient::Smtp::startTls () {
40+ this ->send (" STARTTLS" );
41+ this ->recv ();
42+ this ->tls .connect ();
43+ this ->tlsConnected = true ;
44+ this ->send (" EHLO localhost" );
45+ this ->recv ();
46+ }
47+
48+ void bloomail::BaseClient::Smtp::authenticate () {
49+ this ->send (" AUTH LOGIN" );
50+ this ->recv ();
51+ this ->send (this ->username );
52+ this ->recv ();
53+ this ->send (this ->password );
54+ this ->recv ();
55+ }
56+
57+ void bloomail::BaseClient::Smtp::quit () {
58+ this ->send (" QUIT" );
59+ this ->recv ();
60+ this ->tlsConnected = false ;
61+ }
0 commit comments