https://github.com/cra3z/smtplib
A C++ SMTP client
https://github.com/cra3z/smtplib
cpp smtp-client
Last synced: 3 months ago
JSON representation
A C++ SMTP client
- Host: GitHub
- URL: https://github.com/cra3z/smtplib
- Owner: Cra3z
- License: mit
- Created: 2025-06-23T14:53:07.000Z (4 months ago)
- Default Branch: main
- Last Pushed: 2025-06-24T12:21:56.000Z (4 months ago)
- Last Synced: 2025-06-24T13:34:27.065Z (4 months ago)
- Topics: cpp, smtp-client
- Language: C++
- Homepage:
- Size: 9.77 KB
- Stars: 1
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
en | [zh-cn](README.zh-CN.md)
# smtplibsmtplib is a simple SMTP mail sending library based on Asio, supporting both synchronous and coroutine-based asynchronous interfaces, and supports `SSL/TLS` and `STARTTLS`.
# Build & Install
```shell
git clone https://github.com/Cra3z/smtplib.git
cd smtplib
mkdir
cmake -S . -B -DSMTPLIB_EXAMPLES=ON
cmake --build
cmake --install --prefix
```
The following two variables determine whether to use Boost.Asio or standalone Asio, and whether to use `std::format` or `fmt::format`:
* `SMTPLIB_USE_BOOST_ASIO`: Use Boost.Asio
* `SMTPLIB_USE_FMT_FORMAT`: Use the fmt library for formattingFor example:
```shell
cmake -S . -B -DSMTPLIB_EXAMPLES=ON -DSMTPLIB_USE_BOOST_ASIO=ON -DSMTPLIB_USE_FMT_FORMAT=OFF
```
This will use Boost.Asio and `std::format`.smtplib is a header-only library, so you can simply copy `smtplib.h` to where you need it.
However, it is recommended to embed smtplib as a subproject in your project, or install it first and then import it via CMake's `find_package`.## Build Requirements
* CMake >= 3.26
* C++17 or newer (C++20 is used by default)### Dependencies
* [Boost.Asio](https://github.com/boostorg/asio) >= 1.87 or [standalone Asio](https://github.com/chriskohlhoff/asio) >= 1.28
* [openssl](https://github.com/openssl/openssl) >= 0.9.8
* [fmt](https://github.com/fmtlib/fmt) >= 10.1 (optional, not needed if `std::format` is available)# Examples
Synchronous send with SSL:
```cpp
smtplib::net::io_context io_context;
smtplib::client cli{
io_context.get_executor(),
"your email",
"password",
smtplib::security::ssl
};
cli.connect("smtp.example.com", 465);
cli.send({
.from = "your email",
.to = "target email",
.subject = "Test",
.body = "Hello!"
});
```Asynchronous send (C++20 coroutine) with STARTTLS:
```cpp
smtplib::net::io_context io_context;
smtplib::client cli{
io_context.get_executor(),
"your email",
"password",
smtplib::security::starttls
};
smtplib::net::co_spawn(
io_context,
[&]() -> smtplib::net::awaitable {
co_await cli.async_connect("smtp.example.com", 587);
co_await cli.async_send({
.from = "your email",
.to = "target email",
.subject = "Test",
.body = "Hello!"
});
},
[](std::exception_ptr exp) {
try {
if (exp) {
std::rethrow_exception(exp);
}
}
catch (const std::exception& ex) {
std::cerr << "[ERROR] " << ex.what() << std::endl;
}
}
);
io_context.run();
```