Compare commits

..

30 Commits

Author SHA1 Message Date
Paolo Barbolini
036062a084 Prepare v0.11.18 2025-07-28 09:38:52 +02:00
Francesco Luzzi
8873153178 feat: add ability to give a name to an inline attachment (#1101) 2025-07-21 08:19:31 +02:00
Paolo Barbolini
b073df7666 build(deps): upgrade socket2 to v0.6 (#1098) 2025-07-05 12:09:17 +02:00
Paolo Barbolini
cf6b767a9c Prepare v0.11.17 (#1093) 2025-06-05 21:41:17 +02:00
Paolo Barbolini
d3d8e24824 feat: add rustls-platform-verifier support (#1081) 2025-06-02 10:43:17 +02:00
Paolo Barbolini
c4df9730aa refactor(smtp/pool): remove duplicate abort_concurrent implementation (#1092) 2025-05-24 16:37:54 +02:00
Paolo Barbolini
bfed19e6ad refactor(stub): always use std Mutex (#1091) 2025-05-24 14:34:09 +00:00
David Campbell
629967ac98 docs: use Mailbox::new rather than string parsing (#1090) 2025-05-24 16:21:15 +02:00
Paolo Barbolini
06e381ec9c Prepare v0.11.16 (#1089) 2025-05-12 11:16:14 +02:00
Paolo Barbolini
d9ce9a6e47 chore: deprecate ungated TLS types _when_ no TLS backend is enabled (#1084) 2025-05-11 09:36:47 +02:00
Paolo Barbolini
e892b55b6b build(deps): upgrade webpki-roots to v1 (#1088) 2025-05-06 12:37:39 +00:00
Paolo Barbolini
771d212198 build: gate web-time behind cfg(target_arch = "wasm32") (#1086) 2025-05-01 18:32:26 +02:00
Paolo Barbolini
83ba93944d docs: add missing doc(cfg(...)) attributes (#1085) 2025-05-01 18:16:40 +02:00
Paolo Barbolini
de3ab006e2 fix: feature gate internal TransportBuilder::tls to avoid recursive call site (#1083) 2025-05-01 15:09:07 +02:00
Paolo Barbolini
9504b7f45c refactor: cleanup internal TlsParameters and (Async)NetworkStream config (#1082) 2025-05-01 14:00:56 +02:00
dependabot[bot]
c91b356a96 build(deps): bump tokio from 1.44.1 to 1.44.2 (#1080)
Bumps [tokio](https://github.com/tokio-rs/tokio) from 1.44.1 to 1.44.2.
- [Release notes](https://github.com/tokio-rs/tokio/releases)
- [Commits](https://github.com/tokio-rs/tokio/compare/tokio-1.44.1...tokio-1.44.2)

---
updated-dependencies:
- dependency-name: tokio
  dependency-version: 1.44.2
  dependency-type: indirect
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-04-08 06:57:33 +02:00
dependabot[bot]
118c1ad47f build(deps): bump openssl from 0.10.71 to 0.10.72 (#1079)
Bumps [openssl](https://github.com/sfackler/rust-openssl) from 0.10.71 to 0.10.72.
- [Release notes](https://github.com/sfackler/rust-openssl/releases)
- [Commits](https://github.com/sfackler/rust-openssl/compare/openssl-v0.10.71...openssl-v0.10.72)

---
updated-dependencies:
- dependency-name: openssl
  dependency-version: 0.10.72
  dependency-type: indirect
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-04-06 11:49:28 +02:00
Paolo Barbolini
8bf4d3a9c1 style: fix clippy::io_other_error (#1078) 2025-04-03 10:28:06 +00:00
Paolo Barbolini
1fcff673ba fix: remove E: Clone bound from AsyncFileTransport Clone impl (#1075) 2025-04-03 08:05:39 +00:00
Paolo Barbolini
8c70c0cfb4 build(deps): upgrade semver compatible dependencies (#1076) 2025-04-03 07:54:06 +00:00
Paolo Barbolini
63d8d30088 fix: let cannot be used for global variables (#1077) 2025-04-03 09:50:06 +02:00
Paolo Barbolini
6c0be84817 Prepare v0.11.15 (#1070) 2025-03-10 17:26:17 +01:00
Paolo Barbolini
6059cb04d6 build(deps): upgrade email-encoding to v0.4 (#1069) 2025-03-09 07:22:20 +00:00
Paolo Barbolini
fdf0346556 style: fix rustdoc::broken_intra_doc_links (#1068) 2025-03-09 07:55:15 +01:00
Paolo Barbolini
0f9455715c build(deps): upgrade semver compatible locked dependencies (#1067) 2025-03-08 11:52:38 +00:00
Popax21
0b3a1ed278 feat: add controlled shutdown methods (#1045) 2025-03-08 12:43:05 +01:00
Paolo Barbolini
76bf68268f build(deps): bump minimum supported serde to v1.0.110 (#1064) 2025-03-04 13:06:17 +01:00
Paolo Barbolini
99a86c0fac build(deps): bump minimum supported rustls to v0.23.18 (#1063) 2025-03-04 12:55:43 +01:00
Paolo Barbolini
f0de9ef02c style: deny unreachable_pub lint (#1058) 2025-02-23 10:17:17 +01:00
Paolo Barbolini
b4ddcbdcfc build: bump MSRV to 1.74 (#1060) 2025-02-23 10:16:57 +01:00
29 changed files with 1163 additions and 464 deletions

View File

@@ -75,8 +75,8 @@ jobs:
rust: stable rust: stable
- name: beta - name: beta
rust: beta rust: beta
- name: '1.71' - name: '1.74'
rust: '1.71' rust: '1.74'
steps: steps:
- name: Checkout - name: Checkout

View File

@@ -1,3 +1,99 @@
<a name="v0.11.18"></a>
### v0.11.18 (2025-07-28)
#### Features
* Allow inline attachments to be named ([#1101])
#### Misc
* Upgrade `socket2` to v0.6 ([#1098])
[#1098]: https://github.com/lettre/lettre/pull/1098
[#1101]: https://github.com/lettre/lettre/pull/1101
<a name="v0.11.17"></a>
### v0.11.17 (2025-06-06)
#### Features
* Add support for `rustls-platform-verifier` ([#1081])
#### Misc
* Change readme example to use `Mailbox::new` instead of string parsing ([#1090])
* Replace futures-util `Mutex` with std `Mutex` in `AsyncStubTransport` ([#1091])
* Avoid duplicate `abort_concurrent` implementation ([#1092])
[#1081]: https://github.com/lettre/lettre/pull/1081
[#1090]: https://github.com/lettre/lettre/pull/1090
[#1091]: https://github.com/lettre/lettre/pull/1091
[#1092]: https://github.com/lettre/lettre/pull/1092
<a name="v0.11.16"></a>
### v0.11.16 (2025-05-12)
#### Features
* Always implement `Clone` for `AsyncFileTransport` ([#1075])
#### Changes
* `Tls`, `CertificateStore`, `TlsParameters`, `TlsParametersBuilder`, `Certificate` and `Identity`
are now marked as deprecated when no TLS backend is enabled. They will be properly feature gated
in lettre v0.12 ([#1084])
#### Misc
* Gate `web-time` behind `cfg(target_arch = "wasm32")]` ([#1086])
* Add missing `#[doc(cfg(...))]` attributes ([#1086])
* Upgrade `webpki-roots` to v1 ([#1088])
* Cleanup internal `TlsParameters` and `(Async)NetworkStream` structures ([#1082])
* Feature gate internal `TransportBuilder::tls` to avoid recursive call site warnings ([#1083])
* Fix workaround for embedding cargo script in rustdoc output ([#1077])
* Fix `clippy::io_other_error` warnings ([#1078])
* Upgrade semver compatible dependencies ([#1076], [#1079], [#1080])
[#1075]: https://github.com/lettre/lettre/pull/1075
[#1076]: https://github.com/lettre/lettre/pull/1076
[#1077]: https://github.com/lettre/lettre/pull/1077
[#1078]: https://github.com/lettre/lettre/pull/1078
[#1079]: https://github.com/lettre/lettre/pull/1079
[#1080]: https://github.com/lettre/lettre/pull/1080
[#1082]: https://github.com/lettre/lettre/pull/1082
[#1083]: https://github.com/lettre/lettre/pull/1083
[#1084]: https://github.com/lettre/lettre/pull/1084
[#1086]: https://github.com/lettre/lettre/pull/1086
[#1088]: https://github.com/lettre/lettre/pull/1088
<a name="v0.11.15"></a>
### v0.11.15 (2025-03-10)
#### Upgrade notes
* MSRV is now 1.74 ([#1060])
#### Features
* Add controlled shutdown methods ([#1045], [#1068])
#### Misc
* Deny `unreachable_pub` lint ([#1058])
* Bump minimum supported `rustls` ([#1063])
* Bump minimum supported `serde` ([#1064])
* Upgrade semver compatible dependencies ([#1067])
* Upgrade `email-encoding` to v0.4 ([#1069])
[#1045]: https://github.com/lettre/lettre/pull/1045
[#1058]: https://github.com/lettre/lettre/pull/1058
[#1060]: https://github.com/lettre/lettre/pull/1060
[#1063]: https://github.com/lettre/lettre/pull/1063
[#1064]: https://github.com/lettre/lettre/pull/1064
[#1067]: https://github.com/lettre/lettre/pull/1067
[#1068]: https://github.com/lettre/lettre/pull/1068
[#1069]: https://github.com/lettre/lettre/pull/1069
<a name="v0.11.14"></a> <a name="v0.11.14"></a>
### v0.11.14 (2025-02-23) ### v0.11.14 (2025-02-23)

790
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@@ -1,7 +1,7 @@
[package] [package]
name = "lettre" name = "lettre"
# remember to update html_root_url and README.md (Cargo.toml example and deps.rs badge) # remember to update html_root_url and README.md (Cargo.toml example and deps.rs badge)
version = "0.11.14" version = "0.11.18"
description = "Email client" description = "Email client"
readme = "README.md" readme = "README.md"
homepage = "https://lettre.rs" homepage = "https://lettre.rs"
@@ -11,7 +11,7 @@ authors = ["Alexis Mousset <contact@amousset.me>", "Paolo Barbolini <paolo@paolo
categories = ["email", "network-programming"] categories = ["email", "network-programming"]
keywords = ["email", "smtp", "mailer", "message", "sendmail"] keywords = ["email", "smtp", "mailer", "message", "sendmail"]
edition = "2021" edition = "2021"
rust-version = "1.71" rust-version = "1.74"
[badges] [badges]
is-it-maintained-issue-resolution = { repository = "lettre/lettre" } is-it-maintained-issue-resolution = { repository = "lettre/lettre" }
@@ -32,25 +32,26 @@ mime = { version = "0.3.4", optional = true }
fastrand = { version = "2.0", optional = true } fastrand = { version = "2.0", optional = true }
quoted_printable = { version = "0.5", optional = true } quoted_printable = { version = "0.5", optional = true }
base64 = { version = "0.22", optional = true } base64 = { version = "0.22", optional = true }
email-encoding = { version = "0.3", optional = true } email-encoding = { version = "0.4", optional = true }
# file transport # file transport
uuid = { version = "1", features = ["v4"], optional = true } uuid = { version = "1", features = ["v4"], optional = true }
serde = { version = "1", features = ["derive"], optional = true } serde = { version = "1.0.110", features = ["derive"], optional = true }
serde_json = { version = "1", optional = true } serde_json = { version = "1", optional = true }
# smtp-transport # smtp-transport
nom = { version = "8", optional = true } nom = { version = "8", optional = true }
hostname = { version = "0.4", optional = true } # feature hostname = { version = "0.4", optional = true } # feature
socket2 = { version = "0.5.1", optional = true } socket2 = { version = "0.6", optional = true }
url = { version = "2.4", optional = true } url = { version = "2.4", optional = true }
percent-encoding = { version = "2.3", optional = true } percent-encoding = { version = "2.3", optional = true }
## tls ## tls
native-tls = { version = "0.2.9", optional = true } # feature native-tls = { version = "0.2.9", optional = true } # feature
rustls = { version = "0.23.5", default-features = false, features = ["logging", "std", "tls12"], optional = true } rustls = { version = "0.23.18", default-features = false, features = ["logging", "std", "tls12"], optional = true }
rustls-platform-verifier = { version = "0.6.0", optional = true }
rustls-native-certs = { version = "0.8", optional = true } rustls-native-certs = { version = "0.8", optional = true }
webpki-roots = { version = "0.26", optional = true } webpki-roots = { version = "1.0.0", optional = true }
boring = { version = "4", optional = true } boring = { version = "4", optional = true }
# async # async
@@ -73,6 +74,7 @@ sha2 = { version = "0.10", features = ["oid"], optional = true }
rsa = { version = "0.9", optional = true } rsa = { version = "0.9", optional = true }
ed25519-dalek = { version = "2", optional = true } ed25519-dalek = { version = "2", optional = true }
[target.'cfg(target_arch = "wasm32")'.dependencies]
## web-time for wasm support ## web-time for wasm support
web-time = { version = "1.1.0", optional = true } web-time = { version = "1.1.0", optional = true }

View File

@@ -28,8 +28,8 @@
</div> </div>
<div align="center"> <div align="center">
<a href="https://deps.rs/crate/lettre/0.11.14"> <a href="https://deps.rs/crate/lettre/0.11.18">
<img src="https://deps.rs/crate/lettre/0.11.14/status.svg" <img src="https://deps.rs/crate/lettre/0.11.18/status.svg"
alt="dependency status" /> alt="dependency status" />
</a> </a>
</div> </div>
@@ -53,12 +53,12 @@ Lettre does not provide (for now):
## Supported Rust Versions ## Supported Rust Versions
Lettre supports all Rust versions released in the last 6 months. At the time of writing Lettre supports all Rust versions released in the last 6 months. At the time of writing
the minimum supported Rust version is 1.71, but this could change at any time either from the minimum supported Rust version is 1.74, but this could change at any time either from
one of our dependencies bumping their MSRV or by a new patch release of lettre. one of our dependencies bumping their MSRV or by a new patch release of lettre.
## Example ## Example
This library requires Rust 1.71 or newer. This library requires Rust 1.74 or newer.
To use this library, add the following to your `Cargo.toml`: To use this library, add the following to your `Cargo.toml`:
```toml ```toml
@@ -73,9 +73,9 @@ use lettre::{Message, SmtpTransport, Transport};
fn main() { fn main() {
let email = Message::builder() let email = Message::builder()
.from("NoBody <nobody@domain.tld>".parse().unwrap()) .from(Mailbox::new("NoBody".to_owned(), "nobody@domain.tld".parse().unwrap()))
.reply_to("Yuin <yuin@domain.tld>".parse().unwrap()) .reply_to(Mailbox::new("Yuin".to_owned(), "yuin@domain.tld".parse().unwrap()))
.to("Hei <hei@domain.tld>".parse().unwrap()) .to(Mailbox::new("Hei".to_owned(), "hei@domain.tld".parse().unwrap()))
.subject("Happy new year") .subject("Happy new year")
.header(ContentType::TEXT_PLAIN) .header(ContentType::TEXT_PLAIN)
.body(String::from("Be happy!")) .body(String::from("Be happy!"))

View File

@@ -53,7 +53,7 @@ mod serde_forward_path {
} }
} }
} }
pub fn deserialize<'de, D>(deserializer: D) -> Result<Vec<Address>, D::Error> pub(super) fn deserialize<'de, D>(deserializer: D) -> Result<Vec<Address>, D::Error>
where where
D: serde::Deserializer<'de>, D: serde::Deserializer<'de>,
{ {
@@ -163,6 +163,7 @@ impl Envelope {
} }
#[cfg(feature = "builder")] #[cfg(feature = "builder")]
#[cfg_attr(docsrs, doc(cfg(feature = "builder")))]
impl TryFrom<&Headers> for Envelope { impl TryFrom<&Headers> for Envelope {
type Error = Error; type Error = Error;

View File

@@ -45,6 +45,7 @@ use crate::transport::smtp::Error;
#[async_trait] #[async_trait]
pub trait Executor: Debug + Send + Sync + 'static + private::Sealed { pub trait Executor: Debug + Send + Sync + 'static + private::Sealed {
#[cfg(feature = "smtp-transport")] #[cfg(feature = "smtp-transport")]
#[allow(private_bounds)]
type Handle: SpawnHandle; type Handle: SpawnHandle;
#[cfg(feature = "smtp-transport")] #[cfg(feature = "smtp-transport")]
type Sleep: Future<Output = ()> + Send + 'static; type Sleep: Future<Output = ()> + Send + 'static;
@@ -82,8 +83,8 @@ pub trait Executor: Debug + Send + Sync + 'static + private::Sealed {
#[doc(hidden)] #[doc(hidden)]
#[cfg(feature = "smtp-transport")] #[cfg(feature = "smtp-transport")]
#[async_trait] #[async_trait]
pub trait SpawnHandle: Debug + Send + Sync + 'static + private::Sealed { pub(crate) trait SpawnHandle: Debug + Send + Sync + 'static + private::Sealed {
async fn shutdown(self); async fn shutdown(&self);
} }
/// Async [`Executor`] using `tokio` `1.x` /// Async [`Executor`] using `tokio` `1.x`
@@ -177,7 +178,7 @@ impl Executor for Tokio1Executor {
#[cfg(all(feature = "smtp-transport", feature = "tokio1"))] #[cfg(all(feature = "smtp-transport", feature = "tokio1"))]
#[async_trait] #[async_trait]
impl SpawnHandle for tokio1_crate::task::JoinHandle<()> { impl SpawnHandle for tokio1_crate::task::JoinHandle<()> {
async fn shutdown(self) { async fn shutdown(&self) {
self.abort(); self.abort();
} }
} }
@@ -201,7 +202,7 @@ pub struct AsyncStd1Executor;
#[cfg(feature = "async-std1")] #[cfg(feature = "async-std1")]
impl Executor for AsyncStd1Executor { impl Executor for AsyncStd1Executor {
#[cfg(feature = "smtp-transport")] #[cfg(feature = "smtp-transport")]
type Handle = async_std::task::JoinHandle<()>; type Handle = futures_util::future::AbortHandle;
#[cfg(feature = "smtp-transport")] #[cfg(feature = "smtp-transport")]
type Sleep = BoxFuture<'static, ()>; type Sleep = BoxFuture<'static, ()>;
@@ -211,7 +212,9 @@ impl Executor for AsyncStd1Executor {
F: Future<Output = ()> + Send + 'static, F: Future<Output = ()> + Send + 'static,
F::Output: Send + 'static, F::Output: Send + 'static,
{ {
async_std::task::spawn(fut) let (handle, registration) = futures_util::future::AbortHandle::new_pair();
async_std::task::spawn(futures_util::future::Abortable::new(fut, registration));
handle
} }
#[cfg(feature = "smtp-transport")] #[cfg(feature = "smtp-transport")]
@@ -272,9 +275,9 @@ impl Executor for AsyncStd1Executor {
#[cfg(all(feature = "smtp-transport", feature = "async-std1"))] #[cfg(all(feature = "smtp-transport", feature = "async-std1"))]
#[async_trait] #[async_trait]
impl SpawnHandle for async_std::task::JoinHandle<()> { impl SpawnHandle for futures_util::future::AbortHandle {
async fn shutdown(self) { async fn shutdown(&self) {
self.cancel().await; self.abort();
} }
} }
@@ -291,5 +294,5 @@ mod private {
impl Sealed for tokio1_crate::task::JoinHandle<()> {} impl Sealed for tokio1_crate::task::JoinHandle<()> {}
#[cfg(all(feature = "smtp-transport", feature = "async-std1"))] #[cfg(all(feature = "smtp-transport", feature = "async-std1"))]
impl Sealed for async_std::task::JoinHandle<()> {} impl Sealed for futures_util::future::AbortHandle {}
} }

View File

@@ -6,7 +6,7 @@
//! * Secure defaults //! * Secure defaults
//! * Async support //! * Async support
//! //!
//! Lettre requires Rust 1.71 or newer. //! Lettre requires Rust 1.74 or newer.
//! //!
//! ## Features //! ## Features
//! //!
@@ -93,17 +93,20 @@
//! When the `rustls` feature is enabled, one of the following verification backends //! When the `rustls` feature is enabled, one of the following verification backends
//! MUST also be enabled. //! MUST also be enabled.
//! //!
//! * **rustls-native-certs**: verify TLS certificates using the platform's native certificate store (see [`rustls-native-certs`]) //! * **rustls-platform-verifier**: verify TLS certificate using the OS's native certificate store (see [`rustls-platform-verifier`])
//! * **rustls-native-certs**: verify TLS certificates using the platform's native certificate store (see [`rustls-native-certs`]) - when in doubt use `rustls-platform-verifier`
//! * **webpki-roots**: verify TLS certificates against Mozilla's root certificates (see [`webpki-roots`]) //! * **webpki-roots**: verify TLS certificates against Mozilla's root certificates (see [`webpki-roots`])
//! //!
//! For the `rustls-native-certs` backend to work correctly, the following packages //! The following packages will need to be installed in order for the build
//! will need to be installed in order for the build stage and the compiled program //! stage and the compiled program to run properly.
//! to run properly.
//! //!
//! | Distro | Build-time packages | Runtime packages | //! | Verification backend | Distro | Build-time packages | Runtime packages |
//! | ------------ | -------------------------- | ---------------------------- | //! | --------------------- | ------------ | -------------------------- | ---------------------------- |
//! | Debian | none | `ca-certificates` | //! | `rustls-platform-verifier` | Debian | none | `ca-certificates` |
//! | Alpine Linux | none | `ca-certificates` | //! | `rustls-platform-verifier` | Alpine Linux | none | `ca-certificates` |
//! | `rustls-native-certs` | Debian | none | `ca-certificates` |
//! | `rustls-native-certs` | Alpine Linux | none | `ca-certificates` |
//! | `webpki-roots` | any | none | none |
//! //!
//! ### Sendmail transport //! ### Sendmail transport
//! //!
@@ -151,6 +154,7 @@
//! [AWS-LC]: https://github.com/aws/aws-lc //! [AWS-LC]: https://github.com/aws/aws-lc
//! [`aws-lc-rs`]: https://crates.io/crates/aws-lc-rs //! [`aws-lc-rs`]: https://crates.io/crates/aws-lc-rs
//! [`ring`]: https://crates.io/crates/ring //! [`ring`]: https://crates.io/crates/ring
//! [`rustls-platform-verifier`]: https://crates.io/crates/rustls-platform-verifier
//! [`rustls-native-certs`]: https://crates.io/crates/rustls-native-certs //! [`rustls-native-certs`]: https://crates.io/crates/rustls-native-certs
//! [`webpki-roots`]: https://crates.io/crates/webpki-roots //! [`webpki-roots`]: https://crates.io/crates/webpki-roots
//! [Tokio 1.x]: https://docs.rs/tokio/1 //! [Tokio 1.x]: https://docs.rs/tokio/1
@@ -158,11 +162,12 @@
//! [mime 0.3]: https://docs.rs/mime/0.3 //! [mime 0.3]: https://docs.rs/mime/0.3
//! [DKIM]: https://datatracker.ietf.org/doc/html/rfc6376 //! [DKIM]: https://datatracker.ietf.org/doc/html/rfc6376
#![doc(html_root_url = "https://docs.rs/crate/lettre/0.11.14")] #![doc(html_root_url = "https://docs.rs/crate/lettre/0.11.18")]
#![doc(html_favicon_url = "https://lettre.rs/favicon.ico")] #![doc(html_favicon_url = "https://lettre.rs/favicon.ico")]
#![doc(html_logo_url = "https://avatars0.githubusercontent.com/u/15113230?v=4")] #![doc(html_logo_url = "https://avatars0.githubusercontent.com/u/15113230?v=4")]
#![forbid(unsafe_code)] #![forbid(unsafe_code)]
#![deny( #![deny(
unreachable_pub,
missing_copy_implementations, missing_copy_implementations,
trivial_casts, trivial_casts,
trivial_numeric_casts, trivial_numeric_casts,
@@ -207,12 +212,13 @@ mod compiletime_checks {
#[cfg(all( #[cfg(all(
feature = "rustls", feature = "rustls",
not(feature = "rustls-platform-verifier"),
not(feature = "rustls-native-certs"), not(feature = "rustls-native-certs"),
not(feature = "webpki-roots") not(feature = "webpki-roots")
))] ))]
compile_error!( compile_error!(
"feature `rustls` also requires either the `rustls-native-certs` or the `webpki-roots` feature to "feature `rustls` also requires either the `rustls-platform-verifier`, the `rustls-native-certs`
be enabled" or the `webpki-roots` feature to be enabled"
); );
#[cfg(all(feature = "native-tls", feature = "boring-tls"))] #[cfg(all(feature = "native-tls", feature = "boring-tls"))]

View File

@@ -16,7 +16,10 @@ enum Disposition {
/// File name /// File name
Attached(String), Attached(String),
/// Content id /// Content id
Inline(String), Inline {
content_id: String,
name: Option<String>,
},
} }
impl Attachment { impl Attachment {
@@ -81,7 +84,50 @@ impl Attachment {
/// ``` /// ```
pub fn new_inline(content_id: String) -> Self { pub fn new_inline(content_id: String) -> Self {
Attachment { Attachment {
disposition: Disposition::Inline(content_id), disposition: Disposition::Inline {
content_id,
name: None,
},
}
}
/// Create a new inline attachment giving it a name
///
/// This attachment should be displayed inline into the message
/// body:
///
/// ```html
/// <img src="cid:123">
/// ```
///
///
/// ```rust
/// # use std::error::Error;
/// use std::fs;
///
/// use lettre::message::{header::ContentType, Attachment};
///
/// # fn main() -> Result<(), Box<dyn Error>> {
/// let content_id = String::from("123");
/// let file_name = String::from("image.jpg");
/// # if false {
/// let filebody = fs::read(&file_name)?;
/// # }
/// # let filebody = fs::read("docs/lettre.png")?;
/// let content_type = ContentType::parse("image/jpeg").unwrap();
/// let attachment =
/// Attachment::new_inline_with_name(content_id, file_name).body(filebody, content_type);
///
/// // The image `attachment` will display inline into the email.
/// # Ok(())
/// # }
/// ```
pub fn new_inline_with_name(content_id: String, name: String) -> Self {
Attachment {
disposition: Disposition::Inline {
content_id,
name: Some(name),
},
} }
} }
@@ -95,9 +141,18 @@ impl Attachment {
Disposition::Attached(filename) => { Disposition::Attached(filename) => {
builder.header(header::ContentDisposition::attachment(&filename)) builder.header(header::ContentDisposition::attachment(&filename))
} }
Disposition::Inline(content_id) => builder Disposition::Inline {
content_id,
name: None,
} => builder
.header(header::ContentId::from(format!("<{content_id}>"))) .header(header::ContentId::from(format!("<{content_id}>")))
.header(header::ContentDisposition::inline()), .header(header::ContentDisposition::inline()),
Disposition::Inline {
content_id,
name: Some(name),
} => builder
.header(header::ContentId::from(format!("<{content_id}>")))
.header(header::ContentDisposition::inline_with_name(&name)),
}; };
builder = builder.header(content_type); builder = builder.header(content_type);
builder.body(content) builder.body(content)
@@ -142,4 +197,24 @@ mod tests {
) )
); );
} }
#[test]
fn attachment_inline_with_name() {
let id = String::from("id");
let name = String::from("test");
let part = super::Attachment::new_inline_with_name(id, name).body(
String::from("Hello world!"),
ContentType::parse("text/plain").unwrap(),
);
assert_eq!(
&String::from_utf8_lossy(&part.formatted()),
concat!(
"Content-ID: <id>\r\n",
"Content-Disposition: inline; filename=\"test\"\r\n",
"Content-Type: text/plain\r\n",
"Content-Transfer-Encoding: 7bit\r\n\r\n",
"Hello world!\r\n"
)
);
}
} }

View File

@@ -41,14 +41,14 @@ fn quoted_pair() -> impl Parser<char, char, Error = Cheap<char>> {
// FWS = ([*WSP CRLF] 1*WSP) / ; Folding white space // FWS = ([*WSP CRLF] 1*WSP) / ; Folding white space
// obs-FWS // obs-FWS
pub fn fws() -> impl Parser<char, Option<char>, Error = Cheap<char>> { pub(super) fn fws() -> impl Parser<char, Option<char>, Error = Cheap<char>> {
rfc2234::wsp() rfc2234::wsp()
.or_not() .or_not()
.then_ignore(rfc2234::wsp().ignored().repeated()) .then_ignore(rfc2234::wsp().ignored().repeated())
} }
// CFWS = *([FWS] comment) (([FWS] comment) / FWS) // CFWS = *([FWS] comment) (([FWS] comment) / FWS)
pub fn cfws() -> impl Parser<char, Option<char>, Error = Cheap<char>> { pub(super) fn cfws() -> impl Parser<char, Option<char>, Error = Cheap<char>> {
// TODO: comment are not currently supported, so for now a cfws is // TODO: comment are not currently supported, so for now a cfws is
// the same as a fws. // the same as a fws.
fws() fws()
@@ -106,12 +106,12 @@ pub(super) fn atom() -> impl Parser<char, Vec<char>, Error = Cheap<char>> {
} }
// dot-atom = [CFWS] dot-atom-text [CFWS] // dot-atom = [CFWS] dot-atom-text [CFWS]
pub fn dot_atom() -> impl Parser<char, Vec<char>, Error = Cheap<char>> { pub(super) fn dot_atom() -> impl Parser<char, Vec<char>, Error = Cheap<char>> {
cfws().chain(dot_atom_text()) cfws().chain(dot_atom_text())
} }
// dot-atom-text = 1*atext *("." 1*atext) // dot-atom-text = 1*atext *("." 1*atext)
pub fn dot_atom_text() -> impl Parser<char, Vec<char>, Error = Cheap<char>> { pub(super) fn dot_atom_text() -> impl Parser<char, Vec<char>, Error = Cheap<char>> {
atext().repeated().at_least(1).chain( atext().repeated().at_least(1).chain(
just('.') just('.')
.chain(atext().repeated().at_least(1)) .chain(atext().repeated().at_least(1))
@@ -204,7 +204,7 @@ pub(crate) fn mailbox_list(
// https://datatracker.ietf.org/doc/html/rfc2822#section-3.4.1 // https://datatracker.ietf.org/doc/html/rfc2822#section-3.4.1
// addr-spec = local-part "@" domain // addr-spec = local-part "@" domain
pub fn addr_spec() -> impl Parser<char, (String, String), Error = Cheap<char>> { pub(super) fn addr_spec() -> impl Parser<char, (String, String), Error = Cheap<char>> {
local_part() local_part()
.collect() .collect()
.then_ignore(just('@')) .then_ignore(just('@'))
@@ -212,12 +212,12 @@ pub fn addr_spec() -> impl Parser<char, (String, String), Error = Cheap<char>> {
} }
// local-part = dot-atom / quoted-string / obs-local-part // local-part = dot-atom / quoted-string / obs-local-part
pub fn local_part() -> impl Parser<char, Vec<char>, Error = Cheap<char>> { pub(super) fn local_part() -> impl Parser<char, Vec<char>, Error = Cheap<char>> {
choice((dot_atom(), quoted_string(), obs_local_part())) choice((dot_atom(), quoted_string(), obs_local_part()))
} }
// domain = dot-atom / domain-literal / obs-domain // domain = dot-atom / domain-literal / obs-domain
pub fn domain() -> impl Parser<char, Vec<char>, Error = Cheap<char>> { pub(super) fn domain() -> impl Parser<char, Vec<char>, Error = Cheap<char>> {
// NOTE: omitting domain-literal since it may never be used // NOTE: omitting domain-literal since it may never be used
choice((dot_atom(), obs_domain())) choice((dot_atom(), obs_domain()))
} }
@@ -240,11 +240,11 @@ fn obs_phrase() -> impl Parser<char, Vec<char>, Error = Cheap<char>> {
// https://datatracker.ietf.org/doc/html/rfc2822#section-4.4 // https://datatracker.ietf.org/doc/html/rfc2822#section-4.4
// obs-local-part = word *("." word) // obs-local-part = word *("." word)
pub fn obs_local_part() -> impl Parser<char, Vec<char>, Error = Cheap<char>> { pub(super) fn obs_local_part() -> impl Parser<char, Vec<char>, Error = Cheap<char>> {
word().chain(just('.').chain(word()).repeated().flatten()) word().chain(just('.').chain(word()).repeated().flatten())
} }
// obs-domain = atom *("." atom) // obs-domain = atom *("." atom)
pub fn obs_domain() -> impl Parser<char, Vec<char>, Error = Cheap<char>> { pub(super) fn obs_domain() -> impl Parser<char, Vec<char>, Error = Cheap<char>> {
atom().chain(just('.').chain(atom()).repeated().flatten()) atom().chain(just('.').chain(atom()).repeated().flatten())
} }

View File

@@ -1,6 +1,6 @@
use std::time::SystemTime; use std::time::SystemTime;
#[cfg(feature = "web")] #[cfg(all(feature = "web", target_arch = "wasm32"))]
pub(crate) fn now() -> SystemTime { pub(crate) fn now() -> SystemTime {
fn to_std_systemtime(time: web_time::SystemTime) -> std::time::SystemTime { fn to_std_systemtime(time: web_time::SystemTime) -> std::time::SystemTime {
let duration = time let duration = time
@@ -18,7 +18,7 @@ pub(crate) fn now() -> SystemTime {
to_std_systemtime(web_time::SystemTime::now()) to_std_systemtime(web_time::SystemTime::now())
} }
#[cfg(not(feature = "web"))] #[cfg(not(all(feature = "web", target_arch = "wasm32")))]
pub(crate) fn now() -> SystemTime { pub(crate) fn now() -> SystemTime {
// FIXME: change to #[expect(clippy::disallowed_methods, reason = "the `web` feature is disabled")] // FIXME: change to #[expect(clippy::disallowed_methods, reason = "the `web` feature is disabled")]
#[allow(clippy::disallowed_methods)] #[allow(clippy::disallowed_methods)]

View File

@@ -34,6 +34,7 @@ impl Error {
/// Returns true if the error is an envelope serialization or deserialization error /// Returns true if the error is an envelope serialization or deserialization error
#[cfg(feature = "file-transport-envelope")] #[cfg(feature = "file-transport-envelope")]
#[cfg_attr(docsrs, doc(cfg(feature = "file-transport-envelope")))]
pub fn is_envelope(&self) -> bool { pub fn is_envelope(&self) -> bool {
matches!(self.inner.kind, Kind::Envelope) matches!(self.inner.kind, Kind::Envelope)
} }

View File

@@ -173,7 +173,7 @@ pub struct FileTransport {
} }
/// Asynchronously writes the content and the envelope information to a file /// Asynchronously writes the content and the envelope information to a file
#[derive(Debug, Clone)] #[derive(Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(docsrs, doc(cfg(any(feature = "tokio1", feature = "async-std1"))))] #[cfg_attr(docsrs, doc(cfg(any(feature = "tokio1", feature = "async-std1"))))]
#[cfg(any(feature = "async-std1", feature = "tokio1"))] #[cfg(any(feature = "async-std1", feature = "tokio1"))]
@@ -199,6 +199,7 @@ impl FileTransport {
/// Writes the email content in eml format and the envelope /// Writes the email content in eml format and the envelope
/// in json format. /// in json format.
#[cfg(feature = "file-transport-envelope")] #[cfg(feature = "file-transport-envelope")]
#[cfg_attr(docsrs, doc(cfg(feature = "file-transport-envelope")))]
pub fn with_envelope<P: AsRef<Path>>(path: P) -> FileTransport { pub fn with_envelope<P: AsRef<Path>>(path: P) -> FileTransport {
FileTransport { FileTransport {
path: PathBuf::from(path.as_ref()), path: PathBuf::from(path.as_ref()),
@@ -211,6 +212,7 @@ impl FileTransport {
/// ///
/// Reads the envelope and the raw message content. /// Reads the envelope and the raw message content.
#[cfg(feature = "file-transport-envelope")] #[cfg(feature = "file-transport-envelope")]
#[cfg_attr(docsrs, doc(cfg(feature = "file-transport-envelope")))]
pub fn read(&self, email_id: &str) -> Result<(Envelope, Vec<u8>), Error> { pub fn read(&self, email_id: &str) -> Result<(Envelope, Vec<u8>), Error> {
use std::fs; use std::fs;
@@ -249,6 +251,7 @@ where
/// Writes the email content in eml format and the envelope /// Writes the email content in eml format and the envelope
/// in json format. /// in json format.
#[cfg(feature = "file-transport-envelope")] #[cfg(feature = "file-transport-envelope")]
#[cfg_attr(docsrs, doc(cfg(feature = "file-transport-envelope")))]
pub fn with_envelope<P: AsRef<Path>>(path: P) -> Self { pub fn with_envelope<P: AsRef<Path>>(path: P) -> Self {
Self { Self {
inner: FileTransport::with_envelope(path), inner: FileTransport::with_envelope(path),
@@ -260,6 +263,7 @@ where
/// ///
/// Reads the envelope and the raw message content. /// Reads the envelope and the raw message content.
#[cfg(feature = "file-transport-envelope")] #[cfg(feature = "file-transport-envelope")]
#[cfg_attr(docsrs, doc(cfg(feature = "file-transport-envelope")))]
pub async fn read(&self, email_id: &str) -> Result<(Envelope, Vec<u8>), Error> { pub async fn read(&self, email_id: &str) -> Result<(Envelope, Vec<u8>), Error> {
let eml_file = self.inner.path.join(format!("{email_id}.eml")); let eml_file = self.inner.path.join(format!("{email_id}.eml"));
let eml = E::fs_read(&eml_file).await.map_err(error::io)?; let eml = E::fs_read(&eml_file).await.map_err(error::io)?;
@@ -272,6 +276,16 @@ where
} }
} }
#[cfg(any(feature = "async-std1", feature = "tokio1"))]
impl<E: Executor> Clone for AsyncFileTransport<E> {
fn clone(&self) -> Self {
Self {
inner: self.inner.clone(),
marker_: PhantomData,
}
}
}
impl Transport for FileTransport { impl Transport for FileTransport {
type Ok = Id; type Ok = Id;
type Error = Error; type Error = Error;

View File

@@ -140,6 +140,10 @@ pub trait Transport {
} }
fn send_raw(&self, envelope: &Envelope, email: &[u8]) -> Result<Self::Ok, Self::Error>; fn send_raw(&self, envelope: &Envelope, email: &[u8]) -> Result<Self::Ok, Self::Error>;
/// Shuts down the transport. Future calls to [`Self::send`] and
/// [`Self::send_raw`] might fail.
fn shutdown(&self) {}
} }
/// Async Transport method for emails /// Async Transport method for emails
@@ -166,4 +170,8 @@ pub trait AsyncTransport {
} }
async fn send_raw(&self, envelope: &Envelope, email: &[u8]) -> Result<Self::Ok, Self::Error>; async fn send_raw(&self, envelope: &Envelope, email: &[u8]) -> Result<Self::Ok, Self::Error>;
/// Shuts down the transport. Future calls to [`Self::send`] and
/// [`Self::send_raw`] might fail.
async fn shutdown(&self) {}
} }

View File

@@ -79,6 +79,11 @@ impl AsyncTransport for AsyncSmtpTransport<Tokio1Executor> {
Ok(result) Ok(result)
} }
async fn shutdown(&self) {
#[cfg(feature = "pool")]
self.inner.shutdown().await;
}
} }
#[cfg(feature = "async-std1")] #[cfg(feature = "async-std1")]
@@ -97,6 +102,11 @@ impl AsyncTransport for AsyncSmtpTransport<AsyncStd1Executor> {
Ok(result) Ok(result)
} }
async fn shutdown(&self) {
#[cfg(feature = "pool")]
self.inner.shutdown().await;
}
} }
impl<E> AsyncSmtpTransport<E> impl<E> AsyncSmtpTransport<E>
@@ -224,7 +234,7 @@ where
/// a proper URL encoder, like the following cargo script: /// a proper URL encoder, like the following cargo script:
/// ///
/// ```rust /// ```rust
/// # let _ = r#" /// # const TOML: &str = r#"
/// #!/usr/bin/env cargo /// #!/usr/bin/env cargo
/// ///
/// //! ```cargo /// //! ```cargo
@@ -460,7 +470,7 @@ impl AsyncSmtpTransportBuilder {
} }
/// Build client /// Build client
pub struct AsyncSmtpClient<E> { pub(super) struct AsyncSmtpClient<E> {
info: SmtpInfo, info: SmtpInfo,
marker_: PhantomData<E>, marker_: PhantomData<E>,
} }
@@ -472,7 +482,7 @@ where
/// Creates a new connection directly usable to send emails /// Creates a new connection directly usable to send emails
/// ///
/// Handles encryption and authentication /// Handles encryption and authentication
pub async fn connection(&self) -> Result<AsyncSmtpConnection, Error> { pub(super) async fn connection(&self) -> Result<AsyncSmtpConnection, Error> {
let mut conn = E::connect( let mut conn = E::connect(
&self.info.server, &self.info.server,
self.info.port, self.info.port,

View File

@@ -54,6 +54,7 @@ impl AsyncSmtpConnection {
/// ///
/// Sends EHLO and parses server information /// Sends EHLO and parses server information
#[cfg(feature = "tokio1")] #[cfg(feature = "tokio1")]
#[cfg_attr(docsrs, doc(cfg(feature = "tokio1")))]
pub async fn connect_with_transport( pub async fn connect_with_transport(
stream: Box<dyn AsyncTokioStream>, stream: Box<dyn AsyncTokioStream>,
hello_name: &ClientId, hello_name: &ClientId,
@@ -94,6 +95,7 @@ impl AsyncSmtpConnection {
/// # } /// # }
/// ``` /// ```
#[cfg(feature = "tokio1")] #[cfg(feature = "tokio1")]
#[cfg_attr(docsrs, doc(cfg(feature = "tokio1")))]
pub async fn connect_tokio1<T: tokio1_crate::net::ToSocketAddrs>( pub async fn connect_tokio1<T: tokio1_crate::net::ToSocketAddrs>(
server: T, server: T,
timeout: Option<Duration>, timeout: Option<Duration>,
@@ -112,6 +114,7 @@ impl AsyncSmtpConnection {
/// ///
/// Sends EHLO and parses server information /// Sends EHLO and parses server information
#[cfg(feature = "async-std1")] #[cfg(feature = "async-std1")]
#[cfg_attr(docsrs, doc(cfg(feature = "async-std1")))]
pub async fn connect_asyncstd1<T: async_std::net::ToSocketAddrs>( pub async fn connect_asyncstd1<T: async_std::net::ToSocketAddrs>(
server: T, server: T,
timeout: Option<Duration>, timeout: Option<Duration>,
@@ -376,6 +379,10 @@ impl AsyncSmtpConnection {
/// The X509 certificate of the server (DER encoded) /// The X509 certificate of the server (DER encoded)
#[cfg(any(feature = "native-tls", feature = "rustls", feature = "boring-tls"))] #[cfg(any(feature = "native-tls", feature = "rustls", feature = "boring-tls"))]
#[cfg_attr(
docsrs,
doc(cfg(any(feature = "native-tls", feature = "rustls", feature = "boring-tls")))
)]
pub fn peer_certificate(&self) -> Result<Vec<u8>, Error> { pub fn peer_certificate(&self) -> Result<Vec<u8>, Error> {
self.stream.get_ref().peer_certificate() self.stream.get_ref().peer_certificate()
} }
@@ -392,12 +399,14 @@ impl AsyncSmtpConnection {
/// as the TLSA records match the leaf or issuer certificates. /// as the TLSA records match the leaf or issuer certificates.
/// It cannot be called on non Boring TLS streams. /// It cannot be called on non Boring TLS streams.
#[cfg(feature = "boring-tls")] #[cfg(feature = "boring-tls")]
#[cfg_attr(docsrs, doc(cfg(feature = "boring-tls")))]
pub fn tls_verify_result(&self) -> Result<(), Error> { pub fn tls_verify_result(&self) -> Result<(), Error> {
self.stream.get_ref().tls_verify_result() self.stream.get_ref().tls_verify_result()
} }
/// All the X509 certificates of the chain (DER encoded) /// All the X509 certificates of the chain (DER encoded)
#[cfg(any(feature = "rustls", feature = "boring-tls"))] #[cfg(any(feature = "rustls", feature = "boring-tls"))]
#[cfg_attr(docsrs, doc(cfg(any(feature = "rustls", feature = "boring-tls"))))]
pub fn certificate_chain(&self) -> Result<Vec<Vec<u8>>, Error> { pub fn certificate_chain(&self) -> Result<Vec<Vec<u8>>, Error> {
self.stream.get_ref().certificate_chain() self.stream.get_ref().certificate_chain()
} }

View File

@@ -9,11 +9,11 @@ use std::{
#[cfg(feature = "async-std1")] #[cfg(feature = "async-std1")]
use async_std::net::{TcpStream as AsyncStd1TcpStream, ToSocketAddrs as AsyncStd1ToSocketAddrs}; use async_std::net::{TcpStream as AsyncStd1TcpStream, ToSocketAddrs as AsyncStd1ToSocketAddrs};
use futures_io::{ use futures_io::{
AsyncRead as FuturesAsyncRead, AsyncWrite as FuturesAsyncWrite, Error as IoError, ErrorKind, AsyncRead as FuturesAsyncRead, AsyncWrite as FuturesAsyncWrite, Error as IoError,
Result as IoResult, Result as IoResult,
}; };
#[cfg(feature = "async-std1-rustls")] #[cfg(feature = "async-std1-rustls")]
use futures_rustls::client::TlsStream as AsyncStd1RustlsTlsStream; use futures_rustls::client::TlsStream as AsyncStd1RustlsStream;
#[cfg(any(feature = "tokio1-rustls", feature = "async-std1-rustls"))] #[cfg(any(feature = "tokio1-rustls", feature = "async-std1-rustls"))]
use rustls::pki_types::ServerName; use rustls::pki_types::ServerName;
#[cfg(feature = "tokio1-boring-tls")] #[cfg(feature = "tokio1-boring-tls")]
@@ -28,7 +28,7 @@ use tokio1_crate::net::{
#[cfg(feature = "tokio1-native-tls")] #[cfg(feature = "tokio1-native-tls")]
use tokio1_native_tls_crate::TlsStream as Tokio1TlsStream; use tokio1_native_tls_crate::TlsStream as Tokio1TlsStream;
#[cfg(feature = "tokio1-rustls")] #[cfg(feature = "tokio1-rustls")]
use tokio1_rustls::client::TlsStream as Tokio1RustlsTlsStream; use tokio1_rustls::client::TlsStream as Tokio1RustlsStream;
#[cfg(any( #[cfg(any(
feature = "tokio1-native-tls", feature = "tokio1-native-tls",
@@ -79,7 +79,7 @@ enum InnerAsyncNetworkStream {
Tokio1NativeTls(Tokio1TlsStream<Box<dyn AsyncTokioStream>>), Tokio1NativeTls(Tokio1TlsStream<Box<dyn AsyncTokioStream>>),
/// Encrypted Tokio 1.x TCP stream /// Encrypted Tokio 1.x TCP stream
#[cfg(feature = "tokio1-rustls")] #[cfg(feature = "tokio1-rustls")]
Tokio1RustlsTls(Tokio1RustlsTlsStream<Box<dyn AsyncTokioStream>>), Tokio1Rustls(Tokio1RustlsStream<Box<dyn AsyncTokioStream>>),
/// Encrypted Tokio 1.x TCP stream /// Encrypted Tokio 1.x TCP stream
#[cfg(feature = "tokio1-boring-tls")] #[cfg(feature = "tokio1-boring-tls")]
Tokio1BoringTls(Tokio1SslStream<Box<dyn AsyncTokioStream>>), Tokio1BoringTls(Tokio1SslStream<Box<dyn AsyncTokioStream>>),
@@ -88,7 +88,7 @@ enum InnerAsyncNetworkStream {
AsyncStd1Tcp(AsyncStd1TcpStream), AsyncStd1Tcp(AsyncStd1TcpStream),
/// Encrypted Tokio 1.x TCP stream /// Encrypted Tokio 1.x TCP stream
#[cfg(feature = "async-std1-rustls")] #[cfg(feature = "async-std1-rustls")]
AsyncStd1RustlsTls(AsyncStd1RustlsTlsStream<AsyncStd1TcpStream>), AsyncStd1Rustls(AsyncStd1RustlsStream<AsyncStd1TcpStream>),
/// Can't be built /// Can't be built
None, None,
} }
@@ -113,17 +113,16 @@ impl AsyncNetworkStream {
s.get_ref().get_ref().get_ref().peer_addr() s.get_ref().get_ref().get_ref().peer_addr()
} }
#[cfg(feature = "tokio1-rustls")] #[cfg(feature = "tokio1-rustls")]
InnerAsyncNetworkStream::Tokio1RustlsTls(s) => s.get_ref().0.peer_addr(), InnerAsyncNetworkStream::Tokio1Rustls(s) => s.get_ref().0.peer_addr(),
#[cfg(feature = "tokio1-boring-tls")] #[cfg(feature = "tokio1-boring-tls")]
InnerAsyncNetworkStream::Tokio1BoringTls(s) => s.get_ref().peer_addr(), InnerAsyncNetworkStream::Tokio1BoringTls(s) => s.get_ref().peer_addr(),
#[cfg(feature = "async-std1")] #[cfg(feature = "async-std1")]
InnerAsyncNetworkStream::AsyncStd1Tcp(s) => s.peer_addr(), InnerAsyncNetworkStream::AsyncStd1Tcp(s) => s.peer_addr(),
#[cfg(feature = "async-std1-rustls")] #[cfg(feature = "async-std1-rustls")]
InnerAsyncNetworkStream::AsyncStd1RustlsTls(s) => s.get_ref().0.peer_addr(), InnerAsyncNetworkStream::AsyncStd1Rustls(s) => s.get_ref().0.peer_addr(),
InnerAsyncNetworkStream::None => { InnerAsyncNetworkStream::None => {
debug_assert!(false, "InnerAsyncNetworkStream::None must never be built"); debug_assert!(false, "InnerAsyncNetworkStream::None must never be built");
Err(IoError::new( Err(IoError::other(
ErrorKind::Other,
"InnerAsyncNetworkStream::None must never be built", "InnerAsyncNetworkStream::None must never be built",
)) ))
} }
@@ -131,11 +130,13 @@ impl AsyncNetworkStream {
} }
#[cfg(feature = "tokio1")] #[cfg(feature = "tokio1")]
#[cfg_attr(docsrs, doc(cfg(feature = "tokio1")))]
pub fn use_existing_tokio1(stream: Box<dyn AsyncTokioStream>) -> AsyncNetworkStream { pub fn use_existing_tokio1(stream: Box<dyn AsyncTokioStream>) -> AsyncNetworkStream {
AsyncNetworkStream::new(InnerAsyncNetworkStream::Tokio1Tcp(stream)) AsyncNetworkStream::new(InnerAsyncNetworkStream::Tokio1Tcp(stream))
} }
#[cfg(feature = "tokio1")] #[cfg(feature = "tokio1")]
#[cfg_attr(docsrs, doc(cfg(feature = "tokio1")))]
pub async fn connect_tokio1<T: Tokio1ToSocketAddrs>( pub async fn connect_tokio1<T: Tokio1ToSocketAddrs>(
server: T, server: T,
timeout: Option<Duration>, timeout: Option<Duration>,
@@ -202,6 +203,7 @@ impl AsyncNetworkStream {
} }
#[cfg(feature = "async-std1")] #[cfg(feature = "async-std1")]
#[cfg_attr(docsrs, doc(cfg(feature = "async-std1")))]
pub async fn connect_asyncstd1<T: AsyncStd1ToSocketAddrs>( pub async fn connect_asyncstd1<T: AsyncStd1ToSocketAddrs>(
server: T, server: T,
timeout: Option<Duration>, timeout: Option<Duration>,
@@ -321,7 +323,7 @@ impl AsyncNetworkStream {
match tls_parameters.connector { match tls_parameters.connector {
#[cfg(feature = "native-tls")] #[cfg(feature = "native-tls")]
InnerTlsParameters::NativeTls(connector) => { InnerTlsParameters::NativeTls { connector } => {
#[cfg(not(feature = "tokio1-native-tls"))] #[cfg(not(feature = "tokio1-native-tls"))]
panic!("built without the tokio1-native-tls feature"); panic!("built without the tokio1-native-tls feature");
@@ -338,7 +340,7 @@ impl AsyncNetworkStream {
}; };
} }
#[cfg(feature = "rustls")] #[cfg(feature = "rustls")]
InnerTlsParameters::RustlsTls(config) => { InnerTlsParameters::Rustls { config } => {
#[cfg(not(feature = "tokio1-rustls"))] #[cfg(not(feature = "tokio1-rustls"))]
panic!("built without the tokio1-rustls feature"); panic!("built without the tokio1-rustls feature");
@@ -354,18 +356,21 @@ impl AsyncNetworkStream {
.connect(domain.to_owned(), tcp_stream) .connect(domain.to_owned(), tcp_stream)
.await .await
.map_err(error::connection)?; .map_err(error::connection)?;
Ok(InnerAsyncNetworkStream::Tokio1RustlsTls(stream)) Ok(InnerAsyncNetworkStream::Tokio1Rustls(stream))
}; };
} }
#[cfg(feature = "boring-tls")] #[cfg(feature = "boring-tls")]
InnerTlsParameters::BoringTls(connector) => { InnerTlsParameters::BoringTls {
connector,
accept_invalid_hostnames,
} => {
#[cfg(not(feature = "tokio1-boring-tls"))] #[cfg(not(feature = "tokio1-boring-tls"))]
panic!("built without the tokio1-boring-tls feature"); panic!("built without the tokio1-boring-tls feature");
#[cfg(feature = "tokio1-boring-tls")] #[cfg(feature = "tokio1-boring-tls")]
return { return {
let mut config = connector.configure().map_err(error::connection)?; let mut config = connector.configure().map_err(error::connection)?;
config.set_verify_hostname(tls_parameters.accept_invalid_hostnames); config.set_verify_hostname(accept_invalid_hostnames);
let stream = tokio1_boring::connect(config, &domain, tcp_stream) let stream = tokio1_boring::connect(config, &domain, tcp_stream)
.await .await
@@ -386,11 +391,11 @@ impl AsyncNetworkStream {
match tls_parameters.connector { match tls_parameters.connector {
#[cfg(feature = "native-tls")] #[cfg(feature = "native-tls")]
InnerTlsParameters::NativeTls(connector) => { InnerTlsParameters::NativeTls { connector } => {
panic!("native-tls isn't supported with async-std yet. See https://github.com/lettre/lettre/pull/531#issuecomment-757893531"); panic!("native-tls isn't supported with async-std yet. See https://github.com/lettre/lettre/pull/531#issuecomment-757893531");
} }
#[cfg(feature = "rustls")] #[cfg(feature = "rustls")]
InnerTlsParameters::RustlsTls(config) => { InnerTlsParameters::Rustls { config } => {
#[cfg(not(feature = "async-std1-rustls"))] #[cfg(not(feature = "async-std1-rustls"))]
panic!("built without the async-std1-rustls feature"); panic!("built without the async-std1-rustls feature");
@@ -406,11 +411,11 @@ impl AsyncNetworkStream {
.connect(domain.to_owned(), tcp_stream) .connect(domain.to_owned(), tcp_stream)
.await .await
.map_err(error::connection)?; .map_err(error::connection)?;
Ok(InnerAsyncNetworkStream::AsyncStd1RustlsTls(stream)) Ok(InnerAsyncNetworkStream::AsyncStd1Rustls(stream))
}; };
} }
#[cfg(feature = "boring-tls")] #[cfg(feature = "boring-tls")]
InnerTlsParameters::BoringTls(connector) => { InnerTlsParameters::BoringTls { .. } => {
panic!("boring-tls isn't supported with async-std yet."); panic!("boring-tls isn't supported with async-std yet.");
} }
} }
@@ -423,18 +428,19 @@ impl AsyncNetworkStream {
#[cfg(feature = "tokio1-native-tls")] #[cfg(feature = "tokio1-native-tls")]
InnerAsyncNetworkStream::Tokio1NativeTls(_) => true, InnerAsyncNetworkStream::Tokio1NativeTls(_) => true,
#[cfg(feature = "tokio1-rustls")] #[cfg(feature = "tokio1-rustls")]
InnerAsyncNetworkStream::Tokio1RustlsTls(_) => true, InnerAsyncNetworkStream::Tokio1Rustls(_) => true,
#[cfg(feature = "tokio1-boring-tls")] #[cfg(feature = "tokio1-boring-tls")]
InnerAsyncNetworkStream::Tokio1BoringTls(_) => true, InnerAsyncNetworkStream::Tokio1BoringTls(_) => true,
#[cfg(feature = "async-std1")] #[cfg(feature = "async-std1")]
InnerAsyncNetworkStream::AsyncStd1Tcp(_) => false, InnerAsyncNetworkStream::AsyncStd1Tcp(_) => false,
#[cfg(feature = "async-std1-rustls")] #[cfg(feature = "async-std1-rustls")]
InnerAsyncNetworkStream::AsyncStd1RustlsTls(_) => true, InnerAsyncNetworkStream::AsyncStd1Rustls(_) => true,
InnerAsyncNetworkStream::None => false, InnerAsyncNetworkStream::None => false,
} }
} }
#[cfg(feature = "boring-tls")] #[cfg(feature = "boring-tls")]
#[cfg_attr(docsrs, doc(cfg(feature = "boring-tls")))]
pub fn tls_verify_result(&self) -> Result<(), Error> { pub fn tls_verify_result(&self) -> Result<(), Error> {
match &self.inner { match &self.inner {
#[cfg(feature = "tokio1")] #[cfg(feature = "tokio1")]
@@ -444,7 +450,7 @@ impl AsyncNetworkStream {
#[cfg(feature = "tokio1-native-tls")] #[cfg(feature = "tokio1-native-tls")]
InnerAsyncNetworkStream::Tokio1NativeTls(_) => panic!("Unsupported"), InnerAsyncNetworkStream::Tokio1NativeTls(_) => panic!("Unsupported"),
#[cfg(feature = "tokio1-rustls")] #[cfg(feature = "tokio1-rustls")]
InnerAsyncNetworkStream::Tokio1RustlsTls(_) => panic!("Unsupported"), InnerAsyncNetworkStream::Tokio1Rustls(_) => panic!("Unsupported"),
#[cfg(feature = "tokio1-boring-tls")] #[cfg(feature = "tokio1-boring-tls")]
InnerAsyncNetworkStream::Tokio1BoringTls(stream) => { InnerAsyncNetworkStream::Tokio1BoringTls(stream) => {
stream.ssl().verify_result().map_err(error::tls) stream.ssl().verify_result().map_err(error::tls)
@@ -454,10 +460,11 @@ impl AsyncNetworkStream {
Err(error::client("Connection is not encrypted")) Err(error::client("Connection is not encrypted"))
} }
#[cfg(feature = "async-std1-rustls")] #[cfg(feature = "async-std1-rustls")]
InnerAsyncNetworkStream::AsyncStd1RustlsTls(_) => panic!("Unsupported"), InnerAsyncNetworkStream::AsyncStd1Rustls(_) => panic!("Unsupported"),
InnerAsyncNetworkStream::None => panic!("InnerNetworkStream::None must never be built"), InnerAsyncNetworkStream::None => panic!("InnerNetworkStream::None must never be built"),
} }
} }
pub fn certificate_chain(&self) -> Result<Vec<Vec<u8>>, Error> { pub fn certificate_chain(&self) -> Result<Vec<Vec<u8>>, Error> {
match &self.inner { match &self.inner {
#[cfg(feature = "tokio1")] #[cfg(feature = "tokio1")]
@@ -467,7 +474,7 @@ impl AsyncNetworkStream {
#[cfg(feature = "tokio1-native-tls")] #[cfg(feature = "tokio1-native-tls")]
InnerAsyncNetworkStream::Tokio1NativeTls(_) => panic!("Unsupported"), InnerAsyncNetworkStream::Tokio1NativeTls(_) => panic!("Unsupported"),
#[cfg(feature = "tokio1-rustls")] #[cfg(feature = "tokio1-rustls")]
InnerAsyncNetworkStream::Tokio1RustlsTls(stream) => Ok(stream InnerAsyncNetworkStream::Tokio1Rustls(stream) => Ok(stream
.get_ref() .get_ref()
.1 .1
.peer_certificates() .peer_certificates()
@@ -488,7 +495,7 @@ impl AsyncNetworkStream {
Err(error::client("Connection is not encrypted")) Err(error::client("Connection is not encrypted"))
} }
#[cfg(feature = "async-std1-rustls")] #[cfg(feature = "async-std1-rustls")]
InnerAsyncNetworkStream::AsyncStd1RustlsTls(stream) => Ok(stream InnerAsyncNetworkStream::AsyncStd1Rustls(stream) => Ok(stream
.get_ref() .get_ref()
.1 .1
.peer_certificates() .peer_certificates()
@@ -515,7 +522,7 @@ impl AsyncNetworkStream {
.to_der() .to_der()
.map_err(error::tls)?), .map_err(error::tls)?),
#[cfg(feature = "tokio1-rustls")] #[cfg(feature = "tokio1-rustls")]
InnerAsyncNetworkStream::Tokio1RustlsTls(stream) => Ok(stream InnerAsyncNetworkStream::Tokio1Rustls(stream) => Ok(stream
.get_ref() .get_ref()
.1 .1
.peer_certificates() .peer_certificates()
@@ -535,7 +542,7 @@ impl AsyncNetworkStream {
Err(error::client("Connection is not encrypted")) Err(error::client("Connection is not encrypted"))
} }
#[cfg(feature = "async-std1-rustls")] #[cfg(feature = "async-std1-rustls")]
InnerAsyncNetworkStream::AsyncStd1RustlsTls(stream) => Ok(stream InnerAsyncNetworkStream::AsyncStd1Rustls(stream) => Ok(stream
.get_ref() .get_ref()
.1 .1
.peer_certificates() .peer_certificates()
@@ -575,7 +582,7 @@ impl FuturesAsyncRead for AsyncNetworkStream {
} }
} }
#[cfg(feature = "tokio1-rustls")] #[cfg(feature = "tokio1-rustls")]
InnerAsyncNetworkStream::Tokio1RustlsTls(s) => { InnerAsyncNetworkStream::Tokio1Rustls(s) => {
let mut b = Tokio1ReadBuf::new(buf); let mut b = Tokio1ReadBuf::new(buf);
match Pin::new(s).poll_read(cx, &mut b) { match Pin::new(s).poll_read(cx, &mut b) {
Poll::Ready(Ok(())) => Poll::Ready(Ok(b.filled().len())), Poll::Ready(Ok(())) => Poll::Ready(Ok(b.filled().len())),
@@ -595,7 +602,7 @@ impl FuturesAsyncRead for AsyncNetworkStream {
#[cfg(feature = "async-std1")] #[cfg(feature = "async-std1")]
InnerAsyncNetworkStream::AsyncStd1Tcp(s) => Pin::new(s).poll_read(cx, buf), InnerAsyncNetworkStream::AsyncStd1Tcp(s) => Pin::new(s).poll_read(cx, buf),
#[cfg(feature = "async-std1-rustls")] #[cfg(feature = "async-std1-rustls")]
InnerAsyncNetworkStream::AsyncStd1RustlsTls(s) => Pin::new(s).poll_read(cx, buf), InnerAsyncNetworkStream::AsyncStd1Rustls(s) => Pin::new(s).poll_read(cx, buf),
InnerAsyncNetworkStream::None => { InnerAsyncNetworkStream::None => {
debug_assert!(false, "InnerAsyncNetworkStream::None must never be built"); debug_assert!(false, "InnerAsyncNetworkStream::None must never be built");
Poll::Ready(Ok(0)) Poll::Ready(Ok(0))
@@ -617,13 +624,13 @@ impl FuturesAsyncWrite for AsyncNetworkStream {
#[cfg(feature = "tokio1-native-tls")] #[cfg(feature = "tokio1-native-tls")]
InnerAsyncNetworkStream::Tokio1NativeTls(s) => Pin::new(s).poll_write(cx, buf), InnerAsyncNetworkStream::Tokio1NativeTls(s) => Pin::new(s).poll_write(cx, buf),
#[cfg(feature = "tokio1-rustls")] #[cfg(feature = "tokio1-rustls")]
InnerAsyncNetworkStream::Tokio1RustlsTls(s) => Pin::new(s).poll_write(cx, buf), InnerAsyncNetworkStream::Tokio1Rustls(s) => Pin::new(s).poll_write(cx, buf),
#[cfg(feature = "tokio1-boring-tls")] #[cfg(feature = "tokio1-boring-tls")]
InnerAsyncNetworkStream::Tokio1BoringTls(s) => Pin::new(s).poll_write(cx, buf), InnerAsyncNetworkStream::Tokio1BoringTls(s) => Pin::new(s).poll_write(cx, buf),
#[cfg(feature = "async-std1")] #[cfg(feature = "async-std1")]
InnerAsyncNetworkStream::AsyncStd1Tcp(s) => Pin::new(s).poll_write(cx, buf), InnerAsyncNetworkStream::AsyncStd1Tcp(s) => Pin::new(s).poll_write(cx, buf),
#[cfg(feature = "async-std1-rustls")] #[cfg(feature = "async-std1-rustls")]
InnerAsyncNetworkStream::AsyncStd1RustlsTls(s) => Pin::new(s).poll_write(cx, buf), InnerAsyncNetworkStream::AsyncStd1Rustls(s) => Pin::new(s).poll_write(cx, buf),
InnerAsyncNetworkStream::None => { InnerAsyncNetworkStream::None => {
debug_assert!(false, "InnerAsyncNetworkStream::None must never be built"); debug_assert!(false, "InnerAsyncNetworkStream::None must never be built");
Poll::Ready(Ok(0)) Poll::Ready(Ok(0))
@@ -638,13 +645,13 @@ impl FuturesAsyncWrite for AsyncNetworkStream {
#[cfg(feature = "tokio1-native-tls")] #[cfg(feature = "tokio1-native-tls")]
InnerAsyncNetworkStream::Tokio1NativeTls(s) => Pin::new(s).poll_flush(cx), InnerAsyncNetworkStream::Tokio1NativeTls(s) => Pin::new(s).poll_flush(cx),
#[cfg(feature = "tokio1-rustls")] #[cfg(feature = "tokio1-rustls")]
InnerAsyncNetworkStream::Tokio1RustlsTls(s) => Pin::new(s).poll_flush(cx), InnerAsyncNetworkStream::Tokio1Rustls(s) => Pin::new(s).poll_flush(cx),
#[cfg(feature = "tokio1-boring-tls")] #[cfg(feature = "tokio1-boring-tls")]
InnerAsyncNetworkStream::Tokio1BoringTls(s) => Pin::new(s).poll_flush(cx), InnerAsyncNetworkStream::Tokio1BoringTls(s) => Pin::new(s).poll_flush(cx),
#[cfg(feature = "async-std1")] #[cfg(feature = "async-std1")]
InnerAsyncNetworkStream::AsyncStd1Tcp(s) => Pin::new(s).poll_flush(cx), InnerAsyncNetworkStream::AsyncStd1Tcp(s) => Pin::new(s).poll_flush(cx),
#[cfg(feature = "async-std1-rustls")] #[cfg(feature = "async-std1-rustls")]
InnerAsyncNetworkStream::AsyncStd1RustlsTls(s) => Pin::new(s).poll_flush(cx), InnerAsyncNetworkStream::AsyncStd1Rustls(s) => Pin::new(s).poll_flush(cx),
InnerAsyncNetworkStream::None => { InnerAsyncNetworkStream::None => {
debug_assert!(false, "InnerAsyncNetworkStream::None must never be built"); debug_assert!(false, "InnerAsyncNetworkStream::None must never be built");
Poll::Ready(Ok(())) Poll::Ready(Ok(()))
@@ -659,13 +666,13 @@ impl FuturesAsyncWrite for AsyncNetworkStream {
#[cfg(feature = "tokio1-native-tls")] #[cfg(feature = "tokio1-native-tls")]
InnerAsyncNetworkStream::Tokio1NativeTls(s) => Pin::new(s).poll_shutdown(cx), InnerAsyncNetworkStream::Tokio1NativeTls(s) => Pin::new(s).poll_shutdown(cx),
#[cfg(feature = "tokio1-rustls")] #[cfg(feature = "tokio1-rustls")]
InnerAsyncNetworkStream::Tokio1RustlsTls(s) => Pin::new(s).poll_shutdown(cx), InnerAsyncNetworkStream::Tokio1Rustls(s) => Pin::new(s).poll_shutdown(cx),
#[cfg(feature = "tokio1-boring-tls")] #[cfg(feature = "tokio1-boring-tls")]
InnerAsyncNetworkStream::Tokio1BoringTls(s) => Pin::new(s).poll_shutdown(cx), InnerAsyncNetworkStream::Tokio1BoringTls(s) => Pin::new(s).poll_shutdown(cx),
#[cfg(feature = "async-std1")] #[cfg(feature = "async-std1")]
InnerAsyncNetworkStream::AsyncStd1Tcp(s) => Pin::new(s).poll_close(cx), InnerAsyncNetworkStream::AsyncStd1Tcp(s) => Pin::new(s).poll_close(cx),
#[cfg(feature = "async-std1-rustls")] #[cfg(feature = "async-std1-rustls")]
InnerAsyncNetworkStream::AsyncStd1RustlsTls(s) => Pin::new(s).poll_close(cx), InnerAsyncNetworkStream::AsyncStd1Rustls(s) => Pin::new(s).poll_close(cx),
InnerAsyncNetworkStream::None => { InnerAsyncNetworkStream::None => {
debug_assert!(false, "InnerAsyncNetworkStream::None must never be built"); debug_assert!(false, "InnerAsyncNetworkStream::None must never be built");
Poll::Ready(Ok(())) Poll::Ready(Ok(()))

View File

@@ -300,6 +300,10 @@ impl SmtpConnection {
/// The X509 certificate of the server (DER encoded) /// The X509 certificate of the server (DER encoded)
#[cfg(any(feature = "native-tls", feature = "rustls", feature = "boring-tls"))] #[cfg(any(feature = "native-tls", feature = "rustls", feature = "boring-tls"))]
#[cfg_attr(
docsrs,
doc(cfg(any(feature = "native-tls", feature = "rustls", feature = "boring-tls")))
)]
pub fn peer_certificate(&self) -> Result<Vec<u8>, Error> { pub fn peer_certificate(&self) -> Result<Vec<u8>, Error> {
self.stream.get_ref().peer_certificate() self.stream.get_ref().peer_certificate()
} }
@@ -316,12 +320,14 @@ impl SmtpConnection {
/// as the TLSA records match the leaf or issuer certificates. /// as the TLSA records match the leaf or issuer certificates.
/// It cannot be called on non Boring TLS streams. /// It cannot be called on non Boring TLS streams.
#[cfg(feature = "boring-tls")] #[cfg(feature = "boring-tls")]
#[cfg_attr(docsrs, doc(cfg(feature = "boring-tls")))]
pub fn tls_verify_result(&self) -> Result<(), Error> { pub fn tls_verify_result(&self) -> Result<(), Error> {
self.stream.get_ref().tls_verify_result() self.stream.get_ref().tls_verify_result()
} }
/// All the X509 certificates of the chain (DER encoded) /// All the X509 certificates of the chain (DER encoded)
#[cfg(any(feature = "rustls", feature = "boring-tls"))] #[cfg(any(feature = "rustls", feature = "boring-tls"))]
#[cfg_attr(docsrs, doc(cfg(any(feature = "rustls", feature = "boring-tls"))))]
pub fn certificate_chain(&self) -> Result<Vec<Vec<u8>>, Error> { pub fn certificate_chain(&self) -> Result<Vec<Vec<u8>>, Error> {
self.stream.get_ref().certificate_chain() self.stream.get_ref().certificate_chain()
} }

View File

@@ -58,7 +58,7 @@ struct ClientCodec {
impl ClientCodec { impl ClientCodec {
/// Creates a new client codec /// Creates a new client codec
pub fn new() -> Self { pub(crate) fn new() -> Self {
Self { Self {
status: CodecStatus::StartOfNewLine, status: CodecStatus::StartOfNewLine,
} }

View File

@@ -37,7 +37,7 @@ enum InnerNetworkStream {
NativeTls(TlsStream<TcpStream>), NativeTls(TlsStream<TcpStream>),
/// Encrypted TCP stream /// Encrypted TCP stream
#[cfg(feature = "rustls")] #[cfg(feature = "rustls")]
RustlsTls(StreamOwned<ClientConnection, TcpStream>), Rustls(StreamOwned<ClientConnection, TcpStream>),
#[cfg(feature = "boring-tls")] #[cfg(feature = "boring-tls")]
BoringTls(SslStream<TcpStream>), BoringTls(SslStream<TcpStream>),
/// Can't be built /// Can't be built
@@ -60,7 +60,7 @@ impl NetworkStream {
#[cfg(feature = "native-tls")] #[cfg(feature = "native-tls")]
InnerNetworkStream::NativeTls(s) => s.get_ref().peer_addr(), InnerNetworkStream::NativeTls(s) => s.get_ref().peer_addr(),
#[cfg(feature = "rustls")] #[cfg(feature = "rustls")]
InnerNetworkStream::RustlsTls(s) => s.get_ref().peer_addr(), InnerNetworkStream::Rustls(s) => s.get_ref().peer_addr(),
#[cfg(feature = "boring-tls")] #[cfg(feature = "boring-tls")]
InnerNetworkStream::BoringTls(s) => s.get_ref().peer_addr(), InnerNetworkStream::BoringTls(s) => s.get_ref().peer_addr(),
InnerNetworkStream::None => { InnerNetworkStream::None => {
@@ -80,7 +80,7 @@ impl NetworkStream {
#[cfg(feature = "native-tls")] #[cfg(feature = "native-tls")]
InnerNetworkStream::NativeTls(s) => s.get_ref().shutdown(how), InnerNetworkStream::NativeTls(s) => s.get_ref().shutdown(how),
#[cfg(feature = "rustls")] #[cfg(feature = "rustls")]
InnerNetworkStream::RustlsTls(s) => s.get_ref().shutdown(how), InnerNetworkStream::Rustls(s) => s.get_ref().shutdown(how),
#[cfg(feature = "boring-tls")] #[cfg(feature = "boring-tls")]
InnerNetworkStream::BoringTls(s) => s.get_ref().shutdown(how), InnerNetworkStream::BoringTls(s) => s.get_ref().shutdown(how),
InnerNetworkStream::None => { InnerNetworkStream::None => {
@@ -174,27 +174,30 @@ impl NetworkStream {
) -> Result<InnerNetworkStream, Error> { ) -> Result<InnerNetworkStream, Error> {
Ok(match &tls_parameters.connector { Ok(match &tls_parameters.connector {
#[cfg(feature = "native-tls")] #[cfg(feature = "native-tls")]
InnerTlsParameters::NativeTls(connector) => { InnerTlsParameters::NativeTls { connector } => {
let stream = connector let stream = connector
.connect(tls_parameters.domain(), tcp_stream) .connect(tls_parameters.domain(), tcp_stream)
.map_err(error::connection)?; .map_err(error::connection)?;
InnerNetworkStream::NativeTls(stream) InnerNetworkStream::NativeTls(stream)
} }
#[cfg(feature = "rustls")] #[cfg(feature = "rustls")]
InnerTlsParameters::RustlsTls(connector) => { InnerTlsParameters::Rustls { config } => {
let domain = ServerName::try_from(tls_parameters.domain()) let domain = ServerName::try_from(tls_parameters.domain())
.map_err(|_| error::connection("domain isn't a valid DNS name"))?; .map_err(|_| error::connection("domain isn't a valid DNS name"))?;
let connection = ClientConnection::new(Arc::clone(connector), domain.to_owned()) let connection = ClientConnection::new(Arc::clone(config), domain.to_owned())
.map_err(error::connection)?; .map_err(error::connection)?;
let stream = StreamOwned::new(connection, tcp_stream); let stream = StreamOwned::new(connection, tcp_stream);
InnerNetworkStream::RustlsTls(stream) InnerNetworkStream::Rustls(stream)
} }
#[cfg(feature = "boring-tls")] #[cfg(feature = "boring-tls")]
InnerTlsParameters::BoringTls(connector) => { InnerTlsParameters::BoringTls {
connector,
accept_invalid_hostnames,
} => {
let stream = connector let stream = connector
.configure() .configure()
.map_err(error::connection)? .map_err(error::connection)?
.verify_hostname(tls_parameters.accept_invalid_hostnames) .verify_hostname(*accept_invalid_hostnames)
.connect(tls_parameters.domain(), tcp_stream) .connect(tls_parameters.domain(), tcp_stream)
.map_err(error::connection)?; .map_err(error::connection)?;
InnerNetworkStream::BoringTls(stream) InnerNetworkStream::BoringTls(stream)
@@ -208,7 +211,7 @@ impl NetworkStream {
#[cfg(feature = "native-tls")] #[cfg(feature = "native-tls")]
InnerNetworkStream::NativeTls(_) => true, InnerNetworkStream::NativeTls(_) => true,
#[cfg(feature = "rustls")] #[cfg(feature = "rustls")]
InnerNetworkStream::RustlsTls(_) => true, InnerNetworkStream::Rustls(_) => true,
#[cfg(feature = "boring-tls")] #[cfg(feature = "boring-tls")]
InnerNetworkStream::BoringTls(_) => true, InnerNetworkStream::BoringTls(_) => true,
InnerNetworkStream::None => { InnerNetworkStream::None => {
@@ -219,13 +222,14 @@ impl NetworkStream {
} }
#[cfg(feature = "boring-tls")] #[cfg(feature = "boring-tls")]
#[cfg_attr(docsrs, doc(cfg(feature = "boring-tls")))]
pub fn tls_verify_result(&self) -> Result<(), Error> { pub fn tls_verify_result(&self) -> Result<(), Error> {
match &self.inner { match &self.inner {
InnerNetworkStream::Tcp(_) => Err(error::client("Connection is not encrypted")), InnerNetworkStream::Tcp(_) => Err(error::client("Connection is not encrypted")),
#[cfg(feature = "native-tls")] #[cfg(feature = "native-tls")]
InnerNetworkStream::NativeTls(_) => panic!("Unsupported"), InnerNetworkStream::NativeTls(_) => panic!("Unsupported"),
#[cfg(feature = "rustls")] #[cfg(feature = "rustls")]
InnerNetworkStream::RustlsTls(_) => panic!("Unsupported"), InnerNetworkStream::Rustls(_) => panic!("Unsupported"),
#[cfg(feature = "boring-tls")] #[cfg(feature = "boring-tls")]
InnerNetworkStream::BoringTls(stream) => { InnerNetworkStream::BoringTls(stream) => {
stream.ssl().verify_result().map_err(error::tls) stream.ssl().verify_result().map_err(error::tls)
@@ -235,13 +239,14 @@ impl NetworkStream {
} }
#[cfg(any(feature = "rustls", feature = "boring-tls"))] #[cfg(any(feature = "rustls", feature = "boring-tls"))]
#[cfg_attr(docsrs, doc(cfg(any(feature = "rustls", feature = "boring-tls"))))]
pub fn certificate_chain(&self) -> Result<Vec<Vec<u8>>, Error> { pub fn certificate_chain(&self) -> Result<Vec<Vec<u8>>, Error> {
match &self.inner { match &self.inner {
InnerNetworkStream::Tcp(_) => Err(error::client("Connection is not encrypted")), InnerNetworkStream::Tcp(_) => Err(error::client("Connection is not encrypted")),
#[cfg(feature = "native-tls")] #[cfg(feature = "native-tls")]
InnerNetworkStream::NativeTls(_) => panic!("Unsupported"), InnerNetworkStream::NativeTls(_) => panic!("Unsupported"),
#[cfg(feature = "rustls")] #[cfg(feature = "rustls")]
InnerNetworkStream::RustlsTls(stream) => Ok(stream InnerNetworkStream::Rustls(stream) => Ok(stream
.conn .conn
.peer_certificates() .peer_certificates()
.unwrap() .unwrap()
@@ -261,6 +266,10 @@ impl NetworkStream {
} }
#[cfg(any(feature = "native-tls", feature = "rustls", feature = "boring-tls"))] #[cfg(any(feature = "native-tls", feature = "rustls", feature = "boring-tls"))]
#[cfg_attr(
docsrs,
doc(cfg(any(feature = "native-tls", feature = "rustls", feature = "boring-tls")))
)]
pub fn peer_certificate(&self) -> Result<Vec<u8>, Error> { pub fn peer_certificate(&self) -> Result<Vec<u8>, Error> {
match &self.inner { match &self.inner {
InnerNetworkStream::Tcp(_) => Err(error::client("Connection is not encrypted")), InnerNetworkStream::Tcp(_) => Err(error::client("Connection is not encrypted")),
@@ -272,7 +281,7 @@ impl NetworkStream {
.to_der() .to_der()
.map_err(error::tls)?), .map_err(error::tls)?),
#[cfg(feature = "rustls")] #[cfg(feature = "rustls")]
InnerNetworkStream::RustlsTls(stream) => Ok(stream InnerNetworkStream::Rustls(stream) => Ok(stream
.conn .conn
.peer_certificates() .peer_certificates()
.unwrap() .unwrap()
@@ -296,7 +305,7 @@ impl NetworkStream {
#[cfg(feature = "native-tls")] #[cfg(feature = "native-tls")]
InnerNetworkStream::NativeTls(stream) => stream.get_ref().set_read_timeout(duration), InnerNetworkStream::NativeTls(stream) => stream.get_ref().set_read_timeout(duration),
#[cfg(feature = "rustls")] #[cfg(feature = "rustls")]
InnerNetworkStream::RustlsTls(stream) => stream.get_ref().set_read_timeout(duration), InnerNetworkStream::Rustls(stream) => stream.get_ref().set_read_timeout(duration),
#[cfg(feature = "boring-tls")] #[cfg(feature = "boring-tls")]
InnerNetworkStream::BoringTls(stream) => stream.get_ref().set_read_timeout(duration), InnerNetworkStream::BoringTls(stream) => stream.get_ref().set_read_timeout(duration),
InnerNetworkStream::None => { InnerNetworkStream::None => {
@@ -314,7 +323,7 @@ impl NetworkStream {
#[cfg(feature = "native-tls")] #[cfg(feature = "native-tls")]
InnerNetworkStream::NativeTls(stream) => stream.get_ref().set_write_timeout(duration), InnerNetworkStream::NativeTls(stream) => stream.get_ref().set_write_timeout(duration),
#[cfg(feature = "rustls")] #[cfg(feature = "rustls")]
InnerNetworkStream::RustlsTls(stream) => stream.get_ref().set_write_timeout(duration), InnerNetworkStream::Rustls(stream) => stream.get_ref().set_write_timeout(duration),
#[cfg(feature = "boring-tls")] #[cfg(feature = "boring-tls")]
InnerNetworkStream::BoringTls(stream) => stream.get_ref().set_write_timeout(duration), InnerNetworkStream::BoringTls(stream) => stream.get_ref().set_write_timeout(duration),
InnerNetworkStream::None => { InnerNetworkStream::None => {
@@ -332,7 +341,7 @@ impl Read for NetworkStream {
#[cfg(feature = "native-tls")] #[cfg(feature = "native-tls")]
InnerNetworkStream::NativeTls(s) => s.read(buf), InnerNetworkStream::NativeTls(s) => s.read(buf),
#[cfg(feature = "rustls")] #[cfg(feature = "rustls")]
InnerNetworkStream::RustlsTls(s) => s.read(buf), InnerNetworkStream::Rustls(s) => s.read(buf),
#[cfg(feature = "boring-tls")] #[cfg(feature = "boring-tls")]
InnerNetworkStream::BoringTls(s) => s.read(buf), InnerNetworkStream::BoringTls(s) => s.read(buf),
InnerNetworkStream::None => { InnerNetworkStream::None => {
@@ -350,7 +359,7 @@ impl Write for NetworkStream {
#[cfg(feature = "native-tls")] #[cfg(feature = "native-tls")]
InnerNetworkStream::NativeTls(s) => s.write(buf), InnerNetworkStream::NativeTls(s) => s.write(buf),
#[cfg(feature = "rustls")] #[cfg(feature = "rustls")]
InnerNetworkStream::RustlsTls(s) => s.write(buf), InnerNetworkStream::Rustls(s) => s.write(buf),
#[cfg(feature = "boring-tls")] #[cfg(feature = "boring-tls")]
InnerNetworkStream::BoringTls(s) => s.write(buf), InnerNetworkStream::BoringTls(s) => s.write(buf),
InnerNetworkStream::None => { InnerNetworkStream::None => {
@@ -366,7 +375,7 @@ impl Write for NetworkStream {
#[cfg(feature = "native-tls")] #[cfg(feature = "native-tls")]
InnerNetworkStream::NativeTls(s) => s.flush(), InnerNetworkStream::NativeTls(s) => s.flush(),
#[cfg(feature = "rustls")] #[cfg(feature = "rustls")]
InnerNetworkStream::RustlsTls(s) => s.flush(), InnerNetworkStream::Rustls(s) => s.flush(),
#[cfg(feature = "boring-tls")] #[cfg(feature = "boring-tls")]
InnerNetworkStream::BoringTls(s) => s.flush(), InnerNetworkStream::BoringTls(s) => s.flush(),
InnerNetworkStream::None => { InnerNetworkStream::None => {

View File

@@ -65,6 +65,16 @@ pub enum TlsVersion {
/// connecting to a local server. /// connecting to a local server.
#[derive(Clone)] #[derive(Clone)]
#[allow(missing_copy_implementations)] #[allow(missing_copy_implementations)]
#[cfg_attr(
not(any(feature = "native-tls", feature = "rustls", feature = "boring-tls")),
deprecated(
note = "starting from lettre v0.12 `Tls` won't be available when none of the TLS backends are enabled"
)
)]
#[cfg_attr(
docsrs,
doc(cfg(any(feature = "native-tls", feature = "rustls", feature = "boring-tls")))
)]
pub enum Tls { pub enum Tls {
/// Insecure (plaintext) connection only. /// Insecure (plaintext) connection only.
/// ///
@@ -138,14 +148,25 @@ impl Debug for Tls {
/// Source for the base set of root certificates to trust. /// Source for the base set of root certificates to trust.
#[allow(missing_copy_implementations)] #[allow(missing_copy_implementations)]
#[derive(Clone, Debug, Default)] #[derive(Clone, Debug, Default)]
#[cfg_attr(
not(any(feature = "native-tls", feature = "rustls", feature = "boring-tls")),
deprecated(
note = "starting from lettre v0.12 `CertificateStore` won't be available when none of the TLS backends are enabled"
)
)]
#[cfg_attr(
docsrs,
doc(cfg(any(feature = "native-tls", feature = "rustls", feature = "boring-tls")))
)]
pub enum CertificateStore { pub enum CertificateStore {
/// Use the default for the TLS backend. /// Use the default for the TLS backend.
/// ///
/// For native-tls, this will use the system certificate store on Windows, the keychain on /// For native-tls, this will use the system certificate store on Windows, the keychain on
/// macOS, and OpenSSL directories on Linux (usually `/etc/ssl`). /// macOS, and OpenSSL directories on Linux (usually `/etc/ssl`).
/// ///
/// For rustls, this will also use the system store if the `rustls-native-certs` feature is /// For rustls, this will use the system certificate verifier if the `rustls-platform-verifier`
/// enabled, or will fall back to `webpki-roots`. /// feature is enabled. If the `rustls-native-certs` feature is enabled, system certificate
/// store will be used. Otherwise, it will fall back to `webpki-roots`.
/// ///
/// The boring-tls backend uses the same logic as OpenSSL on all platforms. /// The boring-tls backend uses the same logic as OpenSSL on all platforms.
#[default] #[default]
@@ -161,16 +182,34 @@ pub enum CertificateStore {
/// Parameters to use for secure clients /// Parameters to use for secure clients
#[derive(Clone)] #[derive(Clone)]
#[cfg_attr(
not(any(feature = "native-tls", feature = "rustls", feature = "boring-tls")),
deprecated(
note = "starting from lettre v0.12 `TlsParameters` won't be available when none of the TLS backends are enabled"
)
)]
#[cfg_attr(
docsrs,
doc(cfg(any(feature = "native-tls", feature = "rustls", feature = "boring-tls")))
)]
pub struct TlsParameters { pub struct TlsParameters {
pub(crate) connector: InnerTlsParameters, pub(crate) connector: InnerTlsParameters,
/// The domain name which is expected in the TLS certificate from the server /// The domain name which is expected in the TLS certificate from the server
pub(super) domain: String, pub(super) domain: String,
#[cfg(feature = "boring-tls")]
pub(super) accept_invalid_hostnames: bool,
} }
/// Builder for `TlsParameters` /// Builder for `TlsParameters`
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
#[cfg_attr(
not(any(feature = "native-tls", feature = "rustls", feature = "boring-tls")),
deprecated(
note = "starting from lettre v0.12 `TlsParametersBuilder` won't be available when none of the TLS backends are enabled"
)
)]
#[cfg_attr(
docsrs,
doc(cfg(any(feature = "native-tls", feature = "rustls", feature = "boring-tls")))
)]
pub struct TlsParametersBuilder { pub struct TlsParametersBuilder {
domain: String, domain: String,
cert_store: CertificateStore, cert_store: CertificateStore,
@@ -221,6 +260,8 @@ impl TlsParametersBuilder {
/// Controls whether certificates with an invalid hostname are accepted /// Controls whether certificates with an invalid hostname are accepted
/// ///
/// This option is silently disabled when using `rustls-platform-verifier`.
///
/// Defaults to `false`. /// Defaults to `false`.
/// ///
/// # Warning /// # Warning
@@ -244,6 +285,10 @@ impl TlsParametersBuilder {
/// ///
/// Defaults to [`Tlsv12`][TlsVersion::Tlsv12]. /// Defaults to [`Tlsv12`][TlsVersion::Tlsv12].
#[cfg(any(feature = "native-tls", feature = "rustls", feature = "boring-tls"))] #[cfg(any(feature = "native-tls", feature = "rustls", feature = "boring-tls"))]
#[cfg_attr(
docsrs,
doc(cfg(any(feature = "native-tls", feature = "rustls", feature = "boring-tls")))
)]
pub fn set_min_tls_version(mut self, min_tls_version: TlsVersion) -> Self { pub fn set_min_tls_version(mut self, min_tls_version: TlsVersion) -> Self {
self.min_tls_version = min_tls_version; self.min_tls_version = min_tls_version;
self self
@@ -328,10 +373,8 @@ impl TlsParametersBuilder {
let connector = tls_builder.build().map_err(error::tls)?; let connector = tls_builder.build().map_err(error::tls)?;
Ok(TlsParameters { Ok(TlsParameters {
connector: InnerTlsParameters::NativeTls(connector), connector: InnerTlsParameters::NativeTls { connector },
domain: self.domain, domain: self.domain,
#[cfg(feature = "boring-tls")]
accept_invalid_hostnames: self.accept_invalid_hostnames,
}) })
} }
@@ -389,9 +432,11 @@ impl TlsParametersBuilder {
.map_err(error::tls)?; .map_err(error::tls)?;
let connector = tls_builder.build(); let connector = tls_builder.build();
Ok(TlsParameters { Ok(TlsParameters {
connector: InnerTlsParameters::BoringTls(connector), connector: InnerTlsParameters::BoringTls {
connector,
accept_invalid_hostnames: self.accept_invalid_hostnames,
},
domain: self.domain, domain: self.domain,
accept_invalid_hostnames: self.accept_invalid_hostnames,
}) })
} }
@@ -419,7 +464,10 @@ impl TlsParametersBuilder {
// Build TLS config // Build TLS config
let mut root_cert_store = RootCertStore::empty(); let mut root_cert_store = RootCertStore::empty();
#[cfg(feature = "rustls-native-certs")] #[cfg(all(
not(feature = "rustls-platform-verifier"),
feature = "rustls-native-certs"
))]
fn load_native_roots(store: &mut RootCertStore) { fn load_native_roots(store: &mut RootCertStore) {
let rustls_native_certs::CertificateResult { certs, errors, .. } = let rustls_native_certs::CertificateResult { certs, errors, .. } =
rustls_native_certs::load_native_certs(); rustls_native_certs::load_native_certs();
@@ -439,11 +487,26 @@ impl TlsParametersBuilder {
store.extend(webpki_roots::TLS_SERVER_ROOTS.iter().cloned()); store.extend(webpki_roots::TLS_SERVER_ROOTS.iter().cloned());
} }
#[cfg_attr(not(feature = "rustls-platform-verifier"), allow(unused_mut))]
let mut extra_roots = None::<Vec<CertificateDer<'static>>>;
match self.cert_store { match self.cert_store {
CertificateStore::Default => { CertificateStore::Default => {
#[cfg(feature = "rustls-native-certs")] #[cfg(feature = "rustls-platform-verifier")]
{
extra_roots = Some(Vec::new());
}
#[cfg(all(
not(feature = "rustls-platform-verifier"),
feature = "rustls-native-certs"
))]
load_native_roots(&mut root_cert_store); load_native_roots(&mut root_cert_store);
#[cfg(all(not(feature = "rustls-native-certs"), feature = "webpki-roots"))]
#[cfg(all(
not(feature = "rustls-platform-verifier"),
not(feature = "rustls-native-certs"),
feature = "webpki-roots"
))]
load_webpki_roots(&mut root_cert_store); load_webpki_roots(&mut root_cert_store);
} }
#[cfg(all(feature = "rustls", feature = "webpki-roots"))] #[cfg(all(feature = "rustls", feature = "webpki-roots"))]
@@ -454,11 +517,17 @@ impl TlsParametersBuilder {
} }
for cert in self.root_certs { for cert in self.root_certs {
for rustls_cert in cert.rustls { for rustls_cert in cert.rustls {
#[cfg(feature = "rustls-platform-verifier")]
if let Some(extra_roots) = &mut extra_roots {
extra_roots.push(rustls_cert.clone());
}
root_cert_store.add(rustls_cert).map_err(error::tls)?; root_cert_store.add(rustls_cert).map_err(error::tls)?;
} }
} }
let tls = if self.accept_invalid_certs || self.accept_invalid_hostnames { let tls = if self.accept_invalid_certs
|| (extra_roots.is_none() && self.accept_invalid_hostnames)
{
let verifier = InvalidCertsVerifier { let verifier = InvalidCertsVerifier {
ignore_invalid_hostnames: self.accept_invalid_hostnames, ignore_invalid_hostnames: self.accept_invalid_hostnames,
ignore_invalid_certs: self.accept_invalid_certs, ignore_invalid_certs: self.accept_invalid_certs,
@@ -468,7 +537,23 @@ impl TlsParametersBuilder {
tls.dangerous() tls.dangerous()
.with_custom_certificate_verifier(Arc::new(verifier)) .with_custom_certificate_verifier(Arc::new(verifier))
} else { } else {
tls.with_root_certificates(root_cert_store) #[cfg(feature = "rustls-platform-verifier")]
if let Some(extra_roots) = extra_roots {
tls.dangerous().with_custom_certificate_verifier(Arc::new(
rustls_platform_verifier::Verifier::new_with_extra_roots(
extra_roots,
crypto_provider,
)
.map_err(error::tls)?,
))
} else {
tls.with_root_certificates(root_cert_store)
}
#[cfg(not(feature = "rustls-platform-verifier"))]
{
tls.with_root_certificates(root_cert_store)
}
}; };
let tls = if let Some(identity) = self.identity { let tls = if let Some(identity) = self.identity {
@@ -480,23 +565,26 @@ impl TlsParametersBuilder {
}; };
Ok(TlsParameters { Ok(TlsParameters {
connector: InnerTlsParameters::RustlsTls(Arc::new(tls)), connector: InnerTlsParameters::Rustls {
config: Arc::new(tls),
},
domain: self.domain, domain: self.domain,
#[cfg(feature = "boring-tls")]
accept_invalid_hostnames: self.accept_invalid_hostnames,
}) })
} }
} }
#[derive(Clone)] #[derive(Clone)]
#[allow(clippy::enum_variant_names)] #[allow(clippy::enum_variant_names)]
pub enum InnerTlsParameters { pub(crate) enum InnerTlsParameters {
#[cfg(feature = "native-tls")] #[cfg(feature = "native-tls")]
NativeTls(TlsConnector), NativeTls { connector: TlsConnector },
#[cfg(feature = "rustls")] #[cfg(feature = "rustls")]
RustlsTls(Arc<ClientConfig>), Rustls { config: Arc<ClientConfig> },
#[cfg(feature = "boring-tls")] #[cfg(feature = "boring-tls")]
BoringTls(SslConnector), BoringTls {
connector: SslConnector,
accept_invalid_hostnames: bool,
},
} }
impl TlsParameters { impl TlsParameters {
@@ -545,6 +633,16 @@ impl TlsParameters {
/// A certificate that can be used with [`TlsParametersBuilder::add_root_certificate`] /// A certificate that can be used with [`TlsParametersBuilder::add_root_certificate`]
#[derive(Clone)] #[derive(Clone)]
#[allow(missing_copy_implementations)] #[allow(missing_copy_implementations)]
#[cfg_attr(
not(any(feature = "native-tls", feature = "rustls", feature = "boring-tls")),
deprecated(
note = "starting from lettre v0.12 `Certificate` won't be available when none of the TLS backends are enabled"
)
)]
#[cfg_attr(
docsrs,
doc(cfg(any(feature = "native-tls", feature = "rustls", feature = "boring-tls")))
)]
pub struct Certificate { pub struct Certificate {
#[cfg(feature = "native-tls")] #[cfg(feature = "native-tls")]
native_tls: native_tls::Certificate, native_tls: native_tls::Certificate,
@@ -608,6 +706,16 @@ impl Debug for Certificate {
/// An identity that can be used with [`TlsParametersBuilder::identify_with`] /// An identity that can be used with [`TlsParametersBuilder::identify_with`]
#[allow(missing_copy_implementations)] #[allow(missing_copy_implementations)]
#[cfg_attr(
not(any(feature = "native-tls", feature = "rustls", feature = "boring-tls")),
deprecated(
note = "starting from lettre v0.12 `Identity` won't be available when none of the TLS backends are enabled"
)
)]
#[cfg_attr(
docsrs,
doc(cfg(any(feature = "native-tls", feature = "rustls", feature = "boring-tls")))
)]
pub struct Identity { pub struct Identity {
#[cfg(feature = "native-tls")] #[cfg(feature = "native-tls")]
native_tls: native_tls::Identity, native_tls: native_tls::Identity,

View File

@@ -13,6 +13,7 @@ use super::{
pub(crate) trait TransportBuilder { pub(crate) trait TransportBuilder {
fn new<T: Into<String>>(server: T) -> Self; fn new<T: Into<String>>(server: T) -> Self;
#[cfg(any(feature = "native-tls", feature = "rustls", feature = "boring-tls"))]
fn tls(self, tls: super::Tls) -> Self; fn tls(self, tls: super::Tls) -> Self;
fn port(self, port: u16) -> Self; fn port(self, port: u16) -> Self;
fn credentials(self, credentials: Credentials) -> Self; fn credentials(self, credentials: Credentials) -> Self;
@@ -24,6 +25,7 @@ impl TransportBuilder for SmtpTransportBuilder {
Self::new(server) Self::new(server)
} }
#[cfg(any(feature = "native-tls", feature = "rustls", feature = "boring-tls"))]
fn tls(self, tls: super::Tls) -> Self { fn tls(self, tls: super::Tls) -> Self {
self.tls(tls) self.tls(tls)
} }
@@ -47,6 +49,7 @@ impl TransportBuilder for AsyncSmtpTransportBuilder {
Self::new(server) Self::new(server)
} }
#[cfg(any(feature = "native-tls", feature = "rustls", feature = "boring-tls"))]
fn tls(self, tls: super::Tls) -> Self { fn tls(self, tls: super::Tls) -> Self {
self.tls(tls) self.tls(tls)
} }

View File

@@ -77,6 +77,11 @@ impl Error {
matches!(self.inner.kind, Kind::Tls) matches!(self.inner.kind, Kind::Tls)
} }
/// Returns true if the error is because the transport was shut down
pub fn is_transport_shutdown(&self) -> bool {
matches!(self.inner.kind, Kind::TransportShutdown)
}
/// Returns the status code, if the error was generated from a response. /// Returns the status code, if the error was generated from a response.
pub fn status(&self) -> Option<Code> { pub fn status(&self) -> Option<Code> {
match self.inner.kind { match self.inner.kind {
@@ -111,6 +116,8 @@ pub(crate) enum Kind {
)] )]
#[cfg(any(feature = "native-tls", feature = "rustls", feature = "boring-tls"))] #[cfg(any(feature = "native-tls", feature = "rustls", feature = "boring-tls"))]
Tls, Tls,
/// Transport shutdown error
TransportShutdown,
} }
impl fmt::Debug for Error { impl fmt::Debug for Error {
@@ -136,6 +143,7 @@ impl fmt::Display for Error {
Kind::Connection => f.write_str("Connection error")?, Kind::Connection => f.write_str("Connection error")?,
#[cfg(any(feature = "native-tls", feature = "rustls", feature = "boring-tls"))] #[cfg(any(feature = "native-tls", feature = "rustls", feature = "boring-tls"))]
Kind::Tls => f.write_str("tls error")?, Kind::Tls => f.write_str("tls error")?,
Kind::TransportShutdown => f.write_str("transport has been shut down")?,
Kind::Transient(code) => { Kind::Transient(code) => {
write!(f, "transient error ({code})")?; write!(f, "transient error ({code})")?;
} }
@@ -189,3 +197,7 @@ pub(crate) fn connection<E: Into<BoxError>>(e: E) -> Error {
pub(crate) fn tls<E: Into<BoxError>>(e: E) -> Error { pub(crate) fn tls<E: Into<BoxError>>(e: E) -> Error {
Error::new(Kind::Tls, Some(e)) Error::new(Kind::Tls, Some(e))
} }
pub(crate) fn transport_shutdown() -> Error {
Error::new::<BoxError>(Kind::TransportShutdown, None)
}

View File

@@ -1,6 +1,5 @@
use std::{ use std::{
fmt::{self, Debug}, fmt::{self, Debug},
mem,
ops::{Deref, DerefMut}, ops::{Deref, DerefMut},
sync::{Arc, OnceLock}, sync::{Arc, OnceLock},
time::{Duration, Instant}, time::{Duration, Instant},
@@ -15,11 +14,15 @@ use super::{
super::{client::AsyncSmtpConnection, Error}, super::{client::AsyncSmtpConnection, Error},
PoolConfig, PoolConfig,
}; };
use crate::{executor::SpawnHandle, transport::smtp::async_transport::AsyncSmtpClient, Executor}; use crate::{
executor::SpawnHandle,
transport::smtp::{async_transport::AsyncSmtpClient, error},
Executor,
};
pub struct Pool<E: Executor> { pub(crate) struct Pool<E: Executor> {
config: PoolConfig, config: PoolConfig,
connections: Mutex<Vec<ParkedConnection>>, connections: Mutex<Option<Vec<ParkedConnection>>>,
client: AsyncSmtpClient<E>, client: AsyncSmtpClient<E>,
handle: OnceLock<E::Handle>, handle: OnceLock<E::Handle>,
} }
@@ -29,16 +32,16 @@ struct ParkedConnection {
since: Instant, since: Instant,
} }
pub struct PooledConnection<E: Executor> { pub(crate) struct PooledConnection<E: Executor> {
conn: Option<AsyncSmtpConnection>, conn: Option<AsyncSmtpConnection>,
pool: Arc<Pool<E>>, pool: Arc<Pool<E>>,
} }
impl<E: Executor> Pool<E> { impl<E: Executor> Pool<E> {
pub fn new(config: PoolConfig, client: AsyncSmtpClient<E>) -> Arc<Self> { pub(crate) fn new(config: PoolConfig, client: AsyncSmtpClient<E>) -> Arc<Self> {
let pool = Arc::new(Self { let pool = Arc::new(Self {
config, config,
connections: Mutex::new(Vec::new()), connections: Mutex::new(Some(Vec::new())),
client, client,
handle: OnceLock::new(), handle: OnceLock::new(),
}); });
@@ -60,6 +63,10 @@ impl<E: Executor> Pool<E> {
#[allow(clippy::needless_collect)] #[allow(clippy::needless_collect)]
let (count, dropped) = { let (count, dropped) = {
let mut connections = pool.connections.lock().await; let mut connections = pool.connections.lock().await;
let Some(connections) = connections.as_mut() else {
// The transport was shut down
return;
};
let to_drop = connections let to_drop = connections
.iter() .iter()
@@ -92,6 +99,11 @@ impl<E: Executor> Pool<E> {
}; };
let mut connections = pool.connections.lock().await; let mut connections = pool.connections.lock().await;
let Some(connections) = connections.as_mut() else {
// The transport was shut down
return;
};
connections.push(ParkedConnection::park(conn)); connections.push(ParkedConnection::park(conn));
#[cfg(feature = "tracing")] #[cfg(feature = "tracing")]
@@ -134,10 +146,25 @@ impl<E: Executor> Pool<E> {
pool pool
} }
pub async fn connection(self: &Arc<Self>) -> Result<PooledConnection<E>, Error> { pub(crate) async fn shutdown(&self) {
let connections = { self.connections.lock().await.take() };
if let Some(connections) = connections {
abort_concurrent(connections.into_iter().map(ParkedConnection::unpark)).await;
}
if let Some(handle) = self.handle.get() {
handle.shutdown().await;
}
}
pub(crate) async fn connection(self: &Arc<Self>) -> Result<PooledConnection<E>, Error> {
loop { loop {
let conn = { let conn = {
let mut connections = self.connections.lock().await; let mut connections = self.connections.lock().await;
let Some(connections) = connections.as_mut() else {
// The transport was shut down
return Err(error::transport_shutdown());
};
connections.pop() connections.pop()
}; };
@@ -181,13 +208,20 @@ impl<E: Executor> Pool<E> {
#[cfg(feature = "tracing")] #[cfg(feature = "tracing")]
tracing::debug!("recycling connection"); tracing::debug!("recycling connection");
let mut connections = self.connections.lock().await; let mut connections_guard = self.connections.lock().await;
if connections.len() >= self.config.max_size as usize {
drop(connections); if let Some(connections) = connections_guard.as_mut() {
conn.abort().await; if connections.len() >= self.config.max_size as usize {
drop(connections_guard);
conn.abort().await;
} else {
let conn = ParkedConnection::park(conn);
connections.push(conn);
}
} else { } else {
let conn = ParkedConnection::park(conn); // The pool has already been shut down
connections.push(conn); drop(connections_guard);
conn.abort().await;
} }
} }
} }
@@ -200,7 +234,13 @@ impl<E: Executor> Debug for Pool<E> {
.field( .field(
"connections", "connections",
&match self.connections.try_lock() { &match self.connections.try_lock() {
Some(connections) => format!("{} connections", connections.len()), Some(connections) => {
if let Some(connections) = connections.as_ref() {
format!("{} connections", connections.len())
} else {
"SHUT DOWN".to_owned()
}
}
None => "LOCKED".to_owned(), None => "LOCKED".to_owned(),
}, },
@@ -222,14 +262,16 @@ impl<E: Executor> Drop for Pool<E> {
#[cfg(feature = "tracing")] #[cfg(feature = "tracing")]
tracing::debug!("dropping Pool"); tracing::debug!("dropping Pool");
let connections = mem::take(self.connections.get_mut()); let connections = self.connections.get_mut().take();
let handle = self.handle.take(); let handle = self.handle.take();
E::spawn(async move { E::spawn(async move {
if let Some(handle) = handle { if let Some(handle) = handle {
handle.shutdown().await; handle.shutdown().await;
} }
abort_concurrent(connections.into_iter().map(ParkedConnection::unpark)).await; if let Some(connections) = connections {
abort_concurrent(connections.into_iter().map(ParkedConnection::unpark)).await;
}
}); });
} }
} }

View File

@@ -1,8 +1,8 @@
use std::time::Duration; use std::time::Duration;
#[cfg(any(feature = "tokio1", feature = "async-std1"))] #[cfg(any(feature = "tokio1", feature = "async-std1"))]
pub mod async_impl; pub(super) mod async_impl;
pub mod sync_impl; pub(super) mod sync_impl;
/// Configuration for a connection pool /// Configuration for a connection pool
#[derive(Debug, Clone)] #[derive(Debug, Clone)]

View File

@@ -1,8 +1,7 @@
use std::{ use std::{
fmt::{self, Debug}, fmt::{self, Debug},
mem,
ops::{Deref, DerefMut}, ops::{Deref, DerefMut},
sync::{Arc, Mutex, TryLockError}, sync::{mpsc, Arc, Mutex, TryLockError},
thread, thread,
time::{Duration, Instant}, time::{Duration, Instant},
}; };
@@ -11,11 +10,12 @@ use super::{
super::{client::SmtpConnection, Error}, super::{client::SmtpConnection, Error},
PoolConfig, PoolConfig,
}; };
use crate::transport::smtp::transport::SmtpClient; use crate::transport::smtp::{error, transport::SmtpClient};
pub struct Pool { pub(crate) struct Pool {
config: PoolConfig, config: PoolConfig,
connections: Mutex<Vec<ParkedConnection>>, connections: Mutex<Option<Vec<ParkedConnection>>>,
thread_terminator: mpsc::SyncSender<()>,
client: SmtpClient, client: SmtpClient,
} }
@@ -24,16 +24,19 @@ struct ParkedConnection {
since: Instant, since: Instant,
} }
pub struct PooledConnection { pub(crate) struct PooledConnection {
conn: Option<SmtpConnection>, conn: Option<SmtpConnection>,
pool: Arc<Pool>, pool: Arc<Pool>,
} }
impl Pool { impl Pool {
pub fn new(config: PoolConfig, client: SmtpClient) -> Arc<Self> { pub(crate) fn new(config: PoolConfig, client: SmtpClient) -> Arc<Self> {
let (thread_tx, thread_rx) = mpsc::sync_channel(1);
let pool = Arc::new(Self { let pool = Arc::new(Self {
config, config,
connections: Mutex::new(Vec::new()), connections: Mutex::new(Some(Vec::new())),
thread_terminator: thread_tx,
client, client,
}); });
@@ -54,6 +57,10 @@ impl Pool {
#[allow(clippy::needless_collect)] #[allow(clippy::needless_collect)]
let (count, dropped) = { let (count, dropped) = {
let mut connections = pool.connections.lock().unwrap(); let mut connections = pool.connections.lock().unwrap();
let Some(connections) = connections.as_mut() else {
// The transport was shut down
return;
};
let to_drop = connections let to_drop = connections
.iter() .iter()
@@ -86,6 +93,11 @@ impl Pool {
}; };
let mut connections = pool.connections.lock().unwrap(); let mut connections = pool.connections.lock().unwrap();
let Some(connections) = connections.as_mut() else {
// The transport was shut down
return;
};
connections.push(ParkedConnection::park(conn)); connections.push(ParkedConnection::park(conn));
#[cfg(feature = "tracing")] #[cfg(feature = "tracing")]
@@ -110,7 +122,14 @@ impl Pool {
} }
drop(pool); drop(pool);
thread::sleep(idle_timeout);
match thread_rx.recv_timeout(idle_timeout) {
Ok(()) | Err(mpsc::RecvTimeoutError::Disconnected) => {
// The transport was shut down
return;
}
Err(mpsc::RecvTimeoutError::Timeout) => {}
}
} }
}) })
.expect("couldn't spawn the Pool thread"); .expect("couldn't spawn the Pool thread");
@@ -119,10 +138,25 @@ impl Pool {
pool pool
} }
pub fn connection(self: &Arc<Self>) -> Result<PooledConnection, Error> { pub(crate) fn shutdown(&self) {
let connections = { self.connections.lock().unwrap().take() };
if let Some(connections) = connections {
for conn in connections {
conn.unpark().abort();
}
}
_ = self.thread_terminator.try_send(());
}
pub(crate) fn connection(self: &Arc<Self>) -> Result<PooledConnection, Error> {
loop { loop {
let conn = { let conn = {
let mut connections = self.connections.lock().unwrap(); let mut connections = self.connections.lock().unwrap();
let Some(connections) = connections.as_mut() else {
// The transport was shut down
return Err(error::transport_shutdown());
};
connections.pop() connections.pop()
}; };
@@ -166,13 +200,20 @@ impl Pool {
#[cfg(feature = "tracing")] #[cfg(feature = "tracing")]
tracing::debug!("recycling connection"); tracing::debug!("recycling connection");
let mut connections = self.connections.lock().unwrap(); let mut connections_guard = self.connections.lock().unwrap();
if connections.len() >= self.config.max_size as usize {
drop(connections); if let Some(connections) = connections_guard.as_mut() {
conn.abort(); if connections.len() >= self.config.max_size as usize {
drop(connections_guard);
conn.abort();
} else {
let conn = ParkedConnection::park(conn);
connections.push(conn);
}
} else { } else {
let conn = ParkedConnection::park(conn); // The pool has already been shut down
connections.push(conn); drop(connections_guard);
conn.abort();
} }
} }
} }
@@ -185,7 +226,13 @@ impl Debug for Pool {
.field( .field(
"connections", "connections",
&match self.connections.try_lock() { &match self.connections.try_lock() {
Ok(connections) => format!("{} connections", connections.len()), Ok(connections) => {
if let Some(connections) = connections.as_ref() {
format!("{} connections", connections.len())
} else {
"SHUT DOWN".to_owned()
}
}
Err(TryLockError::WouldBlock) => "LOCKED".to_owned(), Err(TryLockError::WouldBlock) => "LOCKED".to_owned(),
Err(TryLockError::Poisoned(_)) => "POISONED".to_owned(), Err(TryLockError::Poisoned(_)) => "POISONED".to_owned(),
@@ -201,10 +248,11 @@ impl Drop for Pool {
#[cfg(feature = "tracing")] #[cfg(feature = "tracing")]
tracing::debug!("dropping Pool"); tracing::debug!("dropping Pool");
let connections = mem::take(&mut *self.connections.get_mut().unwrap()); if let Some(connections) = self.connections.get_mut().unwrap().take() {
for conn in connections { for conn in connections {
let mut conn = conn.unpark(); let mut conn = conn.unpark();
conn.abort(); conn.abort();
}
} }
} }
} }

View File

@@ -60,6 +60,11 @@ impl Transport for SmtpTransport {
Ok(result) Ok(result)
} }
fn shutdown(&self) {
#[cfg(feature = "pool")]
self.inner.shutdown();
}
} }
impl Debug for SmtpTransport { impl Debug for SmtpTransport {
@@ -172,7 +177,7 @@ impl SmtpTransport {
/// a proper URL encoder, like the following cargo script: /// a proper URL encoder, like the following cargo script:
/// ///
/// ```rust /// ```rust
/// # let _ = r#" /// # const TOML: &str = r#"
/// #!/usr/bin/env cargo /// #!/usr/bin/env cargo
/// ///
/// //! ```cargo /// //! ```cargo
@@ -369,7 +374,7 @@ impl SmtpTransportBuilder {
/// Build client /// Build client
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct SmtpClient { pub(super) struct SmtpClient {
info: SmtpInfo, info: SmtpInfo,
} }
@@ -377,7 +382,7 @@ impl SmtpClient {
/// Creates a new connection directly usable to send emails /// Creates a new connection directly usable to send emails
/// ///
/// Handles encryption and authentication /// Handles encryption and authentication
pub fn connection(&self) -> Result<SmtpConnection, Error> { pub(super) fn connection(&self) -> Result<SmtpConnection, Error> {
#[allow(clippy::match_single_binding)] #[allow(clippy::match_single_binding)]
let tls_parameters = match &self.info.tls { let tls_parameters = match &self.info.tls {
#[cfg(any(feature = "native-tls", feature = "rustls", feature = "boring-tls"))] #[cfg(any(feature = "native-tls", feature = "rustls", feature = "boring-tls"))]

View File

@@ -4,7 +4,7 @@ use std::fmt::{Display, Formatter, Result as FmtResult};
/// Encode a string as xtext /// Encode a string as xtext
#[derive(Debug)] #[derive(Debug)]
pub struct XText<'a>(pub &'a str); pub(crate) struct XText<'a>(pub(crate) &'a str);
impl Display for XText<'_> { impl Display for XText<'_> {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult { fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {

View File

@@ -43,13 +43,11 @@
use std::{ use std::{
error::Error as StdError, error::Error as StdError,
fmt, fmt,
sync::{Arc, Mutex as StdMutex}, sync::{Arc, Mutex},
}; };
#[cfg(any(feature = "tokio1", feature = "async-std1"))] #[cfg(any(feature = "tokio1", feature = "async-std1"))]
use async_trait::async_trait; use async_trait::async_trait;
#[cfg(any(feature = "tokio1", feature = "async-std1"))]
use futures_util::lock::Mutex as FuturesMutex;
#[cfg(any(feature = "tokio1", feature = "async-std1"))] #[cfg(any(feature = "tokio1", feature = "async-std1"))]
use crate::AsyncTransport; use crate::AsyncTransport;
@@ -72,7 +70,7 @@ impl StdError for Error {}
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct StubTransport { pub struct StubTransport {
response: Result<(), Error>, response: Result<(), Error>,
message_log: Arc<StdMutex<Vec<(Envelope, String)>>>, message_log: Arc<Mutex<Vec<(Envelope, String)>>>,
} }
/// This transport logs messages and always returns the given response /// This transport logs messages and always returns the given response
@@ -81,7 +79,7 @@ pub struct StubTransport {
#[cfg_attr(docsrs, doc(cfg(any(feature = "tokio1", feature = "async-std1"))))] #[cfg_attr(docsrs, doc(cfg(any(feature = "tokio1", feature = "async-std1"))))]
pub struct AsyncStubTransport { pub struct AsyncStubTransport {
response: Result<(), Error>, response: Result<(), Error>,
message_log: Arc<FuturesMutex<Vec<(Envelope, String)>>>, message_log: Arc<Mutex<Vec<(Envelope, String)>>>,
} }
impl StubTransport { impl StubTransport {
@@ -89,7 +87,7 @@ impl StubTransport {
pub fn new(response: Result<(), Error>) -> Self { pub fn new(response: Result<(), Error>) -> Self {
Self { Self {
response, response,
message_log: Arc::new(StdMutex::new(vec![])), message_log: Arc::new(Mutex::new(vec![])),
} }
} }
@@ -97,7 +95,7 @@ impl StubTransport {
pub fn new_ok() -> Self { pub fn new_ok() -> Self {
Self { Self {
response: Ok(()), response: Ok(()),
message_log: Arc::new(StdMutex::new(vec![])), message_log: Arc::new(Mutex::new(vec![])),
} }
} }
@@ -105,7 +103,7 @@ impl StubTransport {
pub fn new_error() -> Self { pub fn new_error() -> Self {
Self { Self {
response: Err(Error), response: Err(Error),
message_log: Arc::new(StdMutex::new(vec![])), message_log: Arc::new(Mutex::new(vec![])),
} }
} }
@@ -124,7 +122,7 @@ impl AsyncStubTransport {
pub fn new(response: Result<(), Error>) -> Self { pub fn new(response: Result<(), Error>) -> Self {
Self { Self {
response, response,
message_log: Arc::new(FuturesMutex::new(vec![])), message_log: Arc::new(Mutex::new(vec![])),
} }
} }
@@ -132,7 +130,7 @@ impl AsyncStubTransport {
pub fn new_ok() -> Self { pub fn new_ok() -> Self {
Self { Self {
response: Ok(()), response: Ok(()),
message_log: Arc::new(FuturesMutex::new(vec![])), message_log: Arc::new(Mutex::new(vec![])),
} }
} }
@@ -140,14 +138,14 @@ impl AsyncStubTransport {
pub fn new_error() -> Self { pub fn new_error() -> Self {
Self { Self {
response: Err(Error), response: Err(Error),
message_log: Arc::new(FuturesMutex::new(vec![])), message_log: Arc::new(Mutex::new(vec![])),
} }
} }
/// Return all logged messages sent using [`AsyncTransport::send_raw`] /// Return all logged messages sent using [`AsyncTransport::send_raw`]
#[cfg(any(feature = "tokio1", feature = "async-std1"))] #[cfg(any(feature = "tokio1", feature = "async-std1"))]
pub async fn messages(&self) -> Vec<(Envelope, String)> { pub async fn messages(&self) -> Vec<(Envelope, String)> {
self.message_log.lock().await.clone() self.message_log.lock().unwrap().clone()
} }
} }
@@ -173,7 +171,7 @@ impl AsyncTransport for AsyncStubTransport {
async fn send_raw(&self, envelope: &Envelope, email: &[u8]) -> Result<Self::Ok, Self::Error> { async fn send_raw(&self, envelope: &Envelope, email: &[u8]) -> Result<Self::Ok, Self::Error> {
self.message_log self.message_log
.lock() .lock()
.await .unwrap()
.push((envelope.clone(), String::from_utf8_lossy(email).into())); .push((envelope.clone(), String::from_utf8_lossy(email).into()));
self.response self.response
} }