25#ifndef JOIN_SERVICES_SMTP_CLIENT_HPP
26#define JOIN_SERVICES_SMTP_CLIENT_HPP
41 template <
class Protocol>
46 using Stream =
typename Protocol::Stream;
126 this->
_stream = std::move (other._stream);
127 this->
_host = std::move (other._host);
128 this->
_port = other._port;
129 this->
_login = std::move (other._login);
130 this->
_password = std::move (other._password);
152 const std::string&
host ()
const
176 auth +=
"[" + this->
host () +
"]";
180 auth += this->
host ();
185 auth +=
":" + std::to_string (this->
port ());
205 void credentials (
const std::string& login,
const std::string& password)
219 endpoint.hostname (this->
host ());
221 if (this->
connect (endpoint) == -1)
233 std::vector<std::string> replies;
245 if (std::find (replies.begin (), replies.end (),
"STARTTLS") != replies.end ())
258 auto auth = std::find_if (replies.begin (), replies.end (), [] (
auto const& r) {
259 return r.find (
"AUTH") != std::string::npos;
261 if (auth != replies.end ())
263 if (auth->find (
"LOGIN") != std::string::npos)
271 else if (auth->find (
"PLAIN") != std::string::npos)
287 if (this->
sendTo (message) == -1)
299 if (this->
quit () == -1)
317 this->
_stream.connect (endpoint);
318 return this->
_stream.fail () ? -1 : 0;
340 std::cout << message << std::endl;
342 this->
_stream.write (message.c_str (), message.size ());
343 this->
_stream.write (
"\r\n", 2);
345 return this->
_stream.fail () ? -1 : 0;
353 std::string
readReplies (std::vector<std::string>* replies =
nullptr)
355 std::string reply, code;
363 std::cout << reply << std::endl;
365 if ((reply.find (
"-") != 3) && (reply.find (
" ") != 3))
372 code = reply.substr (0, 3);
376 replies->push_back (reply.substr (4));
504 if (this->
sendMessage (
"MAIL FROM: <" + message.
sender ().address () +
">") == -1)
522 for (
auto const& recipient : message.
recipients ())
524 if (this->
sendMessage (
"RCPT TO: <" + recipient.address () +
">") == -1)
590 gethostname (name,
sizeof (name));
613 template <
class Protocol>
696 this->
_stream.connect (endpoint);
702 return this->
_stream.fail () ? -1 : 0;
static std::string encode(const char *data, size_t size)
encode data.
Definition base64.cpp:345
static IpAddress lookupAddress(const std::string &host, int family)
resolve host name using system name servers.
Definition resolver.hpp:260
static uint16_t resolveService(const std::string &service) noexcept
resolve service name.
Definition resolver.hpp:673
basic SMTP client.
Definition smtp_protocol.hpp:34
int sendFrom(const SmtpMessage &message)
send sender address.
Definition smtp_client.hpp:502
BasicSmtpClient & operator=(const BasicSmtpClient &other)=delete
assign the basic SMTP client instance by copy.
typename Protocol::Endpoint Endpoint
Definition smtp_client.hpp:45
int greeting()
handle greeting.
Definition smtp_client.hpp:390
int initialize(std::vector< std::string > &replies)
client init.
Definition smtp_client.hpp:404
int sendTo(const SmtpMessage &message)
send recpient address.
Definition smtp_client.hpp:520
BasicSmtpClient(const char *host, uint16_t port=Protocol::defaultPort)
create the basic SMTP client instance.
Definition smtp_client.hpp:53
uint16_t port() const
get port.
Definition smtp_client.hpp:161
std::string _password
SMTP password.
Definition smtp_client.hpp:607
void credentials(const std::string &login, const std::string &password)
set credentials.
Definition smtp_client.hpp:205
int sendMessage(const std::string &message)
send message.
Definition smtp_client.hpp:337
BasicSmtpClient(const BasicSmtpClient &other)=delete
create the basic SMTP client instance by copy.
Stream _stream
stream.
Definition smtp_client.hpp:595
uint16_t _port
SMTP port.
Definition smtp_client.hpp:601
typename Protocol::Stream Stream
Definition smtp_client.hpp:46
BasicSmtpClient(const std::string &host, uint16_t port=Protocol::defaultPort)
create the basic SMTP client instance.
Definition smtp_client.hpp:64
BasicSmtpClient(TlsContext ctx, const char *host, uint16_t port=Protocol::defaultPort)
create the basic SMTP client instance using the given context.
Definition smtp_client.hpp:75
std::string authority() const
get authority.
Definition smtp_client.hpp:170
int sendData(const SmtpMessage &message)
send message.
Definition smtp_client.hpp:541
int loginAuthenticate()
authenticate using LOGIN.
Definition smtp_client.hpp:443
BasicSmtpClient(TlsContext ctx, const std::string &host, uint16_t port=Protocol::defaultPort)
create the basic SMTP client instance using the given context.
Definition smtp_client.hpp:88
virtual ~BasicSmtpClient()=default
destroy the basic SMTP client instance.
virtual std::string scheme() const
get scheme.
Definition smtp_client.hpp:143
int startTls()
start encryption.
Definition smtp_client.hpp:421
void close()
close the connection.
Definition smtp_client.hpp:325
virtual int connect(const Endpoint &endpoint)
make a connection to the given endpoint.
Definition smtp_client.hpp:315
int send(const SmtpMessage &message)
send mail message.
Definition smtp_client.hpp:216
BasicSmtpClient(BasicSmtpClient &&other)
create the basic SMTP client instance by move.
Definition smtp_client.hpp:110
std::string _login
SMTP login.
Definition smtp_client.hpp:604
const std::string & host() const
get host.
Definition smtp_client.hpp:152
std::string url() const
get URL.
Definition smtp_client.hpp:195
std::string _host
SMTP host.
Definition smtp_client.hpp:598
int plainAuthenticate()
authenticate using PLAIN.
Definition smtp_client.hpp:476
int quit()
send quit.
Definition smtp_client.hpp:570
std::string readReplies(std::vector< std::string > *replies=nullptr)
read replies.
Definition smtp_client.hpp:353
std::string hostname() const
get host name.
Definition smtp_client.hpp:587
Basic SMTPS client.
Definition smtp_protocol.hpp:37
BasicSmtpSecureClient(BasicSmtpSecureClient &&other)
create the basic SMTPS client instance by move.
Definition smtp_client.hpp:658
BasicSmtpSecureClient(TlsContext ctx, const std::string &host, uint16_t port=Protocol::defaultPort)
create the basic SMTPS client instance using the given context.
Definition smtp_client.hpp:636
typename Protocol::Endpoint Endpoint
Definition smtp_client.hpp:617
BasicSmtpSecureClient & operator=(const BasicSmtpSecureClient &other)=delete
assign the basic SMTPS client instance by copy.
BasicSmtpSecureClient(const BasicSmtpSecureClient &other)=delete
create the basic SMTPS client instance by copy.
int connect(const Endpoint &endpoint) override
make a connection to the given endpoint.
Definition smtp_client.hpp:694
BasicSmtpSecureClient(TlsContext ctx, const char *host, uint16_t port=Protocol::defaultPort)
create the basic SMTPS client instance using the given context.
Definition smtp_client.hpp:625
std::string scheme() const override
get scheme.
Definition smtp_client.hpp:683
virtual ~BasicSmtpSecureClient()=default
destroy the basic SMTPS client instance.
bool isIpv6Address() const
check if IP address is an IPv6 address.
Definition ip_address.cpp:1394
mail message.
Definition smtp_message.hpp:230
int writeContent(std::ostream &out) const
write content to the given output stream.
Definition smtp_message.cpp:449
void sender(const SmtpSender &from)
set mail sender.
Definition smtp_message.cpp:287
const SmtpRecipients & recipients() const
get mail recipients.
Definition smtp_message.cpp:323
int writeHeaders(std::ostream &out) const
write header to the given output stream.
Definition smtp_message.cpp:368
TLS/DTLS context.
Definition tls_context.hpp:42
Definition acceptor.hpp:32
std::error_code make_error_code(join::Errc code) noexcept
Create an std::error_code object.
Definition error.cpp:195
thread_local std::error_code lastError
last error.
Definition error.cpp:32
bool getline(std::istream &in, std::string &line, std::streamsize max=1024)
read line (delimiter "\r\n").
Definition utils.hpp:307