Compare commits
31 Commits
v0.10.0-rc
...
v0.10.0-rc
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
94cc0149d1 | ||
|
|
a89383cdb6 | ||
|
|
592593f4b8 | ||
|
|
97d3c760c0 | ||
|
|
8f28b0c341 | ||
|
|
dc9c5df210 | ||
|
|
c9b3fa0baa | ||
|
|
addf8754dd | ||
|
|
af157c5f26 | ||
|
|
3e8988ae55 | ||
|
|
941a00bcaa | ||
|
|
14079bff8c | ||
|
|
696c06e8d7 | ||
|
|
d4f7618898 | ||
|
|
e0a0a2e624 | ||
|
|
9ab6bb56d3 | ||
|
|
e1d3778329 | ||
|
|
623d69c553 | ||
|
|
55c2618201 | ||
|
|
9f550bce86 | ||
|
|
e875d9ff64 | ||
|
|
aadcc0f83c | ||
|
|
b534a18017 | ||
|
|
0684bccd47 | ||
|
|
4471759221 | ||
|
|
ed454819ee | ||
|
|
47cad567b0 | ||
|
|
b0e2fc9bca | ||
|
|
1d8249165c | ||
|
|
98fc0cb2f3 | ||
|
|
0439bab874 |
6
.github/workflows/test.yml
vendored
6
.github/workflows/test.yml
vendored
@@ -7,6 +7,8 @@ on:
|
|||||||
- master
|
- master
|
||||||
|
|
||||||
env:
|
env:
|
||||||
|
RUSTFLAGS: "--cfg lettre_ignore_tls_mismatch"
|
||||||
|
RUSTDOCFLAGS: "--cfg lettre_ignore_tls_mismatch"
|
||||||
RUST_BACKTRACE: full
|
RUST_BACKTRACE: full
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
@@ -79,8 +81,8 @@ jobs:
|
|||||||
rust: stable
|
rust: stable
|
||||||
- name: beta
|
- name: beta
|
||||||
rust: beta
|
rust: beta
|
||||||
- name: 1.46.0
|
- name: 1.52.1
|
||||||
rust: 1.46.0
|
rust: 1.52.1
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout
|
- name: Checkout
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
|
|
||||||
Several breaking changes were made between 0.9 and 0.10, but changes should be straightforward:
|
Several breaking changes were made between 0.9 and 0.10, but changes should be straightforward:
|
||||||
|
|
||||||
* MSRV is now 1.46
|
* MSRV is now 1.52.1
|
||||||
* The `lettre_email` crate has been merged into `lettre`. To migrate, replace `lettre_email` with `lettre::message`
|
* The `lettre_email` crate has been merged into `lettre`. To migrate, replace `lettre_email` with `lettre::message`
|
||||||
and make sure to enable the `builder` feature (it's enabled by default).
|
and make sure to enable the `builder` feature (it's enabled by default).
|
||||||
* `SendableEmail` has been renamed to `Email` and `EmailBuilder::build()` produces it directly. To migrate,
|
* `SendableEmail` has been renamed to `Email` and `EmailBuilder::build()` produces it directly. To migrate,
|
||||||
@@ -31,7 +31,8 @@ Several breaking changes were made between 0.9 and 0.10, but changes should be s
|
|||||||
* When the hostname feature is disabled or hostname cannot be fetched, `127.0.0.1` is used instead of `localhost` as EHLO parameter (for better RFC compliance and mail server compatibility)
|
* When the hostname feature is disabled or hostname cannot be fetched, `127.0.0.1` is used instead of `localhost` as EHLO parameter (for better RFC compliance and mail server compatibility)
|
||||||
* The `new` method of `ClientId` is deprecated
|
* The `new` method of `ClientId` is deprecated
|
||||||
* Rename `serde-impls` feature to `serde`
|
* Rename `serde-impls` feature to `serde`
|
||||||
|
* The `SendmailTransport` now uses the `sendmail` command in current `PATH` by default instead of
|
||||||
|
`/usr/bin/sendmail`.
|
||||||
|
|
||||||
#### Bug Fixes
|
#### Bug Fixes
|
||||||
|
|
||||||
|
|||||||
30
Cargo.toml
30
Cargo.toml
@@ -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.10.0-rc.1"
|
version = "0.10.0-rc.4"
|
||||||
description = "Email client"
|
description = "Email client"
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
homepage = "https://lettre.rs"
|
homepage = "https://lettre.rs"
|
||||||
@@ -19,6 +19,7 @@ maintenance = { status = "actively-developed" }
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
idna = "0.2"
|
idna = "0.2"
|
||||||
|
once_cell = "1"
|
||||||
tracing = { version = "0.1.16", default-features = false, features = ["std"], optional = true } # feature
|
tracing = { version = "0.1.16", default-features = false, features = ["std"], optional = true } # feature
|
||||||
|
|
||||||
# builder
|
# builder
|
||||||
@@ -27,7 +28,6 @@ mime = { version = "0.3.4", optional = true }
|
|||||||
fastrand = { version = "1.4", optional = true }
|
fastrand = { version = "1.4", optional = true }
|
||||||
quoted_printable = { version = "0.4", optional = true }
|
quoted_printable = { version = "0.4", optional = true }
|
||||||
base64 = { version = "0.13", optional = true }
|
base64 = { version = "0.13", optional = true }
|
||||||
once_cell = "1"
|
|
||||||
regex = { version = "1", default-features = false, features = ["std", "unicode-case"] }
|
regex = { version = "1", default-features = false, features = ["std", "unicode-case"] }
|
||||||
|
|
||||||
# file transport
|
# file transport
|
||||||
@@ -36,15 +36,14 @@ serde = { version = "1", optional = true, features = ["derive"] }
|
|||||||
serde_json = { version = "1", optional = true }
|
serde_json = { version = "1", optional = true }
|
||||||
|
|
||||||
# smtp
|
# smtp
|
||||||
nom = { version = "6", default-features = false, features = ["alloc", "std"], optional = true }
|
nom = { version = "7", optional = true }
|
||||||
r2d2 = { version = "0.8", optional = true } # feature
|
|
||||||
hostname = { version = "0.3", optional = true } # feature
|
hostname = { version = "0.3", optional = true } # feature
|
||||||
|
|
||||||
## tls
|
## tls
|
||||||
native-tls = { version = "0.2", optional = true } # feature
|
native-tls = { version = "0.2", optional = true } # feature
|
||||||
rustls = { version = "0.19", features = ["dangerous_configuration"], optional = true }
|
rustls = { version = "0.20", features = ["dangerous_configuration"], optional = true }
|
||||||
webpki = { version = "0.21", optional = true }
|
rustls-pemfile = { version = "0.2.1", optional = true }
|
||||||
webpki-roots = { version = "0.21", optional = true }
|
webpki-roots = { version = "0.22", optional = true }
|
||||||
|
|
||||||
# async
|
# async
|
||||||
futures-io = { version = "0.3.7", optional = true }
|
futures-io = { version = "0.3.7", optional = true }
|
||||||
@@ -54,12 +53,12 @@ async-trait = { version = "0.1", optional = true }
|
|||||||
## async-std
|
## async-std
|
||||||
async-std = { version = "1.8", optional = true, features = ["unstable"] }
|
async-std = { version = "1.8", optional = true, features = ["unstable"] }
|
||||||
#async-native-tls = { version = "0.3.3", optional = true }
|
#async-native-tls = { version = "0.3.3", optional = true }
|
||||||
async-rustls = { version = "0.2", optional = true }
|
futures-rustls = { version = "0.22", optional = true }
|
||||||
|
|
||||||
## tokio
|
## tokio
|
||||||
tokio1_crate = { package = "tokio", version = "1", features = ["fs", "process", "net", "io-util"], optional = true }
|
tokio1_crate = { package = "tokio", version = "1", features = ["fs", "process", "time", "net", "io-util"], optional = true }
|
||||||
tokio1_native_tls_crate = { package = "tokio-native-tls", version = "0.3", optional = true }
|
tokio1_native_tls_crate = { package = "tokio-native-tls", version = "0.3", optional = true }
|
||||||
tokio1_rustls = { package = "tokio-rustls", version = "0.22", optional = true }
|
tokio1_rustls = { package = "tokio-rustls", version = "0.23", optional = true }
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
criterion = "0.3"
|
criterion = "0.3"
|
||||||
@@ -76,8 +75,9 @@ harness = false
|
|||||||
name = "transport_smtp"
|
name = "transport_smtp"
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
default = ["smtp-transport", "native-tls", "hostname", "r2d2", "builder"]
|
default = ["smtp-transport", "pool", "native-tls", "hostname", "builder"]
|
||||||
builder = ["httpdate", "mime", "base64", "fastrand", "quoted_printable"]
|
builder = ["httpdate", "mime", "base64", "fastrand", "quoted_printable"]
|
||||||
|
mime03 = ["mime"]
|
||||||
|
|
||||||
# transports
|
# transports
|
||||||
file-transport = ["uuid"]
|
file-transport = ["uuid"]
|
||||||
@@ -85,19 +85,21 @@ file-transport-envelope = ["serde", "serde_json", "file-transport"]
|
|||||||
sendmail-transport = []
|
sendmail-transport = []
|
||||||
smtp-transport = ["base64", "nom"]
|
smtp-transport = ["base64", "nom"]
|
||||||
|
|
||||||
rustls-tls = ["webpki", "webpki-roots", "rustls"]
|
pool = ["futures-util"]
|
||||||
|
|
||||||
|
rustls-tls = ["webpki-roots", "rustls", "rustls-pemfile"]
|
||||||
|
|
||||||
# async
|
# async
|
||||||
async-std1 = ["async-std", "async-trait", "futures-io", "futures-util"]
|
async-std1 = ["async-std", "async-trait", "futures-io", "futures-util"]
|
||||||
#async-std1-native-tls = ["async-std1", "native-tls", "async-native-tls"]
|
#async-std1-native-tls = ["async-std1", "native-tls", "async-native-tls"]
|
||||||
async-std1-rustls-tls = ["async-std1", "rustls-tls", "async-rustls"]
|
async-std1-rustls-tls = ["async-std1", "rustls-tls", "futures-rustls"]
|
||||||
tokio1 = ["tokio1_crate", "async-trait", "futures-io", "futures-util"]
|
tokio1 = ["tokio1_crate", "async-trait", "futures-io", "futures-util"]
|
||||||
tokio1-native-tls = ["tokio1", "native-tls", "tokio1_native_tls_crate"]
|
tokio1-native-tls = ["tokio1", "native-tls", "tokio1_native_tls_crate"]
|
||||||
tokio1-rustls-tls = ["tokio1", "rustls-tls", "tokio1_rustls"]
|
tokio1-rustls-tls = ["tokio1", "rustls-tls", "tokio1_rustls"]
|
||||||
|
|
||||||
[package.metadata.docs.rs]
|
[package.metadata.docs.rs]
|
||||||
all-features = true
|
all-features = true
|
||||||
rustdoc-args = ["--cfg", "docsrs"]
|
rustdoc-args = ["--cfg", "docsrs", "--cfg", "lettre_ignore_tls_mismatch"]
|
||||||
|
|
||||||
[[example]]
|
[[example]]
|
||||||
name = "basic_html"
|
name = "basic_html"
|
||||||
|
|||||||
11
README.md
11
README.md
@@ -28,8 +28,8 @@
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div align="center">
|
<div align="center">
|
||||||
<a href="https://deps.rs/crate/lettre/0.10.0-rc.1">
|
<a href="https://deps.rs/crate/lettre/0.10.0-rc.4">
|
||||||
<img src="https://deps.rs/crate/lettre/0.10.0-rc.1/status.svg"
|
<img src="https://deps.rs/crate/lettre/0.10.0-rc.4/status.svg"
|
||||||
alt="dependency status" />
|
alt="dependency status" />
|
||||||
</a>
|
</a>
|
||||||
</div>
|
</div>
|
||||||
@@ -65,13 +65,13 @@ Lettre does not provide (for now):
|
|||||||
|
|
||||||
## Example
|
## Example
|
||||||
|
|
||||||
This library requires Rust 1.46 or newer.
|
This library requires Rust 1.52.1 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
|
||||||
[dependencies]
|
[dependencies]
|
||||||
lettre = "0.10.0-rc.1"
|
lettre = "0.10.0-rc.4"
|
||||||
```
|
```
|
||||||
|
|
||||||
```rust,no_run
|
```rust,no_run
|
||||||
@@ -103,7 +103,8 @@ match mailer.send(&email) {
|
|||||||
|
|
||||||
## Testing
|
## Testing
|
||||||
|
|
||||||
The `lettre` tests require an open mail server listening locally on port 2525 and the `sendmail` command.
|
The `lettre` tests require an open mail server listening locally on port 2525 and the `sendmail` command. If you have python installed
|
||||||
|
such a server can be launched with `python -m smtpd -n -c DebuggingServer localhost:2525`
|
||||||
|
|
||||||
Alternatively only unit tests can be run by doing `cargo test --lib`.
|
Alternatively only unit tests can be run by doing `cargo test --lib`.
|
||||||
|
|
||||||
|
|||||||
@@ -174,15 +174,10 @@ impl FromStr for Address {
|
|||||||
type Err = AddressError;
|
type Err = AddressError;
|
||||||
|
|
||||||
fn from_str(val: &str) -> Result<Self, AddressError> {
|
fn from_str(val: &str) -> Result<Self, AddressError> {
|
||||||
let mut parts = val.rsplitn(2, '@');
|
let at_start = check_address(val)?;
|
||||||
let domain = parts.next().ok_or(AddressError::MissingParts)?;
|
|
||||||
let user = parts.next().ok_or(AddressError::MissingParts)?;
|
|
||||||
|
|
||||||
Address::check_user(user)?;
|
|
||||||
Address::check_domain(domain)?;
|
|
||||||
Ok(Address {
|
Ok(Address {
|
||||||
serialized: val.into(),
|
serialized: val.into(),
|
||||||
at_start: user.len(),
|
at_start,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -209,6 +204,18 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl TryFrom<String> for Address {
|
||||||
|
type Error = AddressError;
|
||||||
|
|
||||||
|
fn try_from(serialized: String) -> Result<Self, AddressError> {
|
||||||
|
let at_start = check_address(&serialized)?;
|
||||||
|
Ok(Address {
|
||||||
|
serialized,
|
||||||
|
at_start,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl AsRef<str> for Address {
|
impl AsRef<str> for Address {
|
||||||
fn as_ref(&self) -> &str {
|
fn as_ref(&self) -> &str {
|
||||||
&self.serialized
|
&self.serialized
|
||||||
@@ -221,6 +228,16 @@ impl AsRef<OsStr> for Address {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn check_address(val: &str) -> Result<usize, AddressError> {
|
||||||
|
let mut parts = val.rsplitn(2, '@');
|
||||||
|
let domain = parts.next().ok_or(AddressError::MissingParts)?;
|
||||||
|
let user = parts.next().ok_or(AddressError::MissingParts)?;
|
||||||
|
|
||||||
|
Address::check_user(user)?;
|
||||||
|
Address::check_domain(domain)?;
|
||||||
|
Ok(user.len())
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Clone, Copy)]
|
#[derive(Debug, PartialEq, Clone, Copy)]
|
||||||
/// Errors in email addresses parsing
|
/// Errors in email addresses parsing
|
||||||
pub enum AddressError {
|
pub enum AddressError {
|
||||||
|
|||||||
118
src/executor.rs
118
src/executor.rs
@@ -1,10 +1,16 @@
|
|||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
|
#[cfg(all(feature = "smtp-transport", feature = "async-std1"))]
|
||||||
|
use futures_util::future::BoxFuture;
|
||||||
|
|
||||||
use std::fmt::Debug;
|
use std::fmt::Debug;
|
||||||
|
#[cfg(feature = "smtp-transport")]
|
||||||
|
use std::future::Future;
|
||||||
#[cfg(feature = "file-transport")]
|
#[cfg(feature = "file-transport")]
|
||||||
use std::io::Result as IoResult;
|
use std::io::Result as IoResult;
|
||||||
#[cfg(feature = "file-transport")]
|
#[cfg(feature = "file-transport")]
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
#[cfg(feature = "smtp-transport")]
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
#[cfg(all(
|
#[cfg(all(
|
||||||
feature = "smtp-transport",
|
feature = "smtp-transport",
|
||||||
@@ -37,12 +43,29 @@ use crate::transport::smtp::Error;
|
|||||||
/// [`AsyncFileTransport`]: crate::AsyncFileTransport
|
/// [`AsyncFileTransport`]: crate::AsyncFileTransport
|
||||||
#[cfg_attr(docsrs, doc(cfg(any(feature = "tokio1", feature = "async-std1"))))]
|
#[cfg_attr(docsrs, doc(cfg(any(feature = "tokio1", feature = "async-std1"))))]
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
pub trait Executor: Debug + Send + Sync + private::Sealed {
|
pub trait Executor: Debug + Send + Sync + 'static + private::Sealed {
|
||||||
|
#[cfg(feature = "smtp-transport")]
|
||||||
|
type Handle: SpawnHandle;
|
||||||
|
#[cfg(feature = "smtp-transport")]
|
||||||
|
type Sleep: Future<Output = ()> + Send + 'static;
|
||||||
|
|
||||||
|
#[doc(hidden)]
|
||||||
|
#[cfg(feature = "smtp-transport")]
|
||||||
|
fn spawn<F>(fut: F) -> Self::Handle
|
||||||
|
where
|
||||||
|
F: Future<Output = ()> + Send + 'static,
|
||||||
|
F::Output: Send + 'static;
|
||||||
|
|
||||||
|
#[doc(hidden)]
|
||||||
|
#[cfg(feature = "smtp-transport")]
|
||||||
|
fn sleep(duration: Duration) -> Self::Sleep;
|
||||||
|
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
#[cfg(feature = "smtp-transport")]
|
#[cfg(feature = "smtp-transport")]
|
||||||
async fn connect(
|
async fn connect(
|
||||||
hostname: &str,
|
hostname: &str,
|
||||||
port: u16,
|
port: u16,
|
||||||
|
timeout: Option<Duration>,
|
||||||
hello_name: &ClientId,
|
hello_name: &ClientId,
|
||||||
tls: &Tls,
|
tls: &Tls,
|
||||||
) -> Result<AsyncSmtpConnection, Error>;
|
) -> Result<AsyncSmtpConnection, Error>;
|
||||||
@@ -56,6 +79,13 @@ pub trait Executor: Debug + Send + Sync + private::Sealed {
|
|||||||
async fn fs_write(path: &Path, contents: &[u8]) -> IoResult<()>;
|
async fn fs_write(path: &Path, contents: &[u8]) -> IoResult<()>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[doc(hidden)]
|
||||||
|
#[cfg(feature = "smtp-transport")]
|
||||||
|
#[async_trait]
|
||||||
|
pub trait SpawnHandle: Debug + Send + Sync + 'static + private::Sealed {
|
||||||
|
async fn shutdown(self);
|
||||||
|
}
|
||||||
|
|
||||||
/// Async [`Executor`] using `tokio` `1.x`
|
/// Async [`Executor`] using `tokio` `1.x`
|
||||||
///
|
///
|
||||||
/// Used by [`AsyncSmtpTransport`], [`AsyncSendmailTransport`] and [`AsyncFileTransport`]
|
/// Used by [`AsyncSmtpTransport`], [`AsyncSendmailTransport`] and [`AsyncFileTransport`]
|
||||||
@@ -74,11 +104,33 @@ pub struct Tokio1Executor;
|
|||||||
#[async_trait]
|
#[async_trait]
|
||||||
#[cfg(feature = "tokio1")]
|
#[cfg(feature = "tokio1")]
|
||||||
impl Executor for Tokio1Executor {
|
impl Executor for Tokio1Executor {
|
||||||
|
#[cfg(feature = "smtp-transport")]
|
||||||
|
type Handle = tokio1_crate::task::JoinHandle<()>;
|
||||||
|
#[cfg(feature = "smtp-transport")]
|
||||||
|
type Sleep = tokio1_crate::time::Sleep;
|
||||||
|
|
||||||
|
#[doc(hidden)]
|
||||||
|
#[cfg(feature = "smtp-transport")]
|
||||||
|
fn spawn<F>(fut: F) -> Self::Handle
|
||||||
|
where
|
||||||
|
F: Future<Output = ()> + Send + 'static,
|
||||||
|
F::Output: Send + 'static,
|
||||||
|
{
|
||||||
|
tokio1_crate::spawn(fut)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[doc(hidden)]
|
||||||
|
#[cfg(feature = "smtp-transport")]
|
||||||
|
fn sleep(duration: Duration) -> Self::Sleep {
|
||||||
|
tokio1_crate::time::sleep(duration)
|
||||||
|
}
|
||||||
|
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
#[cfg(feature = "smtp-transport")]
|
#[cfg(feature = "smtp-transport")]
|
||||||
async fn connect(
|
async fn connect(
|
||||||
hostname: &str,
|
hostname: &str,
|
||||||
port: u16,
|
port: u16,
|
||||||
|
timeout: Option<Duration>,
|
||||||
hello_name: &ClientId,
|
hello_name: &ClientId,
|
||||||
tls: &Tls,
|
tls: &Tls,
|
||||||
) -> Result<AsyncSmtpConnection, Error> {
|
) -> Result<AsyncSmtpConnection, Error> {
|
||||||
@@ -89,8 +141,13 @@ impl Executor for Tokio1Executor {
|
|||||||
_ => None,
|
_ => None,
|
||||||
};
|
};
|
||||||
#[allow(unused_mut)]
|
#[allow(unused_mut)]
|
||||||
let mut conn =
|
let mut conn = AsyncSmtpConnection::connect_tokio1(
|
||||||
AsyncSmtpConnection::connect_tokio1(hostname, port, hello_name, tls_parameters).await?;
|
(hostname, port),
|
||||||
|
timeout,
|
||||||
|
hello_name,
|
||||||
|
tls_parameters,
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
|
||||||
#[cfg(any(feature = "tokio1-native-tls", feature = "tokio1-rustls-tls"))]
|
#[cfg(any(feature = "tokio1-native-tls", feature = "tokio1-rustls-tls"))]
|
||||||
match tls {
|
match tls {
|
||||||
@@ -121,6 +178,14 @@ impl Executor for Tokio1Executor {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(all(feature = "smtp-transport", feature = "tokio1"))]
|
||||||
|
#[async_trait]
|
||||||
|
impl SpawnHandle for tokio1_crate::task::JoinHandle<()> {
|
||||||
|
async fn shutdown(self) {
|
||||||
|
self.abort();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Async [`Executor`] using `async-std` `1.x`
|
/// Async [`Executor`] using `async-std` `1.x`
|
||||||
///
|
///
|
||||||
/// Used by [`AsyncSmtpTransport`], [`AsyncSendmailTransport`] and [`AsyncFileTransport`]
|
/// Used by [`AsyncSmtpTransport`], [`AsyncSendmailTransport`] and [`AsyncFileTransport`]
|
||||||
@@ -139,11 +204,34 @@ pub struct AsyncStd1Executor;
|
|||||||
#[async_trait]
|
#[async_trait]
|
||||||
#[cfg(feature = "async-std1")]
|
#[cfg(feature = "async-std1")]
|
||||||
impl Executor for AsyncStd1Executor {
|
impl Executor for AsyncStd1Executor {
|
||||||
|
#[cfg(feature = "smtp-transport")]
|
||||||
|
type Handle = async_std::task::JoinHandle<()>;
|
||||||
|
#[cfg(feature = "smtp-transport")]
|
||||||
|
type Sleep = BoxFuture<'static, ()>;
|
||||||
|
|
||||||
|
#[doc(hidden)]
|
||||||
|
#[cfg(feature = "smtp-transport")]
|
||||||
|
fn spawn<F>(fut: F) -> Self::Handle
|
||||||
|
where
|
||||||
|
F: Future<Output = ()> + Send + 'static,
|
||||||
|
F::Output: Send + 'static,
|
||||||
|
{
|
||||||
|
async_std::task::spawn(fut)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[doc(hidden)]
|
||||||
|
#[cfg(feature = "smtp-transport")]
|
||||||
|
fn sleep(duration: Duration) -> Self::Sleep {
|
||||||
|
let fut = async move { async_std::task::sleep(duration).await };
|
||||||
|
Box::pin(fut)
|
||||||
|
}
|
||||||
|
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
#[cfg(feature = "smtp-transport")]
|
#[cfg(feature = "smtp-transport")]
|
||||||
async fn connect(
|
async fn connect(
|
||||||
hostname: &str,
|
hostname: &str,
|
||||||
port: u16,
|
port: u16,
|
||||||
|
timeout: Option<Duration>,
|
||||||
hello_name: &ClientId,
|
hello_name: &ClientId,
|
||||||
tls: &Tls,
|
tls: &Tls,
|
||||||
) -> Result<AsyncSmtpConnection, Error> {
|
) -> Result<AsyncSmtpConnection, Error> {
|
||||||
@@ -154,9 +242,13 @@ impl Executor for AsyncStd1Executor {
|
|||||||
_ => None,
|
_ => None,
|
||||||
};
|
};
|
||||||
#[allow(unused_mut)]
|
#[allow(unused_mut)]
|
||||||
let mut conn =
|
let mut conn = AsyncSmtpConnection::connect_asyncstd1(
|
||||||
AsyncSmtpConnection::connect_asyncstd1(hostname, port, hello_name, tls_parameters)
|
(hostname, port),
|
||||||
.await?;
|
timeout,
|
||||||
|
hello_name,
|
||||||
|
tls_parameters,
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
|
||||||
#[cfg(any(feature = "async-std1-native-tls", feature = "async-std1-rustls-tls"))]
|
#[cfg(any(feature = "async-std1-native-tls", feature = "async-std1-rustls-tls"))]
|
||||||
match tls {
|
match tls {
|
||||||
@@ -187,6 +279,14 @@ impl Executor for AsyncStd1Executor {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(all(feature = "smtp-transport", feature = "async-std1"))]
|
||||||
|
#[async_trait]
|
||||||
|
impl SpawnHandle for async_std::task::JoinHandle<()> {
|
||||||
|
async fn shutdown(self) {
|
||||||
|
self.cancel().await;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
mod private {
|
mod private {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
@@ -197,4 +297,10 @@ mod private {
|
|||||||
|
|
||||||
#[cfg(feature = "async-std1")]
|
#[cfg(feature = "async-std1")]
|
||||||
impl Sealed for AsyncStd1Executor {}
|
impl Sealed for AsyncStd1Executor {}
|
||||||
|
|
||||||
|
#[cfg(all(feature = "smtp-transport", feature = "tokio1"))]
|
||||||
|
impl Sealed for tokio1_crate::task::JoinHandle<()> {}
|
||||||
|
|
||||||
|
#[cfg(all(feature = "smtp-transport", feature = "async-std1"))]
|
||||||
|
impl Sealed for async_std::task::JoinHandle<()> {}
|
||||||
}
|
}
|
||||||
|
|||||||
59
src/lib.rs
59
src/lib.rs
@@ -6,7 +6,7 @@
|
|||||||
//! * Secure defaults
|
//! * Secure defaults
|
||||||
//! * Async support
|
//! * Async support
|
||||||
//!
|
//!
|
||||||
//! Lettre requires Rust 1.46 or newer.
|
//! Lettre requires Rust 1.52.1 or newer.
|
||||||
//!
|
//!
|
||||||
//! ## Features
|
//! ## Features
|
||||||
//!
|
//!
|
||||||
@@ -27,7 +27,7 @@
|
|||||||
//! _Send emails using [`SMTP`]_
|
//! _Send emails using [`SMTP`]_
|
||||||
//!
|
//!
|
||||||
//! * **smtp-transport** 📫: Enable the SMTP transport
|
//! * **smtp-transport** 📫: Enable the SMTP transport
|
||||||
//! * **r2d2** 📫: Connection pool for SMTP transport
|
//! * **pool** 📫: Connection pool for SMTP transport
|
||||||
//! * **hostname** 📫: Try to use the actual system hostname for the SMTP `CLIENTID`
|
//! * **hostname** 📫: Try to use the actual system hostname for the SMTP `CLIENTID`
|
||||||
//!
|
//!
|
||||||
//! #### SMTP over TLS via the native-tls crate
|
//! #### SMTP over TLS via the native-tls crate
|
||||||
@@ -84,18 +84,21 @@
|
|||||||
//!
|
//!
|
||||||
//! * **serde**: Serialization/Deserialization of entities
|
//! * **serde**: Serialization/Deserialization of entities
|
||||||
//! * **tracing**: Logging using the `tracing` crate
|
//! * **tracing**: Logging using the `tracing` crate
|
||||||
|
//! * **mime03**: Allow creating a [`ContentType`] from an existing [mime 0.3] `Mime` struct
|
||||||
//!
|
//!
|
||||||
//! [`SMTP`]: crate::transport::smtp
|
//! [`SMTP`]: crate::transport::smtp
|
||||||
//! [`sendmail`]: crate::transport::sendmail
|
//! [`sendmail`]: crate::transport::sendmail
|
||||||
//! [`file`]: crate::transport::file
|
//! [`file`]: crate::transport::file
|
||||||
|
//! [`ContentType`]: crate::message::header::ContentType
|
||||||
//! [tokio]: https://docs.rs/tokio/1
|
//! [tokio]: https://docs.rs/tokio/1
|
||||||
//! [async-std]: https://docs.rs/async-std/1
|
//! [async-std]: https://docs.rs/async-std/1
|
||||||
//! [ring]: https://github.com/briansmith/ring#ring
|
//! [ring]: https://github.com/briansmith/ring#ring
|
||||||
//! [ring-support]: https://github.com/briansmith/ring#online-automated-testing
|
//! [ring-support]: https://github.com/briansmith/ring#online-automated-testing
|
||||||
//! [Tokio 1.x]: https://docs.rs/tokio/1
|
//! [Tokio 1.x]: https://docs.rs/tokio/1
|
||||||
//! [async-std 1.x]: https://docs.rs/async-std/1
|
//! [async-std 1.x]: https://docs.rs/async-std/1
|
||||||
|
//! [mime 0.3]: https://docs.rs/mime/0.3
|
||||||
|
|
||||||
#![doc(html_root_url = "https://docs.rs/crate/lettre/0.10.0-rc.1")]
|
#![doc(html_root_url = "https://docs.rs/crate/lettre/0.10.0-rc.4")]
|
||||||
#![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)]
|
||||||
@@ -109,6 +112,56 @@
|
|||||||
)]
|
)]
|
||||||
#![cfg_attr(docsrs, feature(doc_cfg))]
|
#![cfg_attr(docsrs, feature(doc_cfg))]
|
||||||
|
|
||||||
|
#[cfg(not(lettre_ignore_tls_mismatch))]
|
||||||
|
mod compiletime_checks {
|
||||||
|
#[cfg(all(
|
||||||
|
feature = "tokio1",
|
||||||
|
feature = "native-tls",
|
||||||
|
not(feature = "tokio1-native-tls")
|
||||||
|
))]
|
||||||
|
compile_error!("Lettre is being built with the `tokio1` and the `native-tls` features, but the `tokio1-native-tls` feature hasn't been turned on.
|
||||||
|
If you'd like to use rustls make sure that the `native-tls` hasn't been enabled by mistake (you may need to import lettre without default features)
|
||||||
|
If you're building a library which depends on lettre import it without default features and enable just the features you need.");
|
||||||
|
|
||||||
|
#[cfg(all(
|
||||||
|
feature = "tokio1",
|
||||||
|
feature = "rustls-tls",
|
||||||
|
not(feature = "tokio1-rustls-tls")
|
||||||
|
))]
|
||||||
|
compile_error!("Lettre is being built with the `tokio1` and the `rustls-tls` features, but the `tokio1-rustls-tls` feature hasn't been turned on.
|
||||||
|
If you'd like to use native-tls make sure that the `rustls-tls` hasn't been enabled by mistake.
|
||||||
|
If you're building a library which depends on lettre import it without default features and enable just the features you need.");
|
||||||
|
|
||||||
|
/*
|
||||||
|
#[cfg(all(
|
||||||
|
feature = "async-std1",
|
||||||
|
feature = "native-tls",
|
||||||
|
not(feature = "async-std1-native-tls")
|
||||||
|
))]
|
||||||
|
compile_error!("Lettre is being built with the `async-std1` and the `native-tls` features, but the `async-std1-native-tls` feature hasn't been turned on.
|
||||||
|
If you'd like to use rustls make sure that the `native-tls` hasn't been enabled by mistake (you may need to import lettre without default features)
|
||||||
|
If you're building a library which depends on lettre import it without default features and enable just the features you need.");
|
||||||
|
*/
|
||||||
|
#[cfg(all(
|
||||||
|
feature = "async-std1",
|
||||||
|
feature = "native-tls",
|
||||||
|
not(feature = "async-std1-native-tls")
|
||||||
|
))]
|
||||||
|
compile_error!("Lettre is being built with the `async-std1` and the `native-tls` features, but the async-std integration doesn't support native-tls yet.
|
||||||
|
If you'd like to work on the issue please take a look at https://github.com/lettre/lettre/issues/576.
|
||||||
|
If you'd like to use rustls make sure that the `native-tls` hasn't been enabled by mistake (you may need to import lettre without default features)
|
||||||
|
If you're building a library which depends on lettre import lettre without default features and enable just the features you need.");
|
||||||
|
|
||||||
|
#[cfg(all(
|
||||||
|
feature = "async-std1",
|
||||||
|
feature = "rustls-tls",
|
||||||
|
not(feature = "async-std1-rustls-tls")
|
||||||
|
))]
|
||||||
|
compile_error!("Lettre is being built with the `async-std1` and the `rustls-tls` features, but the `async-std1-rustls-tls` feature hasn't been turned on.
|
||||||
|
If you'd like to use native-tls make sure that the `rustls-tls` hasn't been enabled by mistake (you may need to import lettre without default features)
|
||||||
|
If you're building a library which depends on lettre import it without default features and enable just the features you need.");
|
||||||
|
}
|
||||||
|
|
||||||
pub mod address;
|
pub mod address;
|
||||||
pub mod error;
|
pub mod error;
|
||||||
#[cfg(any(feature = "tokio1", feature = "async-std1"))]
|
#[cfg(any(feature = "tokio1", feature = "async-std1"))]
|
||||||
|
|||||||
@@ -20,21 +20,75 @@ enum Disposition {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Attachment {
|
impl Attachment {
|
||||||
/// Creates a new attachment
|
/// Create a new attachment
|
||||||
|
///
|
||||||
|
/// This attachment will be displayed as a normal attachment,
|
||||||
|
/// with the chosen `filename` appearing as the file name.
|
||||||
|
///
|
||||||
|
/// ```rust
|
||||||
|
/// # use std::error::Error;
|
||||||
|
/// use std::fs;
|
||||||
|
///
|
||||||
|
/// use lettre::message::{Attachment, header::ContentType};
|
||||||
|
///
|
||||||
|
/// # fn main() -> Result<(), Box<dyn Error>> {
|
||||||
|
/// let filename = String::from("invoice.pdf");
|
||||||
|
/// # if false {
|
||||||
|
/// let filebody = fs::read("invoice.pdf")?;
|
||||||
|
/// # }
|
||||||
|
/// # let filebody = fs::read("docs/lettre.png")?;
|
||||||
|
/// let content_type = ContentType::parse("application/pdf").unwrap();
|
||||||
|
/// let attachment = Attachment::new(filename).body(filebody, content_type);
|
||||||
|
///
|
||||||
|
/// // The document `attachment` will show up as a normal attachment.
|
||||||
|
/// # Ok(())
|
||||||
|
/// # }
|
||||||
|
/// ```
|
||||||
pub fn new(filename: String) -> Self {
|
pub fn new(filename: String) -> Self {
|
||||||
Attachment {
|
Attachment {
|
||||||
disposition: Disposition::Attached(filename),
|
disposition: Disposition::Attached(filename),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Creates a new inline attachment
|
/// Create a new inline attachment
|
||||||
|
///
|
||||||
|
/// 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::{Attachment, header::ContentType};
|
||||||
|
///
|
||||||
|
/// # fn main() -> Result<(), Box<dyn Error>> {
|
||||||
|
/// let content_id = String::from("123");
|
||||||
|
/// # if false {
|
||||||
|
/// let filebody = fs::read("image.jpg")?;
|
||||||
|
/// # }
|
||||||
|
/// # let filebody = fs::read("docs/lettre.png")?;
|
||||||
|
/// let content_type = ContentType::parse("image/jpeg").unwrap();
|
||||||
|
/// let attachment = Attachment::new_inline(content_id).body(filebody, content_type);
|
||||||
|
///
|
||||||
|
/// // The image `attachment` will display inline into the email.
|
||||||
|
/// # Ok(())
|
||||||
|
/// # }
|
||||||
|
/// ```
|
||||||
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),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Build the attachment part
|
/// Build the attachment into a [`SinglePart`] which can then be used to build the rest of the email
|
||||||
|
///
|
||||||
|
/// Look at the [Complex MIME body example](crate::message#complex-mime-body)
|
||||||
|
/// to see how [`SinglePart`] can be put into the email.
|
||||||
pub fn body<T: IntoBody>(self, content: T, content_type: ContentType) -> SinglePart {
|
pub fn body<T: IntoBody>(self, content: T, content_type: ContentType) -> SinglePart {
|
||||||
let mut builder = SinglePart::builder();
|
let mut builder = SinglePart::builder();
|
||||||
builder = match self.disposition {
|
builder = match self.disposition {
|
||||||
|
|||||||
@@ -183,8 +183,8 @@ impl MaybeString {
|
|||||||
/// would result into an invalid encoded body.
|
/// would result into an invalid encoded body.
|
||||||
fn is_encoding_ok(&self, encoding: ContentTransferEncoding) -> bool {
|
fn is_encoding_ok(&self, encoding: ContentTransferEncoding) -> bool {
|
||||||
match encoding {
|
match encoding {
|
||||||
ContentTransferEncoding::SevenBit => is_7bit_encoded(&self),
|
ContentTransferEncoding::SevenBit => is_7bit_encoded(self),
|
||||||
ContentTransferEncoding::EightBit => is_8bit_encoded(&self),
|
ContentTransferEncoding::EightBit => is_8bit_encoded(self),
|
||||||
ContentTransferEncoding::Binary
|
ContentTransferEncoding::Binary
|
||||||
| ContentTransferEncoding::QuotedPrintable
|
| ContentTransferEncoding::QuotedPrintable
|
||||||
| ContentTransferEncoding::Base64 => true,
|
| ContentTransferEncoding::Base64 => true,
|
||||||
@@ -342,7 +342,7 @@ where
|
|||||||
|
|
||||||
/// In place conversion to CRLF line endings
|
/// In place conversion to CRLF line endings
|
||||||
fn in_place_crlf_line_endings(string: &mut String) {
|
fn in_place_crlf_line_endings(string: &mut String) {
|
||||||
let indices = find_all_lf_char_indices(&string);
|
let indices = find_all_lf_char_indices(string);
|
||||||
|
|
||||||
for i in indices {
|
for i in indices {
|
||||||
// this relies on `indices` being in reverse order
|
// this relies on `indices` being in reverse order
|
||||||
|
|||||||
@@ -11,7 +11,8 @@ use crate::BoxError;
|
|||||||
/// The `Message` builder takes care of choosing the most
|
/// The `Message` builder takes care of choosing the most
|
||||||
/// efficient encoding based on the chosen body, so in most
|
/// efficient encoding based on the chosen body, so in most
|
||||||
/// use-caches this header shouldn't be set manually.
|
/// use-caches this header shouldn't be set manually.
|
||||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||||
|
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||||
pub enum ContentTransferEncoding {
|
pub enum ContentTransferEncoding {
|
||||||
/// ASCII
|
/// ASCII
|
||||||
SevenBit,
|
SevenBit,
|
||||||
|
|||||||
@@ -11,8 +11,13 @@ use crate::BoxError;
|
|||||||
|
|
||||||
/// `Content-Type` of the body
|
/// `Content-Type` of the body
|
||||||
///
|
///
|
||||||
|
/// This struct can represent any valid [mime type], which can be parsed via
|
||||||
|
/// [`ContentType::parse`]. Constants are provided for the most-used mime-types.
|
||||||
|
///
|
||||||
/// Defined in [RFC2045](https://tools.ietf.org/html/rfc2045#section-5)
|
/// Defined in [RFC2045](https://tools.ietf.org/html/rfc2045#section-5)
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
///
|
||||||
|
/// [mime type]: https://www.iana.org/assignments/media-types/media-types.xhtml
|
||||||
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
pub struct ContentType(Mime);
|
pub struct ContentType(Mime);
|
||||||
|
|
||||||
impl ContentType {
|
impl ContentType {
|
||||||
@@ -62,6 +67,14 @@ impl FromStr for ContentType {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "mime03")]
|
||||||
|
#[cfg_attr(docsrs, doc(cfg(feature = "mime03")))]
|
||||||
|
impl From<Mime> for ContentType {
|
||||||
|
fn from(mime: Mime) -> Self {
|
||||||
|
Self::from_mime(mime)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// An error occurred while trying to [`ContentType::parse`].
|
/// An error occurred while trying to [`ContentType::parse`].
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct ContentTypeErr(mime::FromStrError);
|
pub struct ContentTypeErr(mime::FromStrError);
|
||||||
@@ -78,6 +91,60 @@ impl Display for ContentTypeErr {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// -- Serialization and Deserialization --
|
||||||
|
#[cfg(feature = "serde")]
|
||||||
|
mod serde {
|
||||||
|
use serde::de::{self, Deserialize, Deserializer, Visitor};
|
||||||
|
use serde::ser::{Serialize, Serializer};
|
||||||
|
|
||||||
|
use std::fmt;
|
||||||
|
|
||||||
|
use super::ContentType;
|
||||||
|
|
||||||
|
impl Serialize for ContentType {
|
||||||
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
|
where
|
||||||
|
S: Serializer,
|
||||||
|
{
|
||||||
|
serializer.serialize_newtype_struct("ContentType", &format!("{}", &self.0))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'de> Deserialize<'de> for ContentType {
|
||||||
|
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||||
|
where
|
||||||
|
D: Deserializer<'de>,
|
||||||
|
{
|
||||||
|
struct ContentTypeVisitor;
|
||||||
|
|
||||||
|
impl<'de> Visitor<'de> for ContentTypeVisitor {
|
||||||
|
type Value = ContentType;
|
||||||
|
|
||||||
|
// The error message which states what the Visitor expects to
|
||||||
|
// receive
|
||||||
|
fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
formatter.write_str("a ContentType string like `text/plain`")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn visit_str<E>(self, mime: &str) -> Result<ContentType, E>
|
||||||
|
where
|
||||||
|
E: de::Error,
|
||||||
|
{
|
||||||
|
match ContentType::parse(mime) {
|
||||||
|
Ok(content_type) => Ok(content_type),
|
||||||
|
Err(_) => Err(E::custom(format!(
|
||||||
|
"Couldn't parse the following MIME-Type: {}",
|
||||||
|
mime
|
||||||
|
))),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
deserializer.deserialize_str(ContentTypeVisitor)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
use super::ContentType;
|
use super::ContentType;
|
||||||
|
|||||||
@@ -26,6 +26,9 @@ mod mailbox;
|
|||||||
mod special;
|
mod special;
|
||||||
mod textual;
|
mod textual;
|
||||||
|
|
||||||
|
/// Represents an email header
|
||||||
|
///
|
||||||
|
/// Email header as defined in [RFC5322](https://datatracker.ietf.org/doc/html/rfc5322) and extensions.
|
||||||
pub trait Header: Clone {
|
pub trait Header: Clone {
|
||||||
fn name() -> HeaderName;
|
fn name() -> HeaderName;
|
||||||
|
|
||||||
@@ -160,7 +163,7 @@ impl Display for Headers {
|
|||||||
for (name, value) in &self.headers {
|
for (name, value) in &self.headers {
|
||||||
Display::fmt(name, f)?;
|
Display::fmt(name, f)?;
|
||||||
f.write_str(": ")?;
|
f.write_str(": ")?;
|
||||||
HeaderValueEncoder::encode(&name, &value, f)?;
|
HeaderValueEncoder::encode(name, value, f)?;
|
||||||
f.write_str("\r\n")?;
|
f.write_str("\r\n")?;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -237,7 +240,7 @@ impl HeaderName {
|
|||||||
|
|
||||||
impl Display for HeaderName {
|
impl Display for HeaderName {
|
||||||
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
|
||||||
f.write_str(&self)
|
f.write_str(self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -408,13 +411,14 @@ impl HeaderValueEncoder {
|
|||||||
let mut next_word = next_word;
|
let mut next_word = next_word;
|
||||||
|
|
||||||
while !next_word.is_empty() {
|
while !next_word.is_empty() {
|
||||||
if self.remaining_line_len() <= base64_len(1) {
|
let mut len = available_len_to_max_encode_len(self.remaining_line_len())
|
||||||
|
.min(next_word.len());
|
||||||
|
|
||||||
|
if len == 0 {
|
||||||
self.flush_encode_buf(f, false)?;
|
self.flush_encode_buf(f, false)?;
|
||||||
self.new_line(f)?;
|
self.new_line(f)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut len = available_len_to_max_encode_len(self.remaining_line_len())
|
|
||||||
.min(next_word.len());
|
|
||||||
// avoid slicing on a char boundary
|
// avoid slicing on a char boundary
|
||||||
while !next_word.is_char_boundary(len) {
|
while !next_word.is_char_boundary(len) {
|
||||||
len += 1;
|
len += 1;
|
||||||
@@ -797,4 +801,21 @@ mod tests {
|
|||||||
)
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn issue_653() {
|
||||||
|
let mut headers = Headers::new();
|
||||||
|
headers.insert_raw(
|
||||||
|
HeaderName::new_from_ascii_str("Subject"),
|
||||||
|
"+仮名 :a;go; ;;;;;s;;;;;;;;;;;;;;;;fffeinmjgggggggggfっ".to_string(),
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
headers.to_string(),
|
||||||
|
concat!(
|
||||||
|
"Subject: =?utf-8?b?77yL5Luu5ZCN?= :a;go; \r\n",
|
||||||
|
" =?utf-8?b?Ozs7OztzOzs7Ozs7Ozs7Ozs7Ozs7O2ZmZmVpbm1qZ2dnZ2dnZ2dn772G44Gj?=\r\n"
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,6 +10,9 @@ pub struct MimeVersion {
|
|||||||
minor: u8,
|
minor: u8,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// MIME version 1.0
|
||||||
|
///
|
||||||
|
/// Should be used in all MIME messages.
|
||||||
pub const MIME_VERSION_1_0: MimeVersion = MimeVersion::new(1, 0);
|
pub const MIME_VERSION_1_0: MimeVersion = MimeVersion::new(1, 0);
|
||||||
|
|
||||||
impl MimeVersion {
|
impl MimeVersion {
|
||||||
|
|||||||
@@ -70,7 +70,7 @@ impl Display for Mailbox {
|
|||||||
if let Some(ref name) = self.name {
|
if let Some(ref name) = self.name {
|
||||||
let name = name.trim();
|
let name = name.trim();
|
||||||
if !name.is_empty() {
|
if !name.is_empty() {
|
||||||
f.write_str(&name)?;
|
f.write_str(name)?;
|
||||||
f.write_str(" <")?;
|
f.write_str(" <")?;
|
||||||
self.email.fmt(f)?;
|
self.email.fmt(f)?;
|
||||||
return f.write_char('>');
|
return f.write_char('>');
|
||||||
|
|||||||
@@ -4,15 +4,8 @@
|
|||||||
//!
|
//!
|
||||||
//! This section demonstrates how to build messages.
|
//! This section demonstrates how to build messages.
|
||||||
//!
|
//!
|
||||||
//! <!--
|
|
||||||
//! style for <details><summary>Blablabla</summary> Lots of stuff</details>
|
|
||||||
//! borrowed from https://docs.rs/time/0.2.23/src/time/lib.rs.html#49-54
|
|
||||||
//! -->
|
|
||||||
//! <style>
|
//! <style>
|
||||||
//! summary, details:not([open]) { cursor: pointer; }
|
//! summary, details:not([open]) { cursor: pointer; }
|
||||||
//! summary { display: list-item; }
|
|
||||||
//! summary::marker { content: '▶ '; }
|
|
||||||
//! details[open] summary::marker { content: '▼ '; }
|
|
||||||
//! </style>
|
//! </style>
|
||||||
//!
|
//!
|
||||||
//!
|
//!
|
||||||
@@ -401,7 +394,7 @@ impl MessageBuilder {
|
|||||||
// https://tools.ietf.org/html/rfc5322#section-3.6
|
// https://tools.ietf.org/html/rfc5322#section-3.6
|
||||||
|
|
||||||
// Insert Date if missing
|
// Insert Date if missing
|
||||||
let res = if self.headers.get::<header::Date>().is_none() {
|
let mut res = if self.headers.get::<header::Date>().is_none() {
|
||||||
self.date_now()
|
self.date_now()
|
||||||
} else {
|
} else {
|
||||||
self
|
self
|
||||||
@@ -424,6 +417,10 @@ impl MessageBuilder {
|
|||||||
Some(e) => e,
|
Some(e) => e,
|
||||||
None => Envelope::try_from(&res.headers)?,
|
None => Envelope::try_from(&res.headers)?,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Remove `Bcc` headers now the envelope is set
|
||||||
|
res.headers.remove::<header::Bcc>();
|
||||||
|
|
||||||
Ok(Message {
|
Ok(Message {
|
||||||
headers: res.headers,
|
headers: res.headers,
|
||||||
body,
|
body,
|
||||||
@@ -503,7 +500,7 @@ impl EmailFormat for Message {
|
|||||||
MessageBody::Mime(p) => p.format(out),
|
MessageBody::Mime(p) => p.format(out),
|
||||||
MessageBody::Raw(r) => {
|
MessageBody::Raw(r) => {
|
||||||
out.extend_from_slice(b"\r\n");
|
out.extend_from_slice(b"\r\n");
|
||||||
out.extend_from_slice(&r)
|
out.extend_from_slice(r)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -559,6 +556,7 @@ mod test {
|
|||||||
|
|
||||||
let email = Message::builder()
|
let email = Message::builder()
|
||||||
.date(date)
|
.date(date)
|
||||||
|
.bcc("hidden@example.com".parse().unwrap())
|
||||||
.header(header::From(
|
.header(header::From(
|
||||||
vec![Mailbox::new(
|
vec![Mailbox::new(
|
||||||
Some("Каи".into()),
|
Some("Каи".into()),
|
||||||
|
|||||||
@@ -152,7 +152,7 @@ pub trait AsyncTransport {
|
|||||||
async fn send(&self, message: Message) -> Result<Self::Ok, Self::Error> {
|
async fn send(&self, message: Message) -> Result<Self::Ok, Self::Error> {
|
||||||
let raw = message.formatted();
|
let raw = message.formatted();
|
||||||
let envelope = message.envelope();
|
let envelope = message.envelope();
|
||||||
self.send_raw(&envelope, &raw).await
|
self.send_raw(envelope, &raw).await
|
||||||
}
|
}
|
||||||
|
|
||||||
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>;
|
||||||
|
|||||||
@@ -92,7 +92,7 @@ use std::{
|
|||||||
|
|
||||||
mod error;
|
mod error;
|
||||||
|
|
||||||
const DEFAULT_SENDMAIL: &str = "/usr/sbin/sendmail";
|
const DEFAULT_SENDMAIL: &str = "sendmail";
|
||||||
|
|
||||||
/// Sends emails using the `sendmail` command
|
/// Sends emails using the `sendmail` command
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
@@ -113,7 +113,10 @@ pub struct AsyncSendmailTransport<E: Executor> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl SendmailTransport {
|
impl SendmailTransport {
|
||||||
/// Creates a new transport with the default `/usr/sbin/sendmail` command
|
/// Creates a new transport with the `sendmail` command
|
||||||
|
///
|
||||||
|
/// Note: This uses the `sendmail` command in the current `PATH`. To use another command,
|
||||||
|
/// use [SendmailTransport::new_with_command].
|
||||||
pub fn new() -> SendmailTransport {
|
pub fn new() -> SendmailTransport {
|
||||||
SendmailTransport {
|
SendmailTransport {
|
||||||
command: DEFAULT_SENDMAIL.into(),
|
command: DEFAULT_SENDMAIL.into(),
|
||||||
@@ -147,7 +150,10 @@ impl<E> AsyncSendmailTransport<E>
|
|||||||
where
|
where
|
||||||
E: Executor,
|
E: Executor,
|
||||||
{
|
{
|
||||||
/// Creates a new transport with the default `/usr/sbin/sendmail` command
|
/// Creates a new transport with the `sendmail` command
|
||||||
|
///
|
||||||
|
/// Note: This uses the `sendmail` command in the current `PATH`. To use another command,
|
||||||
|
/// use [AsyncSendmailTransport::new_with_command].
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Self {
|
Self {
|
||||||
inner: SendmailTransport::new(),
|
inner: SendmailTransport::new(),
|
||||||
@@ -260,7 +266,7 @@ impl AsyncTransport for AsyncSendmailTransport<AsyncStd1Executor> {
|
|||||||
.stdin
|
.stdin
|
||||||
.as_mut()
|
.as_mut()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.write_all(&email)
|
.write_all(email)
|
||||||
.await
|
.await
|
||||||
.map_err(error::client)?;
|
.map_err(error::client)?;
|
||||||
let output = process.output().await.map_err(error::client)?;
|
let output = process.output().await.map_err(error::client)?;
|
||||||
@@ -292,7 +298,7 @@ impl AsyncTransport for AsyncSendmailTransport<Tokio1Executor> {
|
|||||||
.stdin
|
.stdin
|
||||||
.as_mut()
|
.as_mut()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.write_all(&email)
|
.write_all(email)
|
||||||
.await
|
.await
|
||||||
.map_err(error::client)?;
|
.map_err(error::client)?;
|
||||||
let output = process.wait_with_output().await.map_err(error::client)?;
|
let output = process.wait_with_output().await.map_err(error::client)?;
|
||||||
|
|||||||
@@ -1,10 +1,16 @@
|
|||||||
use std::{
|
use std::{
|
||||||
fmt::{self, Debug},
|
fmt::{self, Debug},
|
||||||
marker::PhantomData,
|
marker::PhantomData,
|
||||||
|
sync::Arc,
|
||||||
|
time::Duration,
|
||||||
};
|
};
|
||||||
|
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
|
|
||||||
|
#[cfg(feature = "pool")]
|
||||||
|
use super::pool::async_impl::Pool;
|
||||||
|
#[cfg(feature = "pool")]
|
||||||
|
use super::PoolConfig;
|
||||||
use super::{
|
use super::{
|
||||||
client::AsyncSmtpConnection, ClientId, Credentials, Error, Mechanism, Response, SmtpInfo,
|
client::AsyncSmtpConnection, ClientId, Credentials, Error, Mechanism, Response, SmtpInfo,
|
||||||
};
|
};
|
||||||
@@ -18,8 +24,10 @@ use crate::{Envelope, Executor};
|
|||||||
|
|
||||||
/// Asynchronously sends emails using the SMTP protocol
|
/// Asynchronously sends emails using the SMTP protocol
|
||||||
#[cfg_attr(docsrs, doc(cfg(any(feature = "tokio1", feature = "async-std1"))))]
|
#[cfg_attr(docsrs, doc(cfg(any(feature = "tokio1", feature = "async-std1"))))]
|
||||||
pub struct AsyncSmtpTransport<E> {
|
pub struct AsyncSmtpTransport<E: Executor> {
|
||||||
// TODO: pool
|
#[cfg(feature = "pool")]
|
||||||
|
inner: Arc<Pool<E>>,
|
||||||
|
#[cfg(not(feature = "pool"))]
|
||||||
inner: AsyncSmtpClient<E>,
|
inner: AsyncSmtpClient<E>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -35,6 +43,7 @@ impl AsyncTransport for AsyncSmtpTransport<Tokio1Executor> {
|
|||||||
|
|
||||||
let result = conn.send(envelope, email).await?;
|
let result = conn.send(envelope, email).await?;
|
||||||
|
|
||||||
|
#[cfg(not(feature = "pool"))]
|
||||||
conn.quit().await?;
|
conn.quit().await?;
|
||||||
|
|
||||||
Ok(result)
|
Ok(result)
|
||||||
@@ -141,21 +150,41 @@ where
|
|||||||
///
|
///
|
||||||
/// * No authentication
|
/// * No authentication
|
||||||
/// * No TLS
|
/// * No TLS
|
||||||
|
/// * A 60 seconds timeout for smtp commands
|
||||||
/// * Port 25
|
/// * Port 25
|
||||||
///
|
///
|
||||||
/// Consider using [`AsyncSmtpTransport::relay`](#method.relay) or
|
/// Consider using [`AsyncSmtpTransport::relay`](#method.relay) or
|
||||||
/// [`AsyncSmtpTransport::starttls_relay`](#method.starttls_relay) instead,
|
/// [`AsyncSmtpTransport::starttls_relay`](#method.starttls_relay) instead,
|
||||||
/// if possible.
|
/// if possible.
|
||||||
pub fn builder_dangerous<T: Into<String>>(server: T) -> AsyncSmtpTransportBuilder {
|
pub fn builder_dangerous<T: Into<String>>(server: T) -> AsyncSmtpTransportBuilder {
|
||||||
let new = SmtpInfo {
|
let info = SmtpInfo {
|
||||||
server: server.into(),
|
server: server.into(),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
};
|
};
|
||||||
AsyncSmtpTransportBuilder { info: new }
|
AsyncSmtpTransportBuilder {
|
||||||
|
info,
|
||||||
|
#[cfg(feature = "pool")]
|
||||||
|
pool_config: PoolConfig::default(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Tests the SMTP connection
|
||||||
|
///
|
||||||
|
/// `test_connection()` tests the connection by using the SMTP NOOP command.
|
||||||
|
/// The connection is closed afterwards if a connection pool is not used.
|
||||||
|
pub async fn test_connection(&self) -> Result<bool, Error> {
|
||||||
|
let mut conn = self.inner.connection().await?;
|
||||||
|
|
||||||
|
let is_connected = conn.test_connected().await;
|
||||||
|
|
||||||
|
#[cfg(not(feature = "pool"))]
|
||||||
|
conn.quit().await?;
|
||||||
|
|
||||||
|
Ok(is_connected)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<E> Debug for AsyncSmtpTransport<E> {
|
impl<E: Executor> Debug for AsyncSmtpTransport<E> {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
let mut builder = f.debug_struct("AsyncSmtpTransport");
|
let mut builder = f.debug_struct("AsyncSmtpTransport");
|
||||||
builder.field("inner", &self.inner);
|
builder.field("inner", &self.inner);
|
||||||
@@ -180,6 +209,8 @@ where
|
|||||||
#[cfg_attr(docsrs, doc(cfg(any(feature = "tokio1", feature = "async-std1"))))]
|
#[cfg_attr(docsrs, doc(cfg(any(feature = "tokio1", feature = "async-std1"))))]
|
||||||
pub struct AsyncSmtpTransportBuilder {
|
pub struct AsyncSmtpTransportBuilder {
|
||||||
info: SmtpInfo,
|
info: SmtpInfo,
|
||||||
|
#[cfg(feature = "pool")]
|
||||||
|
pool_config: PoolConfig,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Builder for the SMTP `AsyncSmtpTransport`
|
/// Builder for the SMTP `AsyncSmtpTransport`
|
||||||
@@ -208,6 +239,12 @@ impl AsyncSmtpTransportBuilder {
|
|||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Set the timeout duration
|
||||||
|
pub fn timeout(mut self, timeout: Option<Duration>) -> Self {
|
||||||
|
self.info.timeout = timeout;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
/// Set the TLS settings to use
|
/// Set the TLS settings to use
|
||||||
#[cfg(any(
|
#[cfg(any(
|
||||||
feature = "tokio1-native-tls",
|
feature = "tokio1-native-tls",
|
||||||
@@ -228,6 +265,16 @@ impl AsyncSmtpTransportBuilder {
|
|||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Use a custom configuration for the connection pool
|
||||||
|
///
|
||||||
|
/// Defaults can be found at [`PoolConfig`]
|
||||||
|
#[cfg(feature = "pool")]
|
||||||
|
#[cfg_attr(docsrs, doc(cfg(feature = "pool")))]
|
||||||
|
pub fn pool_config(mut self, pool_config: PoolConfig) -> Self {
|
||||||
|
self.pool_config = pool_config;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
/// Build the transport
|
/// Build the transport
|
||||||
pub fn build<E>(self) -> AsyncSmtpTransport<E>
|
pub fn build<E>(self) -> AsyncSmtpTransport<E>
|
||||||
where
|
where
|
||||||
@@ -237,6 +284,10 @@ impl AsyncSmtpTransportBuilder {
|
|||||||
info: self.info,
|
info: self.info,
|
||||||
marker_: PhantomData,
|
marker_: PhantomData,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#[cfg(feature = "pool")]
|
||||||
|
let client = Pool::new(self.pool_config, client);
|
||||||
|
|
||||||
AsyncSmtpTransport { inner: client }
|
AsyncSmtpTransport { inner: client }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -258,13 +309,14 @@ where
|
|||||||
let mut conn = E::connect(
|
let mut conn = E::connect(
|
||||||
&self.info.server,
|
&self.info.server,
|
||||||
self.info.port,
|
self.info.port,
|
||||||
|
self.info.timeout,
|
||||||
&self.info.hello_name,
|
&self.info.hello_name,
|
||||||
&self.info.tls,
|
&self.info.tls,
|
||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
if let Some(credentials) = &self.info.credentials {
|
if let Some(credentials) = &self.info.credentials {
|
||||||
conn.auth(&self.info.authentication, &credentials).await?;
|
conn.auth(&self.info.authentication, credentials).await?;
|
||||||
}
|
}
|
||||||
Ok(conn)
|
Ok(conn)
|
||||||
}
|
}
|
||||||
@@ -278,6 +330,8 @@ impl<E> Debug for AsyncSmtpClient<E> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// `clone` is unused when the `pool` feature is on
|
||||||
|
#[allow(dead_code)]
|
||||||
impl<E> AsyncSmtpClient<E>
|
impl<E> AsyncSmtpClient<E>
|
||||||
where
|
where
|
||||||
E: Executor,
|
E: Executor,
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ use crate::transport::smtp::error::{self, Error};
|
|||||||
use std::fmt::{self, Debug, Display, Formatter};
|
use std::fmt::{self, Debug, Display, Formatter};
|
||||||
|
|
||||||
/// Accepted authentication mechanisms
|
/// Accepted authentication mechanisms
|
||||||
|
///
|
||||||
/// Trying LOGIN last as it is deprecated.
|
/// Trying LOGIN last as it is deprecated.
|
||||||
pub const DEFAULT_MECHANISMS: &[Mechanism] = &[Mechanism::Plain, Mechanism::Login];
|
pub const DEFAULT_MECHANISMS: &[Mechanism] = &[Mechanism::Plain, Mechanism::Login];
|
||||||
|
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ use crate::{
|
|||||||
Envelope,
|
Envelope,
|
||||||
};
|
};
|
||||||
use futures_util::io::{AsyncBufReadExt, AsyncWriteExt, BufReader};
|
use futures_util::io::{AsyncBufReadExt, AsyncWriteExt, BufReader};
|
||||||
use std::fmt::Display;
|
use std::{fmt::Display, time::Duration};
|
||||||
|
|
||||||
#[cfg(feature = "tracing")]
|
#[cfg(feature = "tracing")]
|
||||||
use super::escape_crlf;
|
use super::escape_crlf;
|
||||||
@@ -48,13 +48,13 @@ impl AsyncSmtpConnection {
|
|||||||
///
|
///
|
||||||
/// Sends EHLO and parses server information
|
/// Sends EHLO and parses server information
|
||||||
#[cfg(feature = "tokio1")]
|
#[cfg(feature = "tokio1")]
|
||||||
pub async fn connect_tokio1(
|
pub async fn connect_tokio1<T: tokio1_crate::net::ToSocketAddrs>(
|
||||||
hostname: &str,
|
server: T,
|
||||||
port: u16,
|
timeout: Option<Duration>,
|
||||||
hello_name: &ClientId,
|
hello_name: &ClientId,
|
||||||
tls_parameters: Option<TlsParameters>,
|
tls_parameters: Option<TlsParameters>,
|
||||||
) -> Result<AsyncSmtpConnection, Error> {
|
) -> Result<AsyncSmtpConnection, Error> {
|
||||||
let stream = AsyncNetworkStream::connect_tokio1(hostname, port, tls_parameters).await?;
|
let stream = AsyncNetworkStream::connect_tokio1(server, timeout, tls_parameters).await?;
|
||||||
Self::connect_impl(stream, hello_name).await
|
Self::connect_impl(stream, hello_name).await
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -62,13 +62,13 @@ impl AsyncSmtpConnection {
|
|||||||
///
|
///
|
||||||
/// Sends EHLO and parses server information
|
/// Sends EHLO and parses server information
|
||||||
#[cfg(feature = "async-std1")]
|
#[cfg(feature = "async-std1")]
|
||||||
pub async fn connect_asyncstd1(
|
pub async fn connect_asyncstd1<T: async_std::net::ToSocketAddrs>(
|
||||||
hostname: &str,
|
server: T,
|
||||||
port: u16,
|
timeout: Option<Duration>,
|
||||||
hello_name: &ClientId,
|
hello_name: &ClientId,
|
||||||
tls_parameters: Option<TlsParameters>,
|
tls_parameters: Option<TlsParameters>,
|
||||||
) -> Result<AsyncSmtpConnection, Error> {
|
) -> Result<AsyncSmtpConnection, Error> {
|
||||||
let stream = AsyncNetworkStream::connect_asyncstd1(hostname, port, tls_parameters).await?;
|
let stream = AsyncNetworkStream::connect_asyncstd1(server, timeout, tls_parameters).await?;
|
||||||
Self::connect_impl(stream, hello_name).await
|
Self::connect_impl(stream, hello_name).await
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -161,10 +161,7 @@ impl AsyncSmtpConnection {
|
|||||||
) -> Result<(), Error> {
|
) -> Result<(), Error> {
|
||||||
if self.server_info.supports_feature(Extension::StartTls) {
|
if self.server_info.supports_feature(Extension::StartTls) {
|
||||||
try_smtp!(self.command(Starttls).await, self);
|
try_smtp!(self.command(Starttls).await, self);
|
||||||
try_smtp!(
|
self.stream.get_mut().upgrade_tls(tls_parameters).await?;
|
||||||
self.stream.get_mut().upgrade_tls(tls_parameters).await,
|
|
||||||
self
|
|
||||||
);
|
|
||||||
#[cfg(feature = "tracing")]
|
#[cfg(feature = "tracing")]
|
||||||
tracing::debug!("connection encrypted");
|
tracing::debug!("connection encrypted");
|
||||||
// Send EHLO again
|
// Send EHLO again
|
||||||
@@ -298,7 +295,7 @@ impl AsyncSmtpConnection {
|
|||||||
return if response.is_positive() {
|
return if response.is_positive() {
|
||||||
Ok(response)
|
Ok(response)
|
||||||
} else {
|
} else {
|
||||||
Err(error::code(response.code))
|
Err(error::code(response.code()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(nom::Err::Failure(e)) => {
|
Err(nom::Err::Failure(e)) => {
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
use std::{
|
use std::{
|
||||||
mem,
|
io, mem,
|
||||||
net::SocketAddr,
|
net::SocketAddr,
|
||||||
pin::Pin,
|
pin::Pin,
|
||||||
task::{Context, Poll},
|
task::{Context, Poll},
|
||||||
|
time::Duration,
|
||||||
};
|
};
|
||||||
|
|
||||||
use futures_io::{
|
use futures_io::{
|
||||||
@@ -13,9 +14,9 @@ use futures_io::{
|
|||||||
use tokio1_crate::io::{AsyncRead as _, AsyncWrite as _, ReadBuf as Tokio1ReadBuf};
|
use tokio1_crate::io::{AsyncRead as _, AsyncWrite as _, ReadBuf as Tokio1ReadBuf};
|
||||||
|
|
||||||
#[cfg(feature = "async-std1")]
|
#[cfg(feature = "async-std1")]
|
||||||
use async_std::net::TcpStream as AsyncStd1TcpStream;
|
use async_std::net::{TcpStream as AsyncStd1TcpStream, ToSocketAddrs as AsyncStd1ToSocketAddrs};
|
||||||
#[cfg(feature = "tokio1")]
|
#[cfg(feature = "tokio1")]
|
||||||
use tokio1_crate::net::TcpStream as Tokio1TcpStream;
|
use tokio1_crate::net::{TcpStream as Tokio1TcpStream, ToSocketAddrs as Tokio1ToSocketAddrs};
|
||||||
|
|
||||||
#[cfg(feature = "async-std1-native-tls")]
|
#[cfg(feature = "async-std1-native-tls")]
|
||||||
use async_native_tls::TlsStream as AsyncStd1TlsStream;
|
use async_native_tls::TlsStream as AsyncStd1TlsStream;
|
||||||
@@ -23,7 +24,7 @@ use async_native_tls::TlsStream as AsyncStd1TlsStream;
|
|||||||
use tokio1_native_tls_crate::TlsStream as Tokio1TlsStream;
|
use tokio1_native_tls_crate::TlsStream as Tokio1TlsStream;
|
||||||
|
|
||||||
#[cfg(feature = "async-std1-rustls-tls")]
|
#[cfg(feature = "async-std1-rustls-tls")]
|
||||||
use async_rustls::client::TlsStream as AsyncStd1RustlsTlsStream;
|
use futures_rustls::client::TlsStream as AsyncStd1RustlsTlsStream;
|
||||||
#[cfg(feature = "tokio1-rustls-tls")]
|
#[cfg(feature = "tokio1-rustls-tls")]
|
||||||
use tokio1_rustls::client::TlsStream as Tokio1RustlsTlsStream;
|
use tokio1_rustls::client::TlsStream as Tokio1RustlsTlsStream;
|
||||||
|
|
||||||
@@ -107,14 +108,47 @@ impl AsyncNetworkStream {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "tokio1")]
|
#[cfg(feature = "tokio1")]
|
||||||
pub async fn connect_tokio1(
|
pub async fn connect_tokio1<T: Tokio1ToSocketAddrs>(
|
||||||
hostname: &str,
|
server: T,
|
||||||
port: u16,
|
timeout: Option<Duration>,
|
||||||
tls_parameters: Option<TlsParameters>,
|
tls_parameters: Option<TlsParameters>,
|
||||||
) -> Result<AsyncNetworkStream, Error> {
|
) -> Result<AsyncNetworkStream, Error> {
|
||||||
let tcp_stream = Tokio1TcpStream::connect((hostname, port))
|
async fn try_connect_timeout<T: Tokio1ToSocketAddrs>(
|
||||||
.await
|
server: T,
|
||||||
.map_err(error::connection)?;
|
timeout: Duration,
|
||||||
|
) -> Result<Tokio1TcpStream, Error> {
|
||||||
|
let addrs = tokio1_crate::net::lookup_host(server)
|
||||||
|
.await
|
||||||
|
.map_err(error::connection)?;
|
||||||
|
|
||||||
|
let mut last_err = None;
|
||||||
|
|
||||||
|
for addr in addrs {
|
||||||
|
let connect_future = Tokio1TcpStream::connect(&addr);
|
||||||
|
match tokio1_crate::time::timeout(timeout, connect_future).await {
|
||||||
|
Ok(Ok(stream)) => return Ok(stream),
|
||||||
|
Ok(Err(err)) => last_err = Some(err),
|
||||||
|
Err(_) => {
|
||||||
|
last_err = Some(io::Error::new(
|
||||||
|
io::ErrorKind::TimedOut,
|
||||||
|
"connection timed out",
|
||||||
|
))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Err(match last_err {
|
||||||
|
Some(last_err) => error::connection(last_err),
|
||||||
|
None => error::connection("could not resolve to any address"),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
let tcp_stream = match timeout {
|
||||||
|
Some(t) => try_connect_timeout(server, t).await?,
|
||||||
|
None => Tokio1TcpStream::connect(server)
|
||||||
|
.await
|
||||||
|
.map_err(error::connection)?,
|
||||||
|
};
|
||||||
|
|
||||||
let mut stream = AsyncNetworkStream::new(InnerAsyncNetworkStream::Tokio1Tcp(tcp_stream));
|
let mut stream = AsyncNetworkStream::new(InnerAsyncNetworkStream::Tokio1Tcp(tcp_stream));
|
||||||
if let Some(tls_parameters) = tls_parameters {
|
if let Some(tls_parameters) = tls_parameters {
|
||||||
@@ -124,14 +158,45 @@ impl AsyncNetworkStream {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "async-std1")]
|
#[cfg(feature = "async-std1")]
|
||||||
pub async fn connect_asyncstd1(
|
pub async fn connect_asyncstd1<T: AsyncStd1ToSocketAddrs>(
|
||||||
hostname: &str,
|
server: T,
|
||||||
port: u16,
|
timeout: Option<Duration>,
|
||||||
tls_parameters: Option<TlsParameters>,
|
tls_parameters: Option<TlsParameters>,
|
||||||
) -> Result<AsyncNetworkStream, Error> {
|
) -> Result<AsyncNetworkStream, Error> {
|
||||||
let tcp_stream = AsyncStd1TcpStream::connect((hostname, port))
|
async fn try_connect_timeout<T: AsyncStd1ToSocketAddrs>(
|
||||||
.await
|
server: T,
|
||||||
.map_err(error::connection)?;
|
timeout: Duration,
|
||||||
|
) -> Result<AsyncStd1TcpStream, Error> {
|
||||||
|
let addrs = server.to_socket_addrs().await.map_err(error::connection)?;
|
||||||
|
|
||||||
|
let mut last_err = None;
|
||||||
|
|
||||||
|
for addr in addrs {
|
||||||
|
let connect_future = AsyncStd1TcpStream::connect(&addr);
|
||||||
|
match async_std::future::timeout(timeout, connect_future).await {
|
||||||
|
Ok(Ok(stream)) => return Ok(stream),
|
||||||
|
Ok(Err(err)) => last_err = Some(err),
|
||||||
|
Err(_) => {
|
||||||
|
last_err = Some(io::Error::new(
|
||||||
|
io::ErrorKind::TimedOut,
|
||||||
|
"connection timed out",
|
||||||
|
))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Err(match last_err {
|
||||||
|
Some(last_err) => error::connection(last_err),
|
||||||
|
None => error::connection("could not resolve to any address"),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
let tcp_stream = match timeout {
|
||||||
|
Some(t) => try_connect_timeout(server, t).await?,
|
||||||
|
None => AsyncStd1TcpStream::connect(server)
|
||||||
|
.await
|
||||||
|
.map_err(error::connection)?,
|
||||||
|
};
|
||||||
|
|
||||||
let mut stream = AsyncNetworkStream::new(InnerAsyncNetworkStream::AsyncStd1Tcp(tcp_stream));
|
let mut stream = AsyncNetworkStream::new(InnerAsyncNetworkStream::AsyncStd1Tcp(tcp_stream));
|
||||||
if let Some(tls_parameters) = tls_parameters {
|
if let Some(tls_parameters) = tls_parameters {
|
||||||
@@ -196,9 +261,9 @@ impl AsyncNetworkStream {
|
|||||||
#[cfg(any(feature = "tokio1-native-tls", feature = "tokio1-rustls-tls"))]
|
#[cfg(any(feature = "tokio1-native-tls", feature = "tokio1-rustls-tls"))]
|
||||||
async fn upgrade_tokio1_tls(
|
async fn upgrade_tokio1_tls(
|
||||||
tcp_stream: Tokio1TcpStream,
|
tcp_stream: Tokio1TcpStream,
|
||||||
mut tls_parameters: TlsParameters,
|
tls_parameters: TlsParameters,
|
||||||
) -> Result<InnerAsyncNetworkStream, Error> {
|
) -> Result<InnerAsyncNetworkStream, Error> {
|
||||||
let domain = mem::take(&mut tls_parameters.domain);
|
let domain = tls_parameters.domain().to_string();
|
||||||
|
|
||||||
match tls_parameters.connector {
|
match tls_parameters.connector {
|
||||||
#[cfg(feature = "native-tls")]
|
#[cfg(feature = "native-tls")]
|
||||||
@@ -225,10 +290,13 @@ impl AsyncNetworkStream {
|
|||||||
|
|
||||||
#[cfg(feature = "tokio1-rustls-tls")]
|
#[cfg(feature = "tokio1-rustls-tls")]
|
||||||
return {
|
return {
|
||||||
use tokio1_rustls::{webpki::DNSNameRef, TlsConnector};
|
use std::convert::TryFrom;
|
||||||
|
|
||||||
let domain =
|
use rustls::ServerName;
|
||||||
DNSNameRef::try_from_ascii_str(&domain).map_err(error::connection)?;
|
use tokio1_rustls::TlsConnector;
|
||||||
|
|
||||||
|
let domain = ServerName::try_from(domain.as_str())
|
||||||
|
.map_err(|_| error::connection("domain isn't a valid DNS name"))?;
|
||||||
|
|
||||||
let connector = TlsConnector::from(config);
|
let connector = TlsConnector::from(config);
|
||||||
let stream = connector
|
let stream = connector
|
||||||
@@ -277,10 +345,13 @@ impl AsyncNetworkStream {
|
|||||||
|
|
||||||
#[cfg(feature = "async-std1-rustls-tls")]
|
#[cfg(feature = "async-std1-rustls-tls")]
|
||||||
return {
|
return {
|
||||||
use async_rustls::{webpki::DNSNameRef, TlsConnector};
|
use std::convert::TryFrom;
|
||||||
|
|
||||||
let domain =
|
use futures_rustls::TlsConnector;
|
||||||
DNSNameRef::try_from_ascii_str(&domain).map_err(error::connection)?;
|
use rustls::ServerName;
|
||||||
|
|
||||||
|
let domain = ServerName::try_from(domain.as_str())
|
||||||
|
.map_err(|_| error::connection("domain isn't a valid DNS name"))?;
|
||||||
|
|
||||||
let connector = TlsConnector::from(config);
|
let connector = TlsConnector::from(config);
|
||||||
let stream = connector
|
let stream = connector
|
||||||
|
|||||||
@@ -145,7 +145,7 @@ impl SmtpConnection {
|
|||||||
#[cfg(any(feature = "native-tls", feature = "rustls-tls"))]
|
#[cfg(any(feature = "native-tls", feature = "rustls-tls"))]
|
||||||
{
|
{
|
||||||
try_smtp!(self.command(Starttls), self);
|
try_smtp!(self.command(Starttls), self);
|
||||||
try_smtp!(self.stream.get_mut().upgrade_tls(tls_parameters), self);
|
self.stream.get_mut().upgrade_tls(tls_parameters)?;
|
||||||
#[cfg(feature = "tracing")]
|
#[cfg(feature = "tracing")]
|
||||||
tracing::debug!("connection encrypted");
|
tracing::debug!("connection encrypted");
|
||||||
// Send EHLO again
|
// Send EHLO again
|
||||||
@@ -276,7 +276,7 @@ impl SmtpConnection {
|
|||||||
return if response.is_positive() {
|
return if response.is_positive() {
|
||||||
Ok(response)
|
Ok(response)
|
||||||
} else {
|
} else {
|
||||||
Err(error::code(response.code))
|
Err(error::code(response.code()))
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
Err(nom::Err::Failure(e)) => {
|
Err(nom::Err::Failure(e)) => {
|
||||||
|
|||||||
@@ -78,7 +78,15 @@ impl ClientCodec {
|
|||||||
match self.escape_count {
|
match self.escape_count {
|
||||||
0 => self.escape_count = if *byte == b'\r' { 1 } else { 0 },
|
0 => self.escape_count = if *byte == b'\r' { 1 } else { 0 },
|
||||||
1 => self.escape_count = if *byte == b'\n' { 2 } else { 0 },
|
1 => self.escape_count = if *byte == b'\n' { 2 } else { 0 },
|
||||||
2 => self.escape_count = if *byte == b'.' { 3 } else { 0 },
|
2 => {
|
||||||
|
self.escape_count = if *byte == b'.' {
|
||||||
|
3
|
||||||
|
} else if *byte == b'\r' {
|
||||||
|
1
|
||||||
|
} else {
|
||||||
|
0
|
||||||
|
}
|
||||||
|
}
|
||||||
_ => unreachable!(),
|
_ => unreachable!(),
|
||||||
}
|
}
|
||||||
if self.escape_count == 3 {
|
if self.escape_count == 3 {
|
||||||
@@ -111,6 +119,7 @@ mod test {
|
|||||||
let mut buf: Vec<u8> = vec![];
|
let mut buf: Vec<u8> = vec![];
|
||||||
|
|
||||||
codec.encode(b"test\r\n", &mut buf);
|
codec.encode(b"test\r\n", &mut buf);
|
||||||
|
codec.encode(b"test\r\n\r\n", &mut buf);
|
||||||
codec.encode(b".\r\n", &mut buf);
|
codec.encode(b".\r\n", &mut buf);
|
||||||
codec.encode(b"\r\ntest", &mut buf);
|
codec.encode(b"\r\ntest", &mut buf);
|
||||||
codec.encode(b"te\r\n.\r\nst", &mut buf);
|
codec.encode(b"te\r\n.\r\nst", &mut buf);
|
||||||
@@ -121,7 +130,7 @@ mod test {
|
|||||||
codec.encode(b"test", &mut buf);
|
codec.encode(b"test", &mut buf);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
String::from_utf8(buf).unwrap(),
|
String::from_utf8(buf).unwrap(),
|
||||||
"test\r\n..\r\n\r\ntestte\r\n..\r\nsttesttest.test\n.test\ntest"
|
"test\r\ntest\r\n\r\n..\r\n\r\ntestte\r\n..\r\nsttesttest.test\n.test\ntest"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
#[cfg(feature = "rustls-tls")]
|
||||||
|
use std::convert::TryFrom;
|
||||||
use std::{
|
use std::{
|
||||||
io::{self, Read, Write},
|
io::{self, Read, Write},
|
||||||
mem,
|
mem,
|
||||||
@@ -9,7 +11,7 @@ use std::{
|
|||||||
use native_tls::TlsStream;
|
use native_tls::TlsStream;
|
||||||
|
|
||||||
#[cfg(feature = "rustls-tls")]
|
#[cfg(feature = "rustls-tls")]
|
||||||
use rustls::{ClientSession, StreamOwned};
|
use rustls::{ClientConnection, ServerName, StreamOwned};
|
||||||
|
|
||||||
#[cfg(any(feature = "native-tls", feature = "rustls-tls"))]
|
#[cfg(any(feature = "native-tls", feature = "rustls-tls"))]
|
||||||
use super::InnerTlsParameters;
|
use super::InnerTlsParameters;
|
||||||
@@ -33,7 +35,7 @@ enum InnerNetworkStream {
|
|||||||
NativeTls(TlsStream<TcpStream>),
|
NativeTls(TlsStream<TcpStream>),
|
||||||
/// Encrypted TCP stream
|
/// Encrypted TCP stream
|
||||||
#[cfg(feature = "rustls-tls")]
|
#[cfg(feature = "rustls-tls")]
|
||||||
RustlsTls(StreamOwned<ClientSession, TcpStream>),
|
RustlsTls(StreamOwned<ClientConnection, TcpStream>),
|
||||||
/// Can't be built
|
/// Can't be built
|
||||||
None,
|
None,
|
||||||
}
|
}
|
||||||
@@ -90,12 +92,20 @@ impl NetworkStream {
|
|||||||
timeout: Duration,
|
timeout: Duration,
|
||||||
) -> Result<TcpStream, Error> {
|
) -> Result<TcpStream, Error> {
|
||||||
let addrs = server.to_socket_addrs().map_err(error::connection)?;
|
let addrs = server.to_socket_addrs().map_err(error::connection)?;
|
||||||
|
|
||||||
|
let mut last_err = None;
|
||||||
|
|
||||||
for addr in addrs {
|
for addr in addrs {
|
||||||
if let Ok(result) = TcpStream::connect_timeout(&addr, timeout) {
|
match TcpStream::connect_timeout(&addr, timeout) {
|
||||||
return Ok(result);
|
Ok(stream) => return Ok(stream),
|
||||||
|
Err(err) => last_err = Some(err),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(error::connection("Could not connect"))
|
|
||||||
|
Err(match last_err {
|
||||||
|
Some(last_err) => error::connection(last_err),
|
||||||
|
None => error::connection("could not resolve to any address"),
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
let tcp_stream = match timeout {
|
let tcp_stream = match timeout {
|
||||||
@@ -149,12 +159,11 @@ impl NetworkStream {
|
|||||||
}
|
}
|
||||||
#[cfg(feature = "rustls-tls")]
|
#[cfg(feature = "rustls-tls")]
|
||||||
InnerTlsParameters::RustlsTls(connector) => {
|
InnerTlsParameters::RustlsTls(connector) => {
|
||||||
use webpki::DNSNameRef;
|
let domain = ServerName::try_from(tls_parameters.domain())
|
||||||
|
.map_err(|_| error::connection("domain isn't a valid DNS name"))?;
|
||||||
let domain = DNSNameRef::try_from_ascii_str(tls_parameters.domain())
|
let connection =
|
||||||
.map_err(error::connection)?;
|
ClientConnection::new(connector.clone(), domain).map_err(error::connection)?;
|
||||||
let stream = StreamOwned::new(ClientSession::new(&connector, domain), tcp_stream);
|
let stream = StreamOwned::new(connection, tcp_stream);
|
||||||
|
|
||||||
InnerNetworkStream::RustlsTls(stream)
|
InnerNetworkStream::RustlsTls(stream)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -3,12 +3,13 @@ use crate::transport::smtp::{error, Error};
|
|||||||
#[cfg(feature = "native-tls")]
|
#[cfg(feature = "native-tls")]
|
||||||
use native_tls::{Protocol, TlsConnector};
|
use native_tls::{Protocol, TlsConnector};
|
||||||
#[cfg(feature = "rustls-tls")]
|
#[cfg(feature = "rustls-tls")]
|
||||||
use rustls::{ClientConfig, RootCertStore, ServerCertVerified, ServerCertVerifier, TLSError};
|
use rustls::{
|
||||||
|
client::{ServerCertVerified, ServerCertVerifier, WebPkiVerifier},
|
||||||
|
ClientConfig, Error as TlsError, OwnedTrustAnchor, RootCertStore, ServerName,
|
||||||
|
};
|
||||||
use std::fmt::{self, Debug};
|
use std::fmt::{self, Debug};
|
||||||
#[cfg(feature = "rustls-tls")]
|
#[cfg(feature = "rustls-tls")]
|
||||||
use std::sync::Arc;
|
use std::{sync::Arc, time::SystemTime};
|
||||||
#[cfg(feature = "rustls-tls")]
|
|
||||||
use webpki::DNSNameRef;
|
|
||||||
|
|
||||||
/// Accepted protocols by default.
|
/// Accepted protocols by default.
|
||||||
/// This removes TLS 1.0 and 1.1 compared to tls-native defaults.
|
/// This removes TLS 1.0 and 1.1 compared to tls-native defaults.
|
||||||
@@ -163,21 +164,35 @@ impl TlsParametersBuilder {
|
|||||||
#[cfg(feature = "rustls-tls")]
|
#[cfg(feature = "rustls-tls")]
|
||||||
#[cfg_attr(docsrs, doc(cfg(feature = "rustls-tls")))]
|
#[cfg_attr(docsrs, doc(cfg(feature = "rustls-tls")))]
|
||||||
pub fn build_rustls(self) -> Result<TlsParameters, Error> {
|
pub fn build_rustls(self) -> Result<TlsParameters, Error> {
|
||||||
use webpki_roots::TLS_SERVER_ROOTS;
|
let tls = ClientConfig::builder();
|
||||||
|
let tls = tls.with_safe_defaults();
|
||||||
|
|
||||||
let mut tls = ClientConfig::new();
|
let tls = if self.accept_invalid_certs {
|
||||||
|
tls.with_custom_certificate_verifier(Arc::new(InvalidCertsVerifier {}))
|
||||||
for cert in self.root_certs {
|
} else {
|
||||||
for rustls_cert in cert.rustls {
|
let mut root_cert_store = RootCertStore::empty();
|
||||||
tls.root_store.add(&rustls_cert).map_err(error::tls)?;
|
for cert in self.root_certs {
|
||||||
|
for rustls_cert in cert.rustls {
|
||||||
|
root_cert_store.add(&rustls_cert).map_err(error::tls)?;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
root_cert_store.add_server_trust_anchors(webpki_roots::TLS_SERVER_ROOTS.0.iter().map(
|
||||||
if self.accept_invalid_certs {
|
|ta| {
|
||||||
tls.dangerous()
|
OwnedTrustAnchor::from_subject_spki_name_constraints(
|
||||||
.set_certificate_verifier(Arc::new(InvalidCertsVerifier {}));
|
ta.subject,
|
||||||
}
|
ta.spki,
|
||||||
|
ta.name_constraints,
|
||||||
|
)
|
||||||
|
},
|
||||||
|
));
|
||||||
|
|
||||||
|
tls.with_custom_certificate_verifier(Arc::new(WebPkiVerifier::new(
|
||||||
|
root_cert_store,
|
||||||
|
None,
|
||||||
|
)))
|
||||||
|
};
|
||||||
|
let tls = tls.with_no_client_auth();
|
||||||
|
|
||||||
tls.root_store.add_server_trust_anchors(&TLS_SERVER_ROOTS);
|
|
||||||
Ok(TlsParameters {
|
Ok(TlsParameters {
|
||||||
connector: InnerTlsParameters::RustlsTls(Arc::new(tls)),
|
connector: InnerTlsParameters::RustlsTls(Arc::new(tls)),
|
||||||
domain: self.domain,
|
domain: self.domain,
|
||||||
@@ -257,11 +272,14 @@ impl Certificate {
|
|||||||
|
|
||||||
#[cfg(feature = "rustls-tls")]
|
#[cfg(feature = "rustls-tls")]
|
||||||
let rustls_cert = {
|
let rustls_cert = {
|
||||||
use rustls::internal::pemfile;
|
|
||||||
use std::io::Cursor;
|
use std::io::Cursor;
|
||||||
|
|
||||||
let mut pem = Cursor::new(pem);
|
let mut pem = Cursor::new(pem);
|
||||||
pemfile::certs(&mut pem).map_err(|_| error::tls("invalid certificates"))?
|
rustls_pemfile::certs(&mut pem)
|
||||||
|
.map_err(|_| error::tls("invalid certificates"))?
|
||||||
|
.into_iter()
|
||||||
|
.map(rustls::Certificate)
|
||||||
|
.collect::<Vec<_>>()
|
||||||
};
|
};
|
||||||
|
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
@@ -286,11 +304,13 @@ struct InvalidCertsVerifier;
|
|||||||
impl ServerCertVerifier for InvalidCertsVerifier {
|
impl ServerCertVerifier for InvalidCertsVerifier {
|
||||||
fn verify_server_cert(
|
fn verify_server_cert(
|
||||||
&self,
|
&self,
|
||||||
_roots: &RootCertStore,
|
_end_entity: &rustls::Certificate,
|
||||||
_presented_certs: &[rustls::Certificate],
|
_intermediates: &[rustls::Certificate],
|
||||||
_dns_name: DNSNameRef<'_>,
|
_server_name: &ServerName,
|
||||||
|
_scts: &mut dyn Iterator<Item = &[u8]>,
|
||||||
_ocsp_response: &[u8],
|
_ocsp_response: &[u8],
|
||||||
) -> Result<ServerCertVerified, TLSError> {
|
_now: SystemTime,
|
||||||
|
) -> Result<ServerCertVerified, TlsError> {
|
||||||
Ok(ServerCertVerified::assertion())
|
Ok(ServerCertVerified::assertion())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -72,6 +72,7 @@ impl ClientId {
|
|||||||
/// Supported ESMTP keywords
|
/// Supported ESMTP keywords
|
||||||
#[derive(PartialEq, Eq, Hash, Copy, Clone, Debug)]
|
#[derive(PartialEq, Eq, Hash, Copy, Clone, Debug)]
|
||||||
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||||
|
#[non_exhaustive]
|
||||||
pub enum Extension {
|
pub enum Extension {
|
||||||
/// 8BITMIME keyword
|
/// 8BITMIME keyword
|
||||||
///
|
///
|
||||||
@@ -107,11 +108,11 @@ pub struct ServerInfo {
|
|||||||
/// Server name
|
/// Server name
|
||||||
///
|
///
|
||||||
/// The name given in the server banner
|
/// The name given in the server banner
|
||||||
pub name: String,
|
name: String,
|
||||||
/// ESMTP features supported by the server
|
/// ESMTP features supported by the server
|
||||||
///
|
///
|
||||||
/// It contains the features supported by the server and known by the `Extension` module.
|
/// It contains the features supported by the server and known by the `Extension` module.
|
||||||
pub features: HashSet<Extension>,
|
features: HashSet<Extension>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Display for ServerInfo {
|
impl Display for ServerInfo {
|
||||||
@@ -135,7 +136,7 @@ impl ServerInfo {
|
|||||||
|
|
||||||
let mut features: HashSet<Extension> = HashSet::new();
|
let mut features: HashSet<Extension> = HashSet::new();
|
||||||
|
|
||||||
for line in response.message.as_slice() {
|
for line in response.message() {
|
||||||
if line.is_empty() {
|
if line.is_empty() {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@@ -197,6 +198,11 @@ impl ServerInfo {
|
|||||||
}
|
}
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// The name given in the server banner
|
||||||
|
pub fn name(&self) -> &str {
|
||||||
|
self.name.as_ref()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A `MAIL FROM` extension parameter
|
/// A `MAIL FROM` extension parameter
|
||||||
|
|||||||
@@ -118,10 +118,8 @@
|
|||||||
|
|
||||||
#[cfg(any(feature = "tokio1", feature = "async-std1"))]
|
#[cfg(any(feature = "tokio1", feature = "async-std1"))]
|
||||||
pub use self::async_transport::{AsyncSmtpTransport, AsyncSmtpTransportBuilder};
|
pub use self::async_transport::{AsyncSmtpTransport, AsyncSmtpTransportBuilder};
|
||||||
#[cfg(feature = "r2d2")]
|
#[cfg(feature = "pool")]
|
||||||
pub use self::pool::PoolConfig;
|
pub use self::pool::PoolConfig;
|
||||||
#[cfg(feature = "r2d2")]
|
|
||||||
pub(crate) use self::transport::SmtpClient;
|
|
||||||
pub use self::{
|
pub use self::{
|
||||||
error::Error,
|
error::Error,
|
||||||
transport::{SmtpTransport, SmtpTransportBuilder},
|
transport::{SmtpTransport, SmtpTransportBuilder},
|
||||||
@@ -144,11 +142,11 @@ pub mod client;
|
|||||||
pub mod commands;
|
pub mod commands;
|
||||||
mod error;
|
mod error;
|
||||||
pub mod extension;
|
pub mod extension;
|
||||||
#[cfg(feature = "r2d2")]
|
#[cfg(feature = "pool")]
|
||||||
mod pool;
|
mod pool;
|
||||||
pub mod response;
|
pub mod response;
|
||||||
mod transport;
|
mod transport;
|
||||||
pub mod util;
|
pub(super) mod util;
|
||||||
|
|
||||||
// Registered port numbers:
|
// Registered port numbers:
|
||||||
// https://www.iana.
|
// https://www.iana.
|
||||||
@@ -164,7 +162,7 @@ pub const SUBMISSION_PORT: u16 = 587;
|
|||||||
pub const SUBMISSIONS_PORT: u16 = 465;
|
pub const SUBMISSIONS_PORT: u16 = 465;
|
||||||
|
|
||||||
/// Default timeout
|
/// Default timeout
|
||||||
pub const DEFAULT_TIMEOUT: Duration = Duration::from_secs(60);
|
const DEFAULT_TIMEOUT: Duration = Duration::from_secs(60);
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
struct SmtpInfo {
|
struct SmtpInfo {
|
||||||
|
|||||||
@@ -1,111 +0,0 @@
|
|||||||
use std::time::Duration;
|
|
||||||
|
|
||||||
use crate::transport::smtp::{client::SmtpConnection, error, error::Error, SmtpClient};
|
|
||||||
|
|
||||||
use r2d2::{CustomizeConnection, ManageConnection, Pool};
|
|
||||||
|
|
||||||
/// Configuration for a connection pool
|
|
||||||
#[derive(Debug, Clone)]
|
|
||||||
#[allow(missing_copy_implementations)]
|
|
||||||
#[cfg_attr(docsrs, doc(cfg(feature = "r2d2")))]
|
|
||||||
pub struct PoolConfig {
|
|
||||||
min_idle: u32,
|
|
||||||
max_size: u32,
|
|
||||||
connection_timeout: Duration,
|
|
||||||
idle_timeout: Duration,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl PoolConfig {
|
|
||||||
/// Create a new pool configuration with default values
|
|
||||||
pub fn new() -> Self {
|
|
||||||
Self::default()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Minimum number of idle connections
|
|
||||||
///
|
|
||||||
/// Defaults to `0`
|
|
||||||
pub fn min_idle(mut self, min_idle: u32) -> Self {
|
|
||||||
self.min_idle = min_idle;
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Maximum number of pooled connections
|
|
||||||
///
|
|
||||||
/// Defaults to `10`
|
|
||||||
pub fn max_size(mut self, max_size: u32) -> Self {
|
|
||||||
self.max_size = max_size;
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Connection timeout
|
|
||||||
///
|
|
||||||
/// Defaults to `30 seconds`
|
|
||||||
pub fn connection_timeout(mut self, connection_timeout: Duration) -> Self {
|
|
||||||
self.connection_timeout = connection_timeout;
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Connection idle timeout
|
|
||||||
///
|
|
||||||
/// Defaults to `60 seconds`
|
|
||||||
pub fn idle_timeout(mut self, idle_timeout: Duration) -> Self {
|
|
||||||
self.idle_timeout = idle_timeout;
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn build<C: ManageConnection<Connection = SmtpConnection, Error = Error>>(
|
|
||||||
&self,
|
|
||||||
client: C,
|
|
||||||
) -> Pool<C> {
|
|
||||||
Pool::builder()
|
|
||||||
.min_idle(Some(self.min_idle))
|
|
||||||
.max_size(self.max_size)
|
|
||||||
.connection_timeout(self.connection_timeout)
|
|
||||||
.idle_timeout(Some(self.idle_timeout))
|
|
||||||
.connection_customizer(Box::new(SmtpConnectionQuitter))
|
|
||||||
.build_unchecked(client)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Default for PoolConfig {
|
|
||||||
fn default() -> Self {
|
|
||||||
Self {
|
|
||||||
min_idle: 0,
|
|
||||||
max_size: 10,
|
|
||||||
connection_timeout: Duration::from_secs(30),
|
|
||||||
idle_timeout: Duration::from_secs(60),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ManageConnection for SmtpClient {
|
|
||||||
type Connection = SmtpConnection;
|
|
||||||
type Error = Error;
|
|
||||||
|
|
||||||
fn connect(&self) -> Result<Self::Connection, Error> {
|
|
||||||
self.connection()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn is_valid(&self, conn: &mut Self::Connection) -> Result<(), Error> {
|
|
||||||
if conn.test_connected() {
|
|
||||||
return Ok(());
|
|
||||||
}
|
|
||||||
Err(error::network("is not connected anymore"))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn has_broken(&self, conn: &mut Self::Connection) -> bool {
|
|
||||||
conn.has_broken()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Copy, Clone, Debug)]
|
|
||||||
struct SmtpConnectionQuitter;
|
|
||||||
|
|
||||||
impl CustomizeConnection<SmtpConnection, Error> for SmtpConnectionQuitter {
|
|
||||||
fn on_release(&self, conn: SmtpConnection) {
|
|
||||||
let mut conn = conn;
|
|
||||||
if !conn.has_broken() {
|
|
||||||
let _quit = conn.quit();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
299
src/transport/smtp/pool/async_impl.rs
Normal file
299
src/transport/smtp/pool/async_impl.rs
Normal file
@@ -0,0 +1,299 @@
|
|||||||
|
use std::fmt::{self, Debug};
|
||||||
|
use std::mem;
|
||||||
|
use std::ops::{Deref, DerefMut};
|
||||||
|
use std::sync::Arc;
|
||||||
|
use std::time::{Duration, Instant};
|
||||||
|
|
||||||
|
use futures_util::lock::Mutex;
|
||||||
|
use futures_util::stream::{self, StreamExt};
|
||||||
|
use once_cell::sync::OnceCell;
|
||||||
|
|
||||||
|
use crate::executor::SpawnHandle;
|
||||||
|
use crate::transport::smtp::async_transport::AsyncSmtpClient;
|
||||||
|
use crate::Executor;
|
||||||
|
|
||||||
|
use super::super::client::AsyncSmtpConnection;
|
||||||
|
use super::super::Error;
|
||||||
|
use super::PoolConfig;
|
||||||
|
|
||||||
|
pub struct Pool<E: Executor> {
|
||||||
|
config: PoolConfig,
|
||||||
|
connections: Mutex<Vec<ParkedConnection>>,
|
||||||
|
client: AsyncSmtpClient<E>,
|
||||||
|
handle: OnceCell<E::Handle>,
|
||||||
|
}
|
||||||
|
|
||||||
|
struct ParkedConnection {
|
||||||
|
conn: AsyncSmtpConnection,
|
||||||
|
since: Instant,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct PooledConnection<E: Executor> {
|
||||||
|
conn: Option<AsyncSmtpConnection>,
|
||||||
|
pool: Arc<Pool<E>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<E: Executor> Pool<E> {
|
||||||
|
pub fn new(config: PoolConfig, client: AsyncSmtpClient<E>) -> Arc<Self> {
|
||||||
|
let pool = Arc::new(Self {
|
||||||
|
config,
|
||||||
|
connections: Mutex::new(Vec::new()),
|
||||||
|
client,
|
||||||
|
handle: OnceCell::new(),
|
||||||
|
});
|
||||||
|
|
||||||
|
{
|
||||||
|
let pool_ = Arc::clone(&pool);
|
||||||
|
|
||||||
|
let min_idle = pool_.config.min_idle;
|
||||||
|
let idle_timeout = pool_.config.idle_timeout;
|
||||||
|
let pool = Arc::downgrade(&pool_);
|
||||||
|
|
||||||
|
let handle = E::spawn(async move {
|
||||||
|
loop {
|
||||||
|
#[cfg(feature = "tracing")]
|
||||||
|
tracing::trace!("running cleanup tasks");
|
||||||
|
|
||||||
|
match pool.upgrade() {
|
||||||
|
Some(pool) => {
|
||||||
|
#[allow(clippy::needless_collect)]
|
||||||
|
let (count, dropped) = {
|
||||||
|
let mut connections = pool.connections.lock().await;
|
||||||
|
|
||||||
|
let to_drop = connections
|
||||||
|
.iter()
|
||||||
|
.enumerate()
|
||||||
|
.rev()
|
||||||
|
.filter(|(_, conn)| conn.idle_duration() > idle_timeout)
|
||||||
|
.map(|(i, _)| i)
|
||||||
|
.collect::<Vec<_>>();
|
||||||
|
let dropped = to_drop
|
||||||
|
.into_iter()
|
||||||
|
.map(|i| connections.remove(i))
|
||||||
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
|
(connections.len(), dropped)
|
||||||
|
};
|
||||||
|
|
||||||
|
#[cfg(feature = "tracing")]
|
||||||
|
let mut created = 0;
|
||||||
|
for _ in count..=(min_idle as usize) {
|
||||||
|
let conn = match pool.client.connection().await {
|
||||||
|
Ok(conn) => conn,
|
||||||
|
Err(err) => {
|
||||||
|
#[cfg(feature = "tracing")]
|
||||||
|
tracing::warn!("couldn't create idle connection {}", err);
|
||||||
|
#[cfg(not(feature = "tracing"))]
|
||||||
|
let _ = err;
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut connections = pool.connections.lock().await;
|
||||||
|
connections.push(ParkedConnection::park(conn));
|
||||||
|
|
||||||
|
#[cfg(feature = "tracing")]
|
||||||
|
{
|
||||||
|
created += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "tracing")]
|
||||||
|
if created > 0 {
|
||||||
|
tracing::debug!("created {} idle connections", created);
|
||||||
|
}
|
||||||
|
|
||||||
|
if !dropped.is_empty() {
|
||||||
|
#[cfg(feature = "tracing")]
|
||||||
|
tracing::debug!("dropped {} idle connections", dropped.len());
|
||||||
|
|
||||||
|
abort_concurrent(dropped.into_iter().map(|conn| conn.unpark()))
|
||||||
|
.await;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None => {
|
||||||
|
#[cfg(feature = "tracing")]
|
||||||
|
tracing::warn!(
|
||||||
|
"breaking out of task - no more references to Pool are available"
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
E::sleep(idle_timeout).await;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
pool_
|
||||||
|
.handle
|
||||||
|
.set(handle)
|
||||||
|
.expect("handle hasn't been set yet");
|
||||||
|
}
|
||||||
|
|
||||||
|
pool
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn connection(self: &Arc<Self>) -> Result<PooledConnection<E>, Error> {
|
||||||
|
loop {
|
||||||
|
let conn = {
|
||||||
|
let mut connections = self.connections.lock().await;
|
||||||
|
connections.pop()
|
||||||
|
};
|
||||||
|
|
||||||
|
match conn {
|
||||||
|
Some(conn) => {
|
||||||
|
let mut conn = conn.unpark();
|
||||||
|
|
||||||
|
// TODO: handle the client try another connection if this one isn't good
|
||||||
|
if !conn.test_connected().await {
|
||||||
|
#[cfg(feature = "tracing")]
|
||||||
|
tracing::debug!("dropping a broken connection");
|
||||||
|
|
||||||
|
conn.abort().await;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "tracing")]
|
||||||
|
tracing::debug!("reusing a pooled connection");
|
||||||
|
|
||||||
|
return Ok(PooledConnection::wrap(conn, self.clone()));
|
||||||
|
}
|
||||||
|
None => {
|
||||||
|
#[cfg(feature = "tracing")]
|
||||||
|
tracing::debug!("creating a new connection");
|
||||||
|
|
||||||
|
let conn = self.client.connection().await?;
|
||||||
|
return Ok(PooledConnection::wrap(conn, self.clone()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn recycle(&self, mut conn: AsyncSmtpConnection) {
|
||||||
|
if conn.has_broken() {
|
||||||
|
#[cfg(feature = "tracing")]
|
||||||
|
tracing::debug!("dropping a broken connection instead of recycling it");
|
||||||
|
|
||||||
|
conn.abort().await;
|
||||||
|
drop(conn);
|
||||||
|
} else {
|
||||||
|
#[cfg(feature = "tracing")]
|
||||||
|
tracing::debug!("recycling connection");
|
||||||
|
|
||||||
|
let mut connections = self.connections.lock().await;
|
||||||
|
if connections.len() >= self.config.max_size as usize {
|
||||||
|
drop(connections);
|
||||||
|
conn.abort().await;
|
||||||
|
} else {
|
||||||
|
let conn = ParkedConnection::park(conn);
|
||||||
|
connections.push(conn);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<E: Executor> Debug for Pool<E> {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
f.debug_struct("Pool")
|
||||||
|
.field("config", &self.config)
|
||||||
|
.field(
|
||||||
|
"connections",
|
||||||
|
&match self.connections.try_lock() {
|
||||||
|
Some(connections) => format!("{} connections", connections.len()),
|
||||||
|
|
||||||
|
None => "LOCKED".to_string(),
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.field("client", &self.client)
|
||||||
|
.field(
|
||||||
|
"handle",
|
||||||
|
&match self.handle.get() {
|
||||||
|
Some(_) => "Some(JoinHandle)",
|
||||||
|
None => "None",
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.finish()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<E: Executor> Drop for Pool<E> {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
#[cfg(feature = "tracing")]
|
||||||
|
tracing::debug!("dropping Pool");
|
||||||
|
|
||||||
|
let connections = mem::take(self.connections.get_mut());
|
||||||
|
let handle = self.handle.take();
|
||||||
|
E::spawn(async move {
|
||||||
|
if let Some(handle) = handle {
|
||||||
|
handle.shutdown().await;
|
||||||
|
}
|
||||||
|
|
||||||
|
abort_concurrent(connections.into_iter().map(|conn| conn.unpark())).await;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ParkedConnection {
|
||||||
|
fn park(conn: AsyncSmtpConnection) -> Self {
|
||||||
|
Self {
|
||||||
|
conn,
|
||||||
|
since: Instant::now(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn idle_duration(&self) -> Duration {
|
||||||
|
self.since.elapsed()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn unpark(self) -> AsyncSmtpConnection {
|
||||||
|
self.conn
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<E: Executor> PooledConnection<E> {
|
||||||
|
fn wrap(conn: AsyncSmtpConnection, pool: Arc<Pool<E>>) -> Self {
|
||||||
|
Self {
|
||||||
|
conn: Some(conn),
|
||||||
|
pool,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<E: Executor> Deref for PooledConnection<E> {
|
||||||
|
type Target = AsyncSmtpConnection;
|
||||||
|
|
||||||
|
fn deref(&self) -> &Self::Target {
|
||||||
|
self.conn.as_ref().expect("conn hasn't been dropped yet")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<E: Executor> DerefMut for PooledConnection<E> {
|
||||||
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||||
|
self.conn.as_mut().expect("conn hasn't been dropped yet")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<E: Executor> Drop for PooledConnection<E> {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
let conn = self
|
||||||
|
.conn
|
||||||
|
.take()
|
||||||
|
.expect("AsyncSmtpConnection hasn't been taken yet");
|
||||||
|
let pool = Arc::clone(&self.pool);
|
||||||
|
|
||||||
|
E::spawn(async move {
|
||||||
|
pool.recycle(conn).await;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn abort_concurrent<I>(iter: I)
|
||||||
|
where
|
||||||
|
I: Iterator<Item = AsyncSmtpConnection>,
|
||||||
|
{
|
||||||
|
stream::iter(iter)
|
||||||
|
.for_each_concurrent(8, |mut conn| async move {
|
||||||
|
conn.abort().await;
|
||||||
|
})
|
||||||
|
.await;
|
||||||
|
}
|
||||||
66
src/transport/smtp/pool/mod.rs
Normal file
66
src/transport/smtp/pool/mod.rs
Normal file
@@ -0,0 +1,66 @@
|
|||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
|
#[cfg(any(feature = "tokio1", feature = "async-std1"))]
|
||||||
|
pub mod async_impl;
|
||||||
|
pub mod sync_impl;
|
||||||
|
|
||||||
|
/// Configuration for a connection pool
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
#[allow(missing_copy_implementations)]
|
||||||
|
#[cfg_attr(docsrs, doc(cfg(feature = "pool")))]
|
||||||
|
pub struct PoolConfig {
|
||||||
|
min_idle: u32,
|
||||||
|
max_size: u32,
|
||||||
|
idle_timeout: Duration,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PoolConfig {
|
||||||
|
/// Create a new pool configuration with default values
|
||||||
|
pub fn new() -> Self {
|
||||||
|
Self::default()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Minimum number of idle connections
|
||||||
|
///
|
||||||
|
/// Defaults to `0`
|
||||||
|
pub fn min_idle(mut self, min_idle: u32) -> Self {
|
||||||
|
self.min_idle = min_idle;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Maximum number of pooled connections
|
||||||
|
///
|
||||||
|
/// Defaults to `10`
|
||||||
|
pub fn max_size(mut self, max_size: u32) -> Self {
|
||||||
|
self.max_size = max_size;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Connection timeout
|
||||||
|
///
|
||||||
|
/// Defaults to `30 seconds`
|
||||||
|
#[doc(hidden)]
|
||||||
|
#[deprecated(note = "The Connection timeout is already configured on the SMTP transport")]
|
||||||
|
pub fn connection_timeout(self, connection_timeout: Duration) -> Self {
|
||||||
|
let _ = connection_timeout;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Connection idle timeout
|
||||||
|
///
|
||||||
|
/// Defaults to `60 seconds`
|
||||||
|
pub fn idle_timeout(mut self, idle_timeout: Duration) -> Self {
|
||||||
|
self.idle_timeout = idle_timeout;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for PoolConfig {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self {
|
||||||
|
min_idle: 0,
|
||||||
|
max_size: 10,
|
||||||
|
idle_timeout: Duration::from_secs(60),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
256
src/transport/smtp/pool/sync_impl.rs
Normal file
256
src/transport/smtp/pool/sync_impl.rs
Normal file
@@ -0,0 +1,256 @@
|
|||||||
|
use std::fmt::{self, Debug};
|
||||||
|
use std::ops::{Deref, DerefMut};
|
||||||
|
use std::sync::{Arc, Mutex, TryLockError};
|
||||||
|
use std::time::{Duration, Instant};
|
||||||
|
use std::{mem, thread};
|
||||||
|
|
||||||
|
use crate::transport::smtp::transport::SmtpClient;
|
||||||
|
|
||||||
|
use super::super::client::SmtpConnection;
|
||||||
|
use super::super::Error;
|
||||||
|
use super::PoolConfig;
|
||||||
|
|
||||||
|
pub struct Pool {
|
||||||
|
config: PoolConfig,
|
||||||
|
connections: Mutex<Vec<ParkedConnection>>,
|
||||||
|
client: SmtpClient,
|
||||||
|
}
|
||||||
|
|
||||||
|
struct ParkedConnection {
|
||||||
|
conn: SmtpConnection,
|
||||||
|
since: Instant,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct PooledConnection {
|
||||||
|
conn: Option<SmtpConnection>,
|
||||||
|
pool: Arc<Pool>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Pool {
|
||||||
|
pub fn new(config: PoolConfig, client: SmtpClient) -> Arc<Self> {
|
||||||
|
let pool = Arc::new(Self {
|
||||||
|
config,
|
||||||
|
connections: Mutex::new(Vec::new()),
|
||||||
|
client,
|
||||||
|
});
|
||||||
|
|
||||||
|
{
|
||||||
|
let pool_ = Arc::clone(&pool);
|
||||||
|
|
||||||
|
let min_idle = pool_.config.min_idle;
|
||||||
|
let idle_timeout = pool_.config.idle_timeout;
|
||||||
|
let pool = Arc::downgrade(&pool_);
|
||||||
|
|
||||||
|
thread::Builder::new()
|
||||||
|
.name("lettre-connection-pool".into())
|
||||||
|
.spawn(move || {
|
||||||
|
while let Some(pool) = pool.upgrade() {
|
||||||
|
#[cfg(feature = "tracing")]
|
||||||
|
tracing::trace!("running cleanup tasks");
|
||||||
|
|
||||||
|
#[allow(clippy::needless_collect)]
|
||||||
|
let (count, dropped) = {
|
||||||
|
let mut connections = pool.connections.lock().unwrap();
|
||||||
|
|
||||||
|
let to_drop = connections
|
||||||
|
.iter()
|
||||||
|
.enumerate()
|
||||||
|
.rev()
|
||||||
|
.filter(|(_, conn)| conn.idle_duration() > idle_timeout)
|
||||||
|
.map(|(i, _)| i)
|
||||||
|
.collect::<Vec<_>>();
|
||||||
|
let dropped = to_drop
|
||||||
|
.into_iter()
|
||||||
|
.map(|i| connections.remove(i))
|
||||||
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
|
(connections.len(), dropped)
|
||||||
|
};
|
||||||
|
|
||||||
|
#[cfg(feature = "tracing")]
|
||||||
|
let mut created = 0;
|
||||||
|
for _ in count..=(min_idle as usize) {
|
||||||
|
let conn = match pool.client.connection() {
|
||||||
|
Ok(conn) => conn,
|
||||||
|
Err(err) => {
|
||||||
|
#[cfg(feature = "tracing")]
|
||||||
|
tracing::warn!("couldn't create idle connection {}", err);
|
||||||
|
#[cfg(not(feature = "tracing"))]
|
||||||
|
let _ = err;
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut connections = pool.connections.lock().unwrap();
|
||||||
|
connections.push(ParkedConnection::park(conn));
|
||||||
|
|
||||||
|
#[cfg(feature = "tracing")]
|
||||||
|
{
|
||||||
|
created += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "tracing")]
|
||||||
|
if created > 0 {
|
||||||
|
tracing::debug!("created {} idle connections", created);
|
||||||
|
}
|
||||||
|
|
||||||
|
if !dropped.is_empty() {
|
||||||
|
#[cfg(feature = "tracing")]
|
||||||
|
tracing::debug!("dropped {} idle connections", dropped.len());
|
||||||
|
|
||||||
|
for conn in dropped {
|
||||||
|
let mut conn = conn.unpark();
|
||||||
|
conn.abort();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
thread::sleep(idle_timeout);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.expect("couldn't spawn the Pool thread");
|
||||||
|
}
|
||||||
|
|
||||||
|
pool
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn connection(self: &Arc<Self>) -> Result<PooledConnection, Error> {
|
||||||
|
loop {
|
||||||
|
let conn = {
|
||||||
|
let mut connections = self.connections.lock().unwrap();
|
||||||
|
connections.pop()
|
||||||
|
};
|
||||||
|
|
||||||
|
match conn {
|
||||||
|
Some(conn) => {
|
||||||
|
let mut conn = conn.unpark();
|
||||||
|
|
||||||
|
// TODO: handle the client try another connection if this one isn't good
|
||||||
|
if !conn.test_connected() {
|
||||||
|
#[cfg(feature = "tracing")]
|
||||||
|
tracing::debug!("dropping a broken connection");
|
||||||
|
|
||||||
|
conn.abort();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "tracing")]
|
||||||
|
tracing::debug!("reusing a pooled connection");
|
||||||
|
|
||||||
|
return Ok(PooledConnection::wrap(conn, self.clone()));
|
||||||
|
}
|
||||||
|
None => {
|
||||||
|
#[cfg(feature = "tracing")]
|
||||||
|
tracing::debug!("creating a new connection");
|
||||||
|
|
||||||
|
let conn = self.client.connection()?;
|
||||||
|
return Ok(PooledConnection::wrap(conn, self.clone()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn recycle(&self, mut conn: SmtpConnection) {
|
||||||
|
if conn.has_broken() {
|
||||||
|
#[cfg(feature = "tracing")]
|
||||||
|
tracing::debug!("dropping a broken connection instead of recycling it");
|
||||||
|
|
||||||
|
conn.abort();
|
||||||
|
drop(conn);
|
||||||
|
} else {
|
||||||
|
#[cfg(feature = "tracing")]
|
||||||
|
tracing::debug!("recycling connection");
|
||||||
|
|
||||||
|
let mut connections = self.connections.lock().unwrap();
|
||||||
|
if connections.len() >= self.config.max_size as usize {
|
||||||
|
drop(connections);
|
||||||
|
conn.abort();
|
||||||
|
} else {
|
||||||
|
let conn = ParkedConnection::park(conn);
|
||||||
|
connections.push(conn);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Debug for Pool {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
f.debug_struct("Pool")
|
||||||
|
.field("config", &self.config)
|
||||||
|
.field(
|
||||||
|
"connections",
|
||||||
|
&match self.connections.try_lock() {
|
||||||
|
Ok(connections) => format!("{} connections", connections.len()),
|
||||||
|
|
||||||
|
Err(TryLockError::WouldBlock) => "LOCKED".to_string(),
|
||||||
|
Err(TryLockError::Poisoned(_)) => "POISONED".to_string(),
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.field("client", &self.client)
|
||||||
|
.finish()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Drop for Pool {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
#[cfg(feature = "tracing")]
|
||||||
|
tracing::debug!("dropping Pool");
|
||||||
|
|
||||||
|
let connections = mem::take(&mut *self.connections.get_mut().unwrap());
|
||||||
|
for conn in connections {
|
||||||
|
let mut conn = conn.unpark();
|
||||||
|
conn.abort();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ParkedConnection {
|
||||||
|
fn park(conn: SmtpConnection) -> Self {
|
||||||
|
Self {
|
||||||
|
conn,
|
||||||
|
since: Instant::now(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn idle_duration(&self) -> Duration {
|
||||||
|
self.since.elapsed()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn unpark(self) -> SmtpConnection {
|
||||||
|
self.conn
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PooledConnection {
|
||||||
|
fn wrap(conn: SmtpConnection, pool: Arc<Pool>) -> Self {
|
||||||
|
Self {
|
||||||
|
conn: Some(conn),
|
||||||
|
pool,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Deref for PooledConnection {
|
||||||
|
type Target = SmtpConnection;
|
||||||
|
|
||||||
|
fn deref(&self) -> &Self::Target {
|
||||||
|
self.conn.as_ref().expect("conn hasn't been dropped yet")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl DerefMut for PooledConnection {
|
||||||
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||||
|
self.conn.as_mut().expect("conn hasn't been dropped yet")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Drop for PooledConnection {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
let conn = self
|
||||||
|
.conn
|
||||||
|
.take()
|
||||||
|
.expect("SmtpConnection hasn't been taken yet");
|
||||||
|
self.pool.recycle(conn);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -137,10 +137,10 @@ impl Code {
|
|||||||
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||||
pub struct Response {
|
pub struct Response {
|
||||||
/// Response code
|
/// Response code
|
||||||
pub code: Code,
|
code: Code,
|
||||||
/// Server response string (optional)
|
/// Server response string (optional)
|
||||||
/// Handle multiline responses
|
/// Handle multiline responses
|
||||||
pub message: Vec<String>,
|
message: Vec<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FromStr for Response {
|
impl FromStr for Response {
|
||||||
@@ -180,6 +180,16 @@ impl Response {
|
|||||||
pub fn first_line(&self) -> Option<&str> {
|
pub fn first_line(&self) -> Option<&str> {
|
||||||
self.message.first().map(String::as_str)
|
self.message.first().map(String::as_str)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Response code
|
||||||
|
pub fn code(&self) -> Code {
|
||||||
|
self.code
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Server response string (array of lines)
|
||||||
|
pub fn message(&self) -> impl Iterator<Item = &str> {
|
||||||
|
self.message.iter().map(String::as_str)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parsers (originally from tokio-smtp)
|
// Parsers (originally from tokio-smtp)
|
||||||
|
|||||||
@@ -1,22 +1,23 @@
|
|||||||
|
#[cfg(feature = "pool")]
|
||||||
|
use std::sync::Arc;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
#[cfg(feature = "r2d2")]
|
#[cfg(feature = "pool")]
|
||||||
use r2d2::Pool;
|
use super::pool::sync_impl::Pool;
|
||||||
|
#[cfg(feature = "pool")]
|
||||||
#[cfg(feature = "r2d2")]
|
|
||||||
use super::PoolConfig;
|
use super::PoolConfig;
|
||||||
#[cfg(any(feature = "native-tls", feature = "rustls-tls"))]
|
|
||||||
use super::{error, Tls, TlsParameters, SUBMISSIONS_PORT, SUBMISSION_PORT};
|
|
||||||
use super::{ClientId, Credentials, Error, Mechanism, Response, SmtpConnection, SmtpInfo};
|
use super::{ClientId, Credentials, Error, Mechanism, Response, SmtpConnection, SmtpInfo};
|
||||||
|
#[cfg(any(feature = "native-tls", feature = "rustls-tls"))]
|
||||||
|
use super::{Tls, TlsParameters, SUBMISSIONS_PORT, SUBMISSION_PORT};
|
||||||
use crate::{address::Envelope, Transport};
|
use crate::{address::Envelope, Transport};
|
||||||
|
|
||||||
/// Sends emails using the SMTP protocol
|
/// Sends emails using the SMTP protocol
|
||||||
#[cfg_attr(docsrs, doc(cfg(feature = "smtp-transport")))]
|
#[cfg_attr(docsrs, doc(cfg(feature = "smtp-transport")))]
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct SmtpTransport {
|
pub struct SmtpTransport {
|
||||||
#[cfg(feature = "r2d2")]
|
#[cfg(feature = "pool")]
|
||||||
inner: Pool<SmtpClient>,
|
inner: Arc<Pool>,
|
||||||
#[cfg(not(feature = "r2d2"))]
|
#[cfg(not(feature = "pool"))]
|
||||||
inner: SmtpClient,
|
inner: SmtpClient,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -26,14 +27,11 @@ impl Transport for SmtpTransport {
|
|||||||
|
|
||||||
/// Sends an email
|
/// Sends an email
|
||||||
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> {
|
||||||
#[cfg(feature = "r2d2")]
|
|
||||||
let mut conn = self.inner.get().map_err(error::client)?;
|
|
||||||
#[cfg(not(feature = "r2d2"))]
|
|
||||||
let mut conn = self.inner.connection()?;
|
let mut conn = self.inner.connection()?;
|
||||||
|
|
||||||
let result = conn.send(envelope, email)?;
|
let result = conn.send(envelope, email)?;
|
||||||
|
|
||||||
#[cfg(not(feature = "r2d2"))]
|
#[cfg(not(feature = "pool"))]
|
||||||
conn.quit()?;
|
conn.quit()?;
|
||||||
|
|
||||||
Ok(result)
|
Ok(result)
|
||||||
@@ -105,10 +103,25 @@ impl SmtpTransport {
|
|||||||
|
|
||||||
SmtpTransportBuilder {
|
SmtpTransportBuilder {
|
||||||
info: new,
|
info: new,
|
||||||
#[cfg(feature = "r2d2")]
|
#[cfg(feature = "pool")]
|
||||||
pool_config: PoolConfig::default(),
|
pool_config: PoolConfig::default(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Tests the SMTP connection
|
||||||
|
///
|
||||||
|
/// `test_connection()` tests the connection by using the SMTP NOOP command.
|
||||||
|
/// The connection is closed afterwards if a connection pool is not used.
|
||||||
|
pub fn test_connection(&self) -> Result<bool, Error> {
|
||||||
|
let mut conn = self.inner.connection()?;
|
||||||
|
|
||||||
|
let is_connected = conn.test_connected();
|
||||||
|
|
||||||
|
#[cfg(not(feature = "pool"))]
|
||||||
|
conn.quit()?;
|
||||||
|
|
||||||
|
Ok(is_connected)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Contains client configuration.
|
/// Contains client configuration.
|
||||||
@@ -116,7 +129,7 @@ impl SmtpTransport {
|
|||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct SmtpTransportBuilder {
|
pub struct SmtpTransportBuilder {
|
||||||
info: SmtpInfo,
|
info: SmtpInfo,
|
||||||
#[cfg(feature = "r2d2")]
|
#[cfg(feature = "pool")]
|
||||||
pool_config: PoolConfig,
|
pool_config: PoolConfig,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -163,8 +176,8 @@ impl SmtpTransportBuilder {
|
|||||||
/// Use a custom configuration for the connection pool
|
/// Use a custom configuration for the connection pool
|
||||||
///
|
///
|
||||||
/// Defaults can be found at [`PoolConfig`]
|
/// Defaults can be found at [`PoolConfig`]
|
||||||
#[cfg(feature = "r2d2")]
|
#[cfg(feature = "pool")]
|
||||||
#[cfg_attr(docsrs, doc(cfg(feature = "r2d2")))]
|
#[cfg_attr(docsrs, doc(cfg(feature = "pool")))]
|
||||||
pub fn pool_config(mut self, pool_config: PoolConfig) -> Self {
|
pub fn pool_config(mut self, pool_config: PoolConfig) -> Self {
|
||||||
self.pool_config = pool_config;
|
self.pool_config = pool_config;
|
||||||
self
|
self
|
||||||
@@ -172,16 +185,15 @@ impl SmtpTransportBuilder {
|
|||||||
|
|
||||||
/// Build the transport
|
/// Build the transport
|
||||||
///
|
///
|
||||||
/// If the `r2d2` feature is enabled an `Arc` wrapped pool is be created.
|
/// If the `pool` feature is enabled an `Arc` wrapped pool is be created.
|
||||||
/// Defaults can be found at [`PoolConfig`]
|
/// Defaults can be found at [`PoolConfig`]
|
||||||
pub fn build(self) -> SmtpTransport {
|
pub fn build(self) -> SmtpTransport {
|
||||||
let client = SmtpClient { info: self.info };
|
let client = SmtpClient { info: self.info };
|
||||||
SmtpTransport {
|
|
||||||
#[cfg(feature = "r2d2")]
|
#[cfg(feature = "pool")]
|
||||||
inner: self.pool_config.build(client),
|
let client = Pool::new(self.pool_config, client);
|
||||||
#[cfg(not(feature = "r2d2"))]
|
|
||||||
inner: client,
|
SmtpTransport { inner: client }
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -225,7 +237,7 @@ impl SmtpClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if let Some(credentials) = &self.info.credentials {
|
if let Some(credentials) = &self.info.credentials {
|
||||||
conn.auth(&self.info.authentication, &credentials)?;
|
conn.auth(&self.info.authentication, credentials)?;
|
||||||
}
|
}
|
||||||
Ok(conn)
|
Ok(conn)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ use std::fmt::{Display, Formatter, Result as FmtResult};
|
|||||||
|
|
||||||
/// Encode a string as xtext
|
/// Encode a string as xtext
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
|
||||||
pub struct XText<'a>(pub &'a str);
|
pub struct XText<'a>(pub &'a str);
|
||||||
|
|
||||||
impl<'a> Display for XText<'a> {
|
impl<'a> Display for XText<'a> {
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
#[cfg(all(test, feature = "smtp-transport", feature = "r2d2"))]
|
#[cfg(all(test, feature = "smtp-transport", feature = "pool"))]
|
||||||
mod sync {
|
mod sync {
|
||||||
use lettre::{address::Envelope, SmtpTransport, Transport};
|
use lettre::{address::Envelope, SmtpTransport, Transport};
|
||||||
use std::{sync::mpsc, thread};
|
use std::{sync::mpsc, thread};
|
||||||
|
|||||||
Reference in New Issue
Block a user