25#ifndef JOIN_SERVICES_SMTPCLIENT_HPP
26#define JOIN_SERVICES_SMTPCLIENT_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) {
294 return r.find (
"AUTH") != std::string::npos;
296 if (auth != replies.end ())
298 if (auth->find (
"LOGIN") != std::string::npos)
306 else if (auth->find (
"PLAIN") != std::string::npos)
322 if (this->
sendTo (mail) == -1)
334 if (this->
quit () == -1)
352 this->
_stream.connect (endpoint);
353 return this->
_stream.fail () ? -1 : 0;
375 std::cout << message << std::endl;
377 this->
_stream.write (message.c_str (), message.size ());
378 this->
_stream.write (
"\r\n", 2);
380 return this->
_stream.fail () ? -1 : 0;
388 std::string
readReplies (std::vector<std::string>* replies =
nullptr)
390 std::string reply, code;
398 std::cout << reply << std::endl;
400 if ((reply.find (
"-") != 3) && (reply.find (
" ") != 3))
407 code = reply.substr (0, 3);
411 replies->push_back (reply.substr (4));
466 this->
_stream.startEncryption ();
539 if (this->
sendMessage (
"MAIL FROM: <" + message.
sender ().address () +
">") == -1)
557 for (
auto const& recipient : message.
recipients ())
559 if (this->
sendMessage (
"RCPT TO: <" + recipient.address () +
">") == -1)
625 gethostname (name,
sizeof (name));
648 template <
class Protocol>
729 this->
_stream.connectEncrypted (endpoint);
730 return this->
_stream.fail () ? -1 : 0;
static std::string encode(const char *data, size_t size)
encode data.
Definition base64.cpp:345
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:425
int sendData(const MailMessage &message)
send message.
Definition smtpclient.hpp:576
int initialize(std::vector< std::string > &replies)
client init.
Definition smtpclient.hpp:439
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:642
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:537
int sendMessage(const std::string &message)
send message.
Definition smtpclient.hpp:372
BasicSmtpClient(const BasicSmtpClient &other)=delete
create the basic SMTP client instance by copy.
Stream _stream
stream.
Definition smtpclient.hpp:630
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:636
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:478
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:456
void close()
close the connection.
Definition smtpclient.hpp:360
virtual int connect(const Endpoint &endpoint)
make a connection to the given endpoint.
Definition smtpclient.hpp:350
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:639
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:633
int plainAuthenticate()
authenticate using PLAIN.
Definition smtpclient.hpp:511
int quit()
send quit.
Definition smtpclient.hpp:605
int sendTo(const MailMessage &message)
send recpient address.
Definition smtpclient.hpp:555
std::string readReplies(std::vector< std::string > *replies=nullptr)
read replies.
Definition smtpclient.hpp:388
std::string hostname() const
get host name.
Definition smtpclient.hpp:622
Basic SMTPS client.
Definition smtpclient.hpp:650
BasicSmtpSecureClient(BasicSmtpSecureClient &&other)
create the basic SMTPS client instance by move.
Definition smtpclient.hpp:691
typename Protocol::Endpoint Endpoint
Definition smtpclient.hpp:652
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:727
std::string scheme() const override
get scheme.
Definition smtpclient.hpp:716
BasicSmtpSecureClient(const std::string &host, uint16_t port=465)
create the basic SMTPS client instance.
Definition smtpclient.hpp:669
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:659
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:449
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:550
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:195
const std::string key(65, 'a')
key.
Definition acceptor.hpp:32
std::error_code make_error_code(join::Errc code) noexcept
Create an std::error_code object.
Definition error.cpp:150
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:308