25#ifndef __JOIN_SMTP_CLIENT_HPP__
26#define __JOIN_SMTP_CLIENT_HPP__
39 template <
class Protocol>
44 using Stream =
typename Protocol::Stream;
100 this->
_stream = std::move (other._stream);
101 this->
_host = std::move (other._host);
102 this->
_port = other._port;
103 this->
_login = std::move (other._login);
104 this->
_password = std::move (other._password);
126 const std::string&
host ()
const
150 auth +=
"[" + this->
host () +
"]";
154 auth += this->
host ();
159 auth +=
":" + std::to_string (this->
port ());
179 void credentials (
const std::string& login,
const std::string& password)
193 return this->
_stream.setCertificate (cert,
key);
203 return this->
_stream.setCaPath (caPath);
213 return this->
_stream.setCaFile (caFile);
223 this->
_stream.setVerify (verify, depth);
233 return this->
_stream.setCipher (cipher);
243 return this->
_stream.setCipher_1_3 (cipher);
254 endpoint.hostname (this->
host ());
256 if (this->
connect (endpoint) == -1)
268 std::vector <std::string> replies;
280 if (std::find (replies.begin (), replies.end (),
"STARTTLS") != replies.end ())
293 auto auth = std::find_if (replies.begin (), replies.end (), [] (
auto const &r) {return r.find (
"AUTH") != std::string::npos;});
294 if (auth != replies.end ())
296 if (auth->find (
"LOGIN") != std::string::npos)
304 else if (auth->find (
"PLAIN") != std::string::npos)
320 if (this->
sendTo (mail) == -1)
332 if (this->
quit () == -1)
350 this->
_stream.connect (endpoint);
351 return this->
_stream.fail () ? -1 : 0;
373 std::cout << message << std::endl;
375 this->
_stream.write (message.c_str (), message.size ());
376 this->
_stream.write (
"\r\n", 2);
378 return this->
_stream.fail () ? -1 : 0;
386 std::string
readReplies (std::vector <std::string>* replies =
nullptr)
388 std::string reply, code;
396 std::cout << reply << std::endl;
398 if ((reply.find (
"-") != 3) && (reply.find (
" ") != 3))
405 code = reply.substr (0, 3);
409 replies->push_back (reply.substr (4));
464 this->
_stream.startEncryption ();
537 if (this->
sendMessage (
"MAIL FROM: <" + message.
sender ().address () +
">") == -1)
555 for (
auto const& recipient : message.
recipients ())
557 if (this->
sendMessage (
"RCPT TO: <" + recipient.address () +
">") == -1)
623 gethostname (name,
sizeof (name));
646 template <
class Protocol>
701 BasicSmtpClient <Protocol>::operator= (std::move (other));
727 this->
_stream.connectEncrypted (endpoint);
728 return this->
_stream.fail () ? -1 : 0;
static std::string encode(const char *data, size_t size)
encode data.
Definition base64.cpp:343
basic SMTP client.
Definition smtpclient.hpp:41
void setVerify(bool verify, int depth=-1)
Enable/Disable the verification of the peer certificate.
Definition smtpclient.hpp:221
BasicSmtpClient & operator=(const BasicSmtpClient &other)=delete
assign the basic SMTP client instance by copy.
int setCertificate(const std::string &cert, const std::string &key="")
set the certificate and the private key.
Definition smtpclient.hpp:191
int setCaFile(const std::string &caFile)
set the location of the trusted CA certificate file.
Definition smtpclient.hpp:211
int send(const MailMessage &mail)
send mail message.
Definition smtpclient.hpp:251
typename Protocol::Endpoint Endpoint
Definition smtpclient.hpp:43
int greeting()
handle greeting.
Definition smtpclient.hpp:423
int sendData(const MailMessage &message)
send message.
Definition smtpclient.hpp:574
int initialize(std::vector< std::string > &replies)
client init.
Definition smtpclient.hpp:437
BasicSmtpClient(const char *host, uint16_t port=25)
create the basic SMTP client instance.
Definition smtpclient.hpp:51
uint16_t port() const
get port.
Definition smtpclient.hpp:135
std::string _password
SMTP password.
Definition smtpclient.hpp:640
void credentials(const std::string &login, const std::string &password)
set credentials.
Definition smtpclient.hpp:179
int sendFrom(const MailMessage &message)
send sender address.
Definition smtpclient.hpp:535
int sendMessage(const std::string &message)
send message.
Definition smtpclient.hpp:370
BasicSmtpClient(const BasicSmtpClient &other)=delete
create the basic SMTP client instance by copy.
Stream _stream
stream.
Definition smtpclient.hpp:628
int setCaPath(const std::string &caPath)
set the location of the trusted CA certificates.
Definition smtpclient.hpp:201
uint16_t _port
SMTP port.
Definition smtpclient.hpp:634
BasicSmtpClient(const std::string &host, uint16_t port=25)
create the basic SMTP client instance.
Definition smtpclient.hpp:62
typename Protocol::Stream Stream
Definition smtpclient.hpp:44
std::string authority() const
get authority.
Definition smtpclient.hpp:144
int setCipher(const std::string &cipher)
set the cipher list (TLSv1.2 and below).
Definition smtpclient.hpp:231
int loginAuthenticate()
authenticate using LOGIN.
Definition smtpclient.hpp:476
virtual ~BasicSmtpClient()=default
destroy the basic SMTP client instance.
virtual std::string scheme() const
get scheme.
Definition smtpclient.hpp:117
int startTls()
start encryption.
Definition smtpclient.hpp:454
void close()
close the connection.
Definition smtpclient.hpp:358
virtual int connect(const Endpoint &endpoint)
make a connection to the given endpoint.
Definition smtpclient.hpp:348
BasicSmtpClient(BasicSmtpClient &&other)
create the basic SMTP client instance by move.
Definition smtpclient.hpp:84
int setCipher_1_3(const std::string &cipher)
set the cipher list (TLSv1.3).
Definition smtpclient.hpp:241
std::string _login
SMTP login.
Definition smtpclient.hpp:637
const std::string & host() const
get host.
Definition smtpclient.hpp:126
std::string url() const
get URL.
Definition smtpclient.hpp:169
std::string _host
SMTP host.
Definition smtpclient.hpp:631
int plainAuthenticate()
authenticate using PLAIN.
Definition smtpclient.hpp:509
int quit()
send quit.
Definition smtpclient.hpp:603
int sendTo(const MailMessage &message)
send recpient address.
Definition smtpclient.hpp:553
std::string readReplies(std::vector< std::string > *replies=nullptr)
read replies.
Definition smtpclient.hpp:386
std::string hostname() const
get host name.
Definition smtpclient.hpp:620
Basic SMTPS client.
Definition smtpclient.hpp:648
BasicSmtpSecureClient(BasicSmtpSecureClient &&other)
create the basic SMTPS client instance by move.
Definition smtpclient.hpp:689
typename Protocol::Endpoint Endpoint
Definition smtpclient.hpp:650
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 smtpclient.hpp:725
std::string scheme() const override
get scheme.
Definition smtpclient.hpp:714
BasicSmtpSecureClient(const std::string &host, uint16_t port=465)
create the basic SMTPS client instance.
Definition smtpclient.hpp:667
virtual ~BasicSmtpSecureClient()=default
destroy the basic SMTPS client instance.
BasicSmtpSecureClient(const char *host, uint16_t port=465)
create the basic SMTPS client instance.
Definition smtpclient.hpp:657
bool isIpv6Address() const
check if IP address is an IPv6 address.
Definition ipaddress.cpp:1394
mail message.
Definition mailmessage.hpp:230
int writeContent(std::ostream &out) const
write content to the given output stream.
Definition mailmessage.cpp:443
const MailRecipients & recipients() const
get mail recipients.
Definition mailmessage.cpp:323
int writeHeaders(std::ostream &out) const
write header to the given output stream.
Definition mailmessage.cpp:368
void sender(const MailSender &from)
set mail sender.
Definition mailmessage.cpp:287
static uint16_t resolveService(const std::string &service)
resolve service name.
Definition resolver.cpp:546
IpAddress resolveHost(const std::string &host, int family, const IpAddress &server, uint16_t port=dnsPort, int timeout=5000)
resolve host name using address family.
Definition resolver.cpp:194
const std::string key(65, 'a')
key.
Definition acceptor.hpp:32
std::error_code make_error_code(join::Errc code)
Create an std::error_code object.
Definition error.cpp:154
__inline__ bool getline(std::istream &in, std::string &line, std::streamsize max=1024)
read line (delimiter "\r\n").
Definition utils.hpp:299
thread_local std::error_code lastError
last error.
Definition error.cpp:32