113 lines
3.3 KiB
C++
113 lines
3.3 KiB
C++
#pragma once
|
|
|
|
#include <Void/Functional/Error.hpp>
|
|
#include <Void/Functional/Result.hpp>
|
|
|
|
#include <chrono>
|
|
#include <cstddef>
|
|
#include <cstdint>
|
|
#include <memory>
|
|
#include <optional>
|
|
#include <stop_token>
|
|
#include <string>
|
|
#include <string_view>
|
|
#include <vector>
|
|
|
|
namespace Void::Net
|
|
{
|
|
enum class SmtpSecurity {
|
|
/// TLS from the first byte (typically port 465).
|
|
implicit_tls,
|
|
/// Plaintext greeting/EHLO, then a required STARTTLS upgrade
|
|
/// (typically port 587 or 25).
|
|
starttls_required,
|
|
};
|
|
|
|
struct SmtpCredentials {
|
|
std::string username{};
|
|
std::string password{};
|
|
};
|
|
|
|
struct SmtpLimits {
|
|
/// Maximum raw message body accepted by `send()`.
|
|
std::uint64_t max_message_bytes{10ULL * 1024ULL * 1024ULL};
|
|
/// Maximum bytes in one complete (possibly multi-line) reply.
|
|
std::uint64_t max_response_bytes{64ULL * 1024ULL};
|
|
/// Maximum bytes in one reply line.
|
|
std::size_t max_line_bytes{4096};
|
|
/// Maximum recipients accepted by `send()`.
|
|
std::size_t max_recipients{100};
|
|
};
|
|
|
|
struct SmtpOptions {
|
|
std::string host{};
|
|
std::uint16_t port{587};
|
|
SmtpSecurity security{SmtpSecurity::starttls_required};
|
|
/// Identity verified against the server certificate; defaults
|
|
/// to `host`. Peer and hostname verification are always on.
|
|
std::string server_name{};
|
|
/// PEM trust store; empty uses the system default store.
|
|
std::string ca_file{};
|
|
/// Credentials are sent with AUTH PLAIN and only over TLS.
|
|
std::optional<SmtpCredentials> credentials{};
|
|
/// Name announced in EHLO.
|
|
std::string client_domain{"localhost"};
|
|
std::chrono::milliseconds connect_timeout{
|
|
std::chrono::seconds{10}};
|
|
std::chrono::milliseconds io_timeout{std::chrono::seconds{30}};
|
|
SmtpLimits limits{};
|
|
std::stop_token stop{};
|
|
};
|
|
|
|
struct SmtpMessage {
|
|
std::string from{};
|
|
std::vector<std::string> recipients{};
|
|
std::string data{};
|
|
};
|
|
|
|
/// Synchronous SMTP submission client over a verified TLS connection.
|
|
///
|
|
/// Supports implicit TLS and STARTTLS-required dialogue, AUTH PLAIN
|
|
/// (TLS only), MAIL/RCPT/DATA with CRLF normalization and dot-stuffing,
|
|
/// bounded replies, bounded message size, deadlines, and cancellation.
|
|
/// A failed send invalidates the client.
|
|
class SmtpClient final
|
|
{
|
|
public:
|
|
|
|
SmtpClient() noexcept;
|
|
~SmtpClient() noexcept;
|
|
SmtpClient(SmtpClient&&) noexcept;
|
|
SmtpClient& operator=(SmtpClient&&) noexcept;
|
|
SmtpClient(const SmtpClient&) = delete;
|
|
SmtpClient& operator=(const SmtpClient&) = delete;
|
|
|
|
/// Connect, greet, negotiate TLS (when required), authenticate
|
|
/// (when credentials are configured), and prepare submission.
|
|
[[nodiscard]] static Void::Result::Result<SmtpClient>
|
|
connect(SmtpOptions options);
|
|
|
|
/// Convenience: connect, send one message, then QUIT.
|
|
[[nodiscard]] static Void::Result::Result<void>
|
|
submit(SmtpOptions options, const SmtpMessage& message);
|
|
|
|
[[nodiscard]] bool valid() const noexcept;
|
|
[[nodiscard]] bool secure() const noexcept;
|
|
|
|
[[nodiscard]] Void::Result::Result<void>
|
|
send(const SmtpMessage& message);
|
|
|
|
/// Send QUIT (best effort) and close the connection.
|
|
[[nodiscard]] Void::Result::Result<void> quit();
|
|
|
|
void close() noexcept;
|
|
|
|
private:
|
|
|
|
struct Impl;
|
|
std::unique_ptr<Impl> m_impl;
|
|
explicit SmtpClient(std::unique_ptr<Impl> impl) noexcept;
|
|
};
|
|
|
|
} // namespace Void::Net
|