Compare commits
1 Commits
v0.11.11
...
smtp-error
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
18d2d051ed |
4
.github/workflows/test.yml
vendored
4
.github/workflows/test.yml
vendored
@@ -75,8 +75,8 @@ jobs:
|
||||
rust: stable
|
||||
- name: beta
|
||||
rust: beta
|
||||
- name: '1.71'
|
||||
rust: '1.71'
|
||||
- name: '1.70'
|
||||
rust: '1.70'
|
||||
|
||||
steps:
|
||||
- name: Checkout
|
||||
|
||||
32
CHANGELOG.md
32
CHANGELOG.md
@@ -1,35 +1,3 @@
|
||||
<a name="v0.11.11"></a>
|
||||
### v0.11.11 (2024-12-05)
|
||||
|
||||
#### Upgrade notes
|
||||
|
||||
* MSRV is now 1.71 ([#1008])
|
||||
|
||||
#### Bug fixes
|
||||
|
||||
* Fix off-by-one error reaching the minimum number of configured pooled connections ([#1012])
|
||||
|
||||
#### Misc
|
||||
|
||||
* Fix clippy warnings ([#1009])
|
||||
* Fix `-Zminimal-versions` build ([#1007])
|
||||
|
||||
[#1007]: https://github.com/lettre/lettre/pull/1007
|
||||
[#1008]: https://github.com/lettre/lettre/pull/1008
|
||||
[#1009]: https://github.com/lettre/lettre/pull/1009
|
||||
[#1012]: https://github.com/lettre/lettre/pull/1012
|
||||
|
||||
<a name="v0.11.10"></a>
|
||||
### v0.11.10 (2024-10-23)
|
||||
|
||||
#### Bug fixes
|
||||
|
||||
* Ignore disconnect errors when `pool` feature of SMTP transport is disabled ([#999])
|
||||
* Use case insensitive comparisons for matching login challenge requests ([#1000])
|
||||
|
||||
[#999]: https://github.com/lettre/lettre/pull/999
|
||||
[#1000]: https://github.com/lettre/lettre/pull/1000
|
||||
|
||||
<a name="v0.11.9"></a>
|
||||
### v0.11.9 (2024-09-13)
|
||||
|
||||
|
||||
462
Cargo.lock
generated
462
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@@ -1,7 +1,7 @@
|
||||
[package]
|
||||
name = "lettre"
|
||||
# remember to update html_root_url and README.md (Cargo.toml example and deps.rs badge)
|
||||
version = "0.11.11"
|
||||
version = "0.11.9"
|
||||
description = "Email client"
|
||||
readme = "README.md"
|
||||
homepage = "https://lettre.rs"
|
||||
@@ -11,7 +11,7 @@ authors = ["Alexis Mousset <contact@amousset.me>", "Paolo Barbolini <paolo@paolo
|
||||
categories = ["email", "network-programming"]
|
||||
keywords = ["email", "smtp", "mailer", "message", "sendmail"]
|
||||
edition = "2021"
|
||||
rust-version = "1.71"
|
||||
rust-version = "1.70"
|
||||
|
||||
[badges]
|
||||
is-it-maintained-issue-resolution = { repository = "lettre/lettre" }
|
||||
@@ -44,7 +44,7 @@ url = { version = "2.4", optional = true }
|
||||
percent-encoding = { version = "2.3", optional = true }
|
||||
|
||||
## tls
|
||||
native-tls = { version = "0.2.9", optional = true } # feature
|
||||
native-tls = { version = "0.2.5", optional = true } # feature
|
||||
rustls = { version = "0.23.5", default-features = false, features = ["ring", "logging", "std", "tls12"], optional = true }
|
||||
rustls-pemfile = { version = "2", optional = true }
|
||||
rustls-native-certs = { version = "0.8", optional = true }
|
||||
|
||||
@@ -28,8 +28,8 @@
|
||||
</div>
|
||||
|
||||
<div align="center">
|
||||
<a href="https://deps.rs/crate/lettre/0.11.11">
|
||||
<img src="https://deps.rs/crate/lettre/0.11.11/status.svg"
|
||||
<a href="https://deps.rs/crate/lettre/0.11.9">
|
||||
<img src="https://deps.rs/crate/lettre/0.11.9/status.svg"
|
||||
alt="dependency status" />
|
||||
</a>
|
||||
</div>
|
||||
@@ -53,12 +53,12 @@ Lettre does not provide (for now):
|
||||
## Supported Rust Versions
|
||||
|
||||
Lettre supports all Rust versions released in the last 6 months. At the time of writing
|
||||
the minimum supported Rust version is 1.71, but this could change at any time either from
|
||||
the minimum supported Rust version is 1.70, but this could change at any time either from
|
||||
one of our dependencies bumping their MSRV or by a new patch release of lettre.
|
||||
|
||||
## Example
|
||||
|
||||
This library requires Rust 1.71 or newer.
|
||||
This library requires Rust 1.70 or newer.
|
||||
To use this library, add the following to your `Cargo.toml`:
|
||||
|
||||
```toml
|
||||
|
||||
@@ -36,7 +36,7 @@ impl<'de> Deserialize<'de> for Address {
|
||||
{
|
||||
struct FieldVisitor;
|
||||
|
||||
impl Visitor<'_> for FieldVisitor {
|
||||
impl<'de> Visitor<'de> for FieldVisitor {
|
||||
type Value = Field;
|
||||
|
||||
fn expecting(&self, formatter: &mut Formatter<'_>) -> FmtResult {
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
//! * Secure defaults
|
||||
//! * Async support
|
||||
//!
|
||||
//! Lettre requires Rust 1.71 or newer.
|
||||
//! Lettre requires Rust 1.70 or newer.
|
||||
//!
|
||||
//! ## Features
|
||||
//!
|
||||
@@ -109,7 +109,7 @@
|
||||
//! [mime 0.3]: https://docs.rs/mime/0.3
|
||||
//! [DKIM]: https://datatracker.ietf.org/doc/html/rfc6376
|
||||
|
||||
#![doc(html_root_url = "https://docs.rs/crate/lettre/0.11.11")]
|
||||
#![doc(html_root_url = "https://docs.rs/crate/lettre/0.11.9")]
|
||||
#![doc(html_favicon_url = "https://lettre.rs/favicon.ico")]
|
||||
#![doc(html_logo_url = "https://avatars0.githubusercontent.com/u/15113230?v=4")]
|
||||
#![forbid(unsafe_code)]
|
||||
|
||||
@@ -345,6 +345,7 @@ fn dkim_canonicalize_headers<'a>(
|
||||
|
||||
/// Sign with Dkim a message by adding Dkim-Signature header created with configuration expressed by
|
||||
/// dkim_config
|
||||
|
||||
pub fn dkim_sign(message: &mut Message, dkim_config: &DkimConfig) {
|
||||
dkim_sign_fixed_time(message, dkim_config, SystemTime::now())
|
||||
}
|
||||
|
||||
@@ -119,7 +119,7 @@ mod serde {
|
||||
{
|
||||
struct ContentTypeVisitor;
|
||||
|
||||
impl Visitor<'_> for ContentTypeVisitor {
|
||||
impl<'de> Visitor<'de> for ContentTypeVisitor {
|
||||
type Value = ContentType;
|
||||
|
||||
// The error message which states what the Visitor expects to
|
||||
|
||||
@@ -36,7 +36,7 @@ impl<'de> Deserialize<'de> for Mailbox {
|
||||
{
|
||||
struct FieldVisitor;
|
||||
|
||||
impl Visitor<'_> for FieldVisitor {
|
||||
impl<'de> Visitor<'de> for FieldVisitor {
|
||||
type Value = Field;
|
||||
|
||||
fn expecting(&self, formatter: &mut Formatter<'_>) -> FmtResult {
|
||||
|
||||
@@ -174,7 +174,7 @@ impl Mailboxes {
|
||||
self
|
||||
}
|
||||
|
||||
/// Adds a new [`Mailbox`] to the list, in a `Vec::push` style pattern.
|
||||
/// Adds a new [`Mailbox`] to the list, in a Vec::push style pattern.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
|
||||
@@ -345,7 +345,7 @@ impl MessageBuilder {
|
||||
let hostname = hostname::get()
|
||||
.map_err(|_| ())
|
||||
.and_then(|s| s.into_string().map_err(|_| ()))
|
||||
.unwrap_or_else(|()| DEFAULT_MESSAGE_ID_DOMAIN.to_owned());
|
||||
.unwrap_or_else(|_| DEFAULT_MESSAGE_ID_DOMAIN.to_owned());
|
||||
#[cfg(not(feature = "hostname"))]
|
||||
let hostname = DEFAULT_MESSAGE_ID_DOMAIN.to_owned();
|
||||
|
||||
|
||||
@@ -45,7 +45,7 @@ impl AsyncTransport for AsyncSmtpTransport<Tokio1Executor> {
|
||||
let result = conn.send(envelope, email).await?;
|
||||
|
||||
#[cfg(not(feature = "pool"))]
|
||||
conn.abort().await;
|
||||
conn.quit().await?;
|
||||
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
@@ -98,17 +98,13 @@ impl Mechanism {
|
||||
let decoded_challenge = challenge
|
||||
.ok_or_else(|| error::client("This mechanism does expect a challenge"))?;
|
||||
|
||||
if contains_ignore_ascii_case(
|
||||
decoded_challenge,
|
||||
["User Name", "Username:", "Username", "User Name\0"],
|
||||
) {
|
||||
if ["User Name", "Username:", "Username", "User Name\0"]
|
||||
.contains(&decoded_challenge)
|
||||
{
|
||||
return Ok(credentials.authentication_identity.clone());
|
||||
}
|
||||
|
||||
if contains_ignore_ascii_case(
|
||||
decoded_challenge,
|
||||
["Password", "Password:", "Password\0"],
|
||||
) {
|
||||
if ["Password", "Password:", "Password\0"].contains(&decoded_challenge) {
|
||||
return Ok(credentials.secret.clone());
|
||||
}
|
||||
|
||||
@@ -125,15 +121,6 @@ impl Mechanism {
|
||||
}
|
||||
}
|
||||
|
||||
fn contains_ignore_ascii_case<'a>(
|
||||
haystack: &str,
|
||||
needles: impl IntoIterator<Item = &'a str>,
|
||||
) -> bool {
|
||||
needles
|
||||
.into_iter()
|
||||
.any(|item| item.eq_ignore_ascii_case(haystack))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::{Credentials, Mechanism};
|
||||
@@ -168,23 +155,6 @@ mod test {
|
||||
assert!(mechanism.response(&credentials, None).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_login_case_insensitive() {
|
||||
let mechanism = Mechanism::Login;
|
||||
|
||||
let credentials = Credentials::new("alice".to_owned(), "wonderland".to_owned());
|
||||
|
||||
assert_eq!(
|
||||
mechanism.response(&credentials, Some("username")).unwrap(),
|
||||
"alice"
|
||||
);
|
||||
assert_eq!(
|
||||
mechanism.response(&credentials, Some("password")).unwrap(),
|
||||
"wonderland"
|
||||
);
|
||||
assert!(mechanism.response(&credentials, None).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_xoauth2() {
|
||||
let mechanism = Mechanism::Xoauth2;
|
||||
|
||||
@@ -10,34 +10,21 @@ use super::{AsyncNetworkStream, ClientCodec, TlsParameters};
|
||||
use crate::{
|
||||
transport::smtp::{
|
||||
authentication::{Credentials, Mechanism},
|
||||
client::{ConnectionState, ConnectionWrapper},
|
||||
commands::{Auth, Data, Ehlo, Mail, Noop, Quit, Rcpt, Starttls},
|
||||
error,
|
||||
error::Error,
|
||||
error::{self, Error},
|
||||
extension::{ClientId, Extension, MailBodyParameter, MailParameter, ServerInfo},
|
||||
response::{parse_response, Response},
|
||||
},
|
||||
Envelope,
|
||||
};
|
||||
|
||||
macro_rules! try_smtp (
|
||||
($err: expr, $client: ident) => ({
|
||||
match $err {
|
||||
Ok(val) => val,
|
||||
Err(err) => {
|
||||
$client.abort().await;
|
||||
return Err(From::from(err))
|
||||
},
|
||||
}
|
||||
})
|
||||
);
|
||||
|
||||
/// Structure that implements the SMTP client
|
||||
pub struct AsyncSmtpConnection {
|
||||
/// TCP stream between client and server
|
||||
/// Value is None before connection
|
||||
stream: BufReader<AsyncNetworkStream>,
|
||||
/// Panic state
|
||||
panic: bool,
|
||||
stream: ConnectionWrapper<BufReader<AsyncNetworkStream>>,
|
||||
/// Whether QUIT has been sent
|
||||
sent_quit: bool,
|
||||
/// Information about the server
|
||||
server_info: ServerInfo,
|
||||
}
|
||||
@@ -125,8 +112,8 @@ impl AsyncSmtpConnection {
|
||||
) -> Result<AsyncSmtpConnection, Error> {
|
||||
let stream = BufReader::new(stream);
|
||||
let mut conn = AsyncSmtpConnection {
|
||||
stream,
|
||||
panic: false,
|
||||
stream: ConnectionWrapper::new(stream),
|
||||
sent_quit: false,
|
||||
server_info: ServerInfo::default(),
|
||||
};
|
||||
// TODO log
|
||||
@@ -170,30 +157,28 @@ impl AsyncSmtpConnection {
|
||||
mail_options.push(MailParameter::Body(MailBodyParameter::EightBitMime));
|
||||
}
|
||||
|
||||
try_smtp!(
|
||||
self.command(Mail::new(envelope.from().cloned(), mail_options))
|
||||
.await,
|
||||
self
|
||||
);
|
||||
self.command(Mail::new(envelope.from().cloned(), mail_options))
|
||||
.await?;
|
||||
|
||||
// Recipient
|
||||
for to_address in envelope.to() {
|
||||
try_smtp!(
|
||||
self.command(Rcpt::new(to_address.clone(), vec![])).await,
|
||||
self
|
||||
);
|
||||
self.command(Rcpt::new(to_address.clone(), vec![])).await?;
|
||||
}
|
||||
|
||||
// Data
|
||||
try_smtp!(self.command(Data).await, self);
|
||||
self.command(Data).await?;
|
||||
|
||||
// Message content
|
||||
let result = try_smtp!(self.message(email).await, self);
|
||||
let result = self.message(email).await?;
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
pub fn has_broken(&self) -> bool {
|
||||
self.panic
|
||||
self.sent_quit
|
||||
|| matches!(
|
||||
self.stream.state(),
|
||||
ConnectionState::BrokenConnection | ConnectionState::BrokenResponse
|
||||
)
|
||||
}
|
||||
|
||||
pub fn can_starttls(&self) -> bool {
|
||||
@@ -213,12 +198,14 @@ impl AsyncSmtpConnection {
|
||||
hello_name: &ClientId,
|
||||
) -> Result<(), Error> {
|
||||
if self.server_info.supports_feature(Extension::StartTls) {
|
||||
try_smtp!(self.command(Starttls).await, self);
|
||||
self.stream.get_mut().upgrade_tls(tls_parameters).await?;
|
||||
self.command(Starttls).await?;
|
||||
self.stream
|
||||
.async_op(|stream| stream.get_mut().upgrade_tls(tls_parameters))
|
||||
.await?;
|
||||
#[cfg(feature = "tracing")]
|
||||
tracing::debug!("connection encrypted");
|
||||
// Send EHLO again
|
||||
try_smtp!(self.ehlo(hello_name).await, self);
|
||||
self.ehlo(hello_name).await?;
|
||||
Ok(())
|
||||
} else {
|
||||
Err(error::client("STARTTLS is not supported on this server"))
|
||||
@@ -227,32 +214,39 @@ impl AsyncSmtpConnection {
|
||||
|
||||
/// Send EHLO and update server info
|
||||
async fn ehlo(&mut self, hello_name: &ClientId) -> Result<(), Error> {
|
||||
let ehlo_response = try_smtp!(self.command(Ehlo::new(hello_name.clone())).await, self);
|
||||
self.server_info = try_smtp!(ServerInfo::from_response(&ehlo_response), self);
|
||||
let ehlo_response = self.command(Ehlo::new(hello_name.clone())).await?;
|
||||
self.server_info = ServerInfo::from_response(&ehlo_response)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn quit(&mut self) -> Result<Response, Error> {
|
||||
Ok(try_smtp!(self.command(Quit).await, self))
|
||||
self.sent_quit = true;
|
||||
self.command(Quit).await
|
||||
}
|
||||
|
||||
pub async fn abort(&mut self) {
|
||||
// Only try to quit if we are not already broken
|
||||
if !self.panic {
|
||||
self.panic = true;
|
||||
let _ = self.command(Quit).await;
|
||||
// `write` already rejects writes if the connection state if bad
|
||||
if !self.sent_quit {
|
||||
let _ = self.quit().await;
|
||||
}
|
||||
|
||||
if !matches!(self.stream.state(), ConnectionState::BrokenConnection) {
|
||||
let _ = self
|
||||
.stream
|
||||
.async_op(|stream| async { stream.close().await.map_err(error::network) })
|
||||
.await;
|
||||
}
|
||||
let _ = self.stream.close().await;
|
||||
}
|
||||
|
||||
/// Sets the underlying stream
|
||||
pub fn set_stream(&mut self, stream: AsyncNetworkStream) {
|
||||
self.stream = BufReader::new(stream);
|
||||
self.stream = ConnectionWrapper::new(BufReader::new(stream));
|
||||
}
|
||||
|
||||
/// Tells if the underlying stream is currently encrypted
|
||||
pub fn is_encrypted(&self) -> bool {
|
||||
self.stream.get_ref().is_encrypted()
|
||||
self.stream.get_ref().get_ref().is_encrypted()
|
||||
}
|
||||
|
||||
/// Checks if the server is connected using the NOOP SMTP command
|
||||
@@ -279,15 +273,13 @@ impl AsyncSmtpConnection {
|
||||
|
||||
while challenges > 0 && response.has_code(334) {
|
||||
challenges -= 1;
|
||||
response = try_smtp!(
|
||||
self.command(Auth::new_from_response(
|
||||
response = self
|
||||
.command(Auth::new_from_response(
|
||||
mechanism,
|
||||
credentials.clone(),
|
||||
&response,
|
||||
)?)
|
||||
.await,
|
||||
self
|
||||
);
|
||||
.await?;
|
||||
}
|
||||
|
||||
if challenges == 0 {
|
||||
@@ -316,15 +308,17 @@ impl AsyncSmtpConnection {
|
||||
/// Writes a string to the server
|
||||
async fn write(&mut self, string: &[u8]) -> Result<(), Error> {
|
||||
self.stream
|
||||
.get_mut()
|
||||
.write_all(string)
|
||||
.await
|
||||
.map_err(error::network)?;
|
||||
.async_op(|stream| async {
|
||||
stream
|
||||
.get_mut()
|
||||
.write_all(string)
|
||||
.await
|
||||
.map_err(error::network)
|
||||
})
|
||||
.await?;
|
||||
self.stream
|
||||
.get_mut()
|
||||
.flush()
|
||||
.await
|
||||
.map_err(error::network)?;
|
||||
.async_op(|stream| async { stream.get_mut().flush().await.map_err(error::network) })
|
||||
.await?;
|
||||
|
||||
#[cfg(feature = "tracing")]
|
||||
tracing::debug!("Wrote: {}", escape_crlf(&String::from_utf8_lossy(string)));
|
||||
@@ -337,9 +331,10 @@ impl AsyncSmtpConnection {
|
||||
|
||||
while self
|
||||
.stream
|
||||
.read_line(&mut buffer)
|
||||
.await
|
||||
.map_err(error::network)?
|
||||
.async_op(|stream| async {
|
||||
stream.read_line(&mut buffer).await.map_err(error::network)
|
||||
})
|
||||
.await?
|
||||
> 0
|
||||
{
|
||||
#[cfg(feature = "tracing")]
|
||||
@@ -356,10 +351,12 @@ impl AsyncSmtpConnection {
|
||||
}
|
||||
}
|
||||
Err(nom::Err::Failure(e)) => {
|
||||
self.stream.set_state(ConnectionState::BrokenResponse);
|
||||
return Err(error::response(e.to_string()));
|
||||
}
|
||||
Err(nom::Err::Incomplete(_)) => { /* read more */ }
|
||||
Err(nom::Err::Error(e)) => {
|
||||
self.stream.set_state(ConnectionState::BrokenResponse);
|
||||
return Err(error::response(e.to_string()));
|
||||
}
|
||||
}
|
||||
@@ -371,12 +368,12 @@ impl AsyncSmtpConnection {
|
||||
/// The X509 certificate of the server (DER encoded)
|
||||
#[cfg(any(feature = "native-tls", feature = "rustls-tls", feature = "boring-tls"))]
|
||||
pub fn peer_certificate(&self) -> Result<Vec<u8>, Error> {
|
||||
self.stream.get_ref().peer_certificate()
|
||||
self.stream.get_ref().get_ref().peer_certificate()
|
||||
}
|
||||
|
||||
/// All the X509 certificates of the chain (DER encoded)
|
||||
#[cfg(any(feature = "rustls-tls", feature = "boring-tls"))]
|
||||
pub fn certificate_chain(&self) -> Result<Vec<Vec<u8>>, Error> {
|
||||
self.stream.get_ref().certificate_chain()
|
||||
self.stream.get_ref().get_ref().certificate_chain()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,38 +7,25 @@ use std::{
|
||||
|
||||
#[cfg(feature = "tracing")]
|
||||
use super::escape_crlf;
|
||||
use super::{ClientCodec, NetworkStream, TlsParameters};
|
||||
use super::{ClientCodec, ConnectionWrapper, NetworkStream, TlsParameters};
|
||||
use crate::{
|
||||
address::Envelope,
|
||||
transport::smtp::{
|
||||
authentication::{Credentials, Mechanism},
|
||||
client::ConnectionState,
|
||||
commands::{Auth, Data, Ehlo, Mail, Noop, Quit, Rcpt, Starttls},
|
||||
error,
|
||||
error::Error,
|
||||
error::{self, Error},
|
||||
extension::{ClientId, Extension, MailBodyParameter, MailParameter, ServerInfo},
|
||||
response::{parse_response, Response},
|
||||
},
|
||||
};
|
||||
|
||||
macro_rules! try_smtp (
|
||||
($err: expr, $client: ident) => ({
|
||||
match $err {
|
||||
Ok(val) => val,
|
||||
Err(err) => {
|
||||
$client.abort();
|
||||
return Err(From::from(err))
|
||||
},
|
||||
}
|
||||
})
|
||||
);
|
||||
|
||||
/// Structure that implements the SMTP client
|
||||
pub struct SmtpConnection {
|
||||
/// TCP stream between client and server
|
||||
/// Value is None before connection
|
||||
stream: BufReader<NetworkStream>,
|
||||
/// Panic state
|
||||
panic: bool,
|
||||
stream: ConnectionWrapper<BufReader<NetworkStream>>,
|
||||
/// Whether QUIT has been sent
|
||||
sent_quit: bool,
|
||||
/// Information about the server
|
||||
server_info: ServerInfo,
|
||||
}
|
||||
@@ -64,8 +51,8 @@ impl SmtpConnection {
|
||||
let stream = NetworkStream::connect(server, timeout, tls_parameters, local_address)?;
|
||||
let stream = BufReader::new(stream);
|
||||
let mut conn = SmtpConnection {
|
||||
stream,
|
||||
panic: false,
|
||||
stream: ConnectionWrapper::new(stream),
|
||||
sent_quit: false,
|
||||
server_info: ServerInfo::default(),
|
||||
};
|
||||
conn.set_timeout(timeout).map_err(error::network)?;
|
||||
@@ -110,26 +97,27 @@ impl SmtpConnection {
|
||||
mail_options.push(MailParameter::Body(MailBodyParameter::EightBitMime));
|
||||
}
|
||||
|
||||
try_smtp!(
|
||||
self.command(Mail::new(envelope.from().cloned(), mail_options)),
|
||||
self
|
||||
);
|
||||
self.command(Mail::new(envelope.from().cloned(), mail_options))?;
|
||||
|
||||
// Recipient
|
||||
for to_address in envelope.to() {
|
||||
try_smtp!(self.command(Rcpt::new(to_address.clone(), vec![])), self);
|
||||
self.command(Rcpt::new(to_address.clone(), vec![]))?;
|
||||
}
|
||||
|
||||
// Data
|
||||
try_smtp!(self.command(Data), self);
|
||||
self.command(Data)?;
|
||||
|
||||
// Message content
|
||||
let result = try_smtp!(self.message(email), self);
|
||||
let result = self.message(email)?;
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
pub fn has_broken(&self) -> bool {
|
||||
self.panic
|
||||
self.sent_quit
|
||||
|| matches!(
|
||||
self.stream.state(),
|
||||
ConnectionState::BrokenConnection | ConnectionState::BrokenResponse
|
||||
)
|
||||
}
|
||||
|
||||
pub fn can_starttls(&self) -> bool {
|
||||
@@ -145,12 +133,13 @@ impl SmtpConnection {
|
||||
if self.server_info.supports_feature(Extension::StartTls) {
|
||||
#[cfg(any(feature = "native-tls", feature = "rustls-tls", feature = "boring-tls"))]
|
||||
{
|
||||
try_smtp!(self.command(Starttls), self);
|
||||
self.stream.get_mut().upgrade_tls(tls_parameters)?;
|
||||
self.command(Starttls)?;
|
||||
self.stream
|
||||
.sync_op(|stream| stream.get_mut().upgrade_tls(tls_parameters))?;
|
||||
#[cfg(feature = "tracing")]
|
||||
tracing::debug!("connection encrypted");
|
||||
// Send EHLO again
|
||||
try_smtp!(self.ehlo(hello_name), self);
|
||||
self.ehlo(hello_name)?;
|
||||
Ok(())
|
||||
}
|
||||
#[cfg(not(any(
|
||||
@@ -168,38 +157,47 @@ impl SmtpConnection {
|
||||
|
||||
/// Send EHLO and update server info
|
||||
fn ehlo(&mut self, hello_name: &ClientId) -> Result<(), Error> {
|
||||
let ehlo_response = try_smtp!(self.command(Ehlo::new(hello_name.clone())), self);
|
||||
self.server_info = try_smtp!(ServerInfo::from_response(&ehlo_response), self);
|
||||
let ehlo_response = self.command(Ehlo::new(hello_name.clone()))?;
|
||||
self.server_info = ServerInfo::from_response(&ehlo_response)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn quit(&mut self) -> Result<Response, Error> {
|
||||
Ok(try_smtp!(self.command(Quit), self))
|
||||
self.sent_quit = true;
|
||||
self.command(Quit)
|
||||
}
|
||||
|
||||
pub fn abort(&mut self) {
|
||||
// Only try to quit if we are not already broken
|
||||
if !self.panic {
|
||||
self.panic = true;
|
||||
let _ = self.command(Quit);
|
||||
// `write` already rejects writes if the connection state if bad
|
||||
if !self.sent_quit {
|
||||
let _ = self.quit();
|
||||
}
|
||||
|
||||
if !matches!(self.stream.state(), ConnectionState::BrokenConnection) {
|
||||
let _ = self.stream.sync_op(|stream| {
|
||||
stream
|
||||
.get_mut()
|
||||
.shutdown(std::net::Shutdown::Both)
|
||||
.map_err(error::network)
|
||||
});
|
||||
}
|
||||
let _ = self.stream.get_mut().shutdown(std::net::Shutdown::Both);
|
||||
}
|
||||
|
||||
/// Sets the underlying stream
|
||||
pub fn set_stream(&mut self, stream: NetworkStream) {
|
||||
self.stream = BufReader::new(stream);
|
||||
self.stream = ConnectionWrapper::new(BufReader::new(stream));
|
||||
}
|
||||
|
||||
/// Tells if the underlying stream is currently encrypted
|
||||
pub fn is_encrypted(&self) -> bool {
|
||||
self.stream.get_ref().is_encrypted()
|
||||
self.stream.get_ref().get_ref().is_encrypted()
|
||||
}
|
||||
|
||||
/// Set timeout
|
||||
pub fn set_timeout(&mut self, duration: Option<Duration>) -> io::Result<()> {
|
||||
self.stream.get_mut().set_read_timeout(duration)?;
|
||||
self.stream.get_mut().set_write_timeout(duration)
|
||||
self.stream.get_mut().get_mut().set_read_timeout(duration)?;
|
||||
self.stream.get_mut().get_mut().set_write_timeout(duration)
|
||||
}
|
||||
|
||||
/// Checks if the server is connected using the NOOP SMTP command
|
||||
@@ -224,14 +222,11 @@ impl SmtpConnection {
|
||||
|
||||
while challenges > 0 && response.has_code(334) {
|
||||
challenges -= 1;
|
||||
response = try_smtp!(
|
||||
self.command(Auth::new_from_response(
|
||||
mechanism,
|
||||
credentials.clone(),
|
||||
&response,
|
||||
)?),
|
||||
self
|
||||
);
|
||||
response = self.command(Auth::new_from_response(
|
||||
mechanism,
|
||||
credentials.clone(),
|
||||
&response,
|
||||
)?)?;
|
||||
}
|
||||
|
||||
if challenges == 0 {
|
||||
@@ -261,10 +256,9 @@ impl SmtpConnection {
|
||||
/// Writes a string to the server
|
||||
fn write(&mut self, string: &[u8]) -> Result<(), Error> {
|
||||
self.stream
|
||||
.get_mut()
|
||||
.write_all(string)
|
||||
.map_err(error::network)?;
|
||||
self.stream.get_mut().flush().map_err(error::network)?;
|
||||
.sync_op(|stream| stream.get_mut().write_all(string).map_err(error::network))?;
|
||||
self.stream
|
||||
.sync_op(|stream| stream.get_mut().flush().map_err(error::network))?;
|
||||
|
||||
#[cfg(feature = "tracing")]
|
||||
tracing::debug!("Wrote: {}", escape_crlf(&String::from_utf8_lossy(string)));
|
||||
@@ -275,7 +269,11 @@ impl SmtpConnection {
|
||||
pub fn read_response(&mut self) -> Result<Response, Error> {
|
||||
let mut buffer = String::with_capacity(100);
|
||||
|
||||
while self.stream.read_line(&mut buffer).map_err(error::network)? > 0 {
|
||||
while self
|
||||
.stream
|
||||
.sync_op(|stream| stream.read_line(&mut buffer).map_err(error::network))?
|
||||
> 0
|
||||
{
|
||||
#[cfg(feature = "tracing")]
|
||||
tracing::debug!("<< {}", escape_crlf(&buffer));
|
||||
match parse_response(&buffer) {
|
||||
@@ -290,10 +288,12 @@ impl SmtpConnection {
|
||||
};
|
||||
}
|
||||
Err(nom::Err::Failure(e)) => {
|
||||
self.stream.set_state(ConnectionState::BrokenResponse);
|
||||
return Err(error::response(e.to_string()));
|
||||
}
|
||||
Err(nom::Err::Incomplete(_)) => { /* read more */ }
|
||||
Err(nom::Err::Error(e)) => {
|
||||
self.stream.set_state(ConnectionState::BrokenResponse);
|
||||
return Err(error::response(e.to_string()));
|
||||
}
|
||||
}
|
||||
@@ -305,12 +305,12 @@ impl SmtpConnection {
|
||||
/// The X509 certificate of the server (DER encoded)
|
||||
#[cfg(any(feature = "native-tls", feature = "rustls-tls", feature = "boring-tls"))]
|
||||
pub fn peer_certificate(&self) -> Result<Vec<u8>, Error> {
|
||||
self.stream.get_ref().peer_certificate()
|
||||
self.stream.get_ref().get_ref().peer_certificate()
|
||||
}
|
||||
|
||||
/// All the X509 certificates of the chain (DER encoded)
|
||||
#[cfg(any(feature = "rustls-tls", feature = "boring-tls"))]
|
||||
pub fn certificate_chain(&self) -> Result<Vec<Vec<u8>>, Error> {
|
||||
self.stream.get_ref().certificate_chain()
|
||||
self.stream.get_ref().get_ref().certificate_chain()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,6 +24,8 @@
|
||||
|
||||
#[cfg(feature = "serde")]
|
||||
use std::fmt::Debug;
|
||||
#[cfg(any(feature = "tokio1", feature = "async-std1"))]
|
||||
use std::future::Future;
|
||||
|
||||
#[cfg(any(feature = "tokio1", feature = "async-std1"))]
|
||||
pub use self::async_connection::AsyncSmtpConnection;
|
||||
@@ -40,6 +42,7 @@ pub use self::{
|
||||
connection::SmtpConnection,
|
||||
tls::{Certificate, CertificateStore, Identity, Tls, TlsParameters, TlsParametersBuilder},
|
||||
};
|
||||
use super::{error, Error};
|
||||
|
||||
#[cfg(any(feature = "tokio1", feature = "async-std1"))]
|
||||
mod async_connection;
|
||||
@@ -49,6 +52,99 @@ mod connection;
|
||||
mod net;
|
||||
mod tls;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(super) struct ConnectionWrapper<C> {
|
||||
conn: C,
|
||||
state: ConnectionState,
|
||||
}
|
||||
|
||||
impl<C> ConnectionWrapper<C> {
|
||||
pub(super) fn new(conn: C) -> Self {
|
||||
Self {
|
||||
conn,
|
||||
state: ConnectionState::ProbablyConnected,
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn get_ref(&self) -> &C {
|
||||
&self.conn
|
||||
}
|
||||
|
||||
pub(super) fn get_mut(&mut self) -> &mut C {
|
||||
&mut self.conn
|
||||
}
|
||||
|
||||
pub(super) fn state(&self) -> ConnectionState {
|
||||
self.state
|
||||
}
|
||||
|
||||
pub(super) fn set_state(&mut self, state: ConnectionState) {
|
||||
self.state = state;
|
||||
}
|
||||
|
||||
pub(super) fn sync_op<F, T>(&mut self, f: F) -> Result<T, Error>
|
||||
where
|
||||
F: FnOnce(&mut C) -> Result<T, Error>,
|
||||
{
|
||||
if !matches!(
|
||||
self.state,
|
||||
ConnectionState::ProbablyConnected | ConnectionState::BrokenResponse
|
||||
) {
|
||||
return Err(error::client(
|
||||
"attempted to send operation to broken connection",
|
||||
));
|
||||
}
|
||||
|
||||
self.state = ConnectionState::Writing;
|
||||
match f(&mut self.conn) {
|
||||
Ok(t) => {
|
||||
self.state = ConnectionState::ProbablyConnected;
|
||||
Ok(t)
|
||||
}
|
||||
Err(err) => {
|
||||
self.state = ConnectionState::BrokenConnection;
|
||||
Err(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(any(feature = "tokio1", feature = "async-std1"))]
|
||||
pub(super) async fn async_op<'a, F, Fut, T>(&'a mut self, f: F) -> Result<T, Error>
|
||||
where
|
||||
F: FnOnce(&'a mut C) -> Fut,
|
||||
Fut: Future<Output = Result<T, Error>>,
|
||||
{
|
||||
if !matches!(
|
||||
self.state,
|
||||
ConnectionState::ProbablyConnected | ConnectionState::BrokenResponse
|
||||
) {
|
||||
return Err(error::client(
|
||||
"attempted to send operation to broken connection",
|
||||
));
|
||||
}
|
||||
|
||||
self.state = ConnectionState::Writing;
|
||||
match f(&mut self.conn).await {
|
||||
Ok(t) => {
|
||||
self.state = ConnectionState::ProbablyConnected;
|
||||
Ok(t)
|
||||
}
|
||||
Err(err) => {
|
||||
self.state = ConnectionState::BrokenConnection;
|
||||
Err(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Copy, Clone)]
|
||||
pub(super) enum ConnectionState {
|
||||
ProbablyConnected,
|
||||
Writing,
|
||||
BrokenResponse,
|
||||
BrokenConnection,
|
||||
}
|
||||
|
||||
/// The codec used for transparency
|
||||
#[derive(Debug)]
|
||||
struct ClientCodec {
|
||||
|
||||
@@ -119,12 +119,12 @@ impl NetworkStream {
|
||||
|
||||
if let Some(timeout) = timeout {
|
||||
match socket.connect_timeout(&addr.into(), timeout) {
|
||||
Ok(()) => return Ok(socket.into()),
|
||||
Ok(_) => return Ok(socket.into()),
|
||||
Err(err) => last_err = Some(err),
|
||||
}
|
||||
} else {
|
||||
match socket.connect(&addr.into()) {
|
||||
Ok(()) => return Ok(socket.into()),
|
||||
Ok(_) => return Ok(socket.into()),
|
||||
Err(err) => last_err = Some(err),
|
||||
}
|
||||
}
|
||||
@@ -369,7 +369,7 @@ impl Write for NetworkStream {
|
||||
/// If the local address is set, binds the socket to this address.
|
||||
/// If local address is not set, then destination address is required to determine the default
|
||||
/// local address on some platforms.
|
||||
/// See: <https://github.com/hyperium/hyper/blob/faf24c6ad8eee1c3d5ccc9a4d4835717b8e2903f/src/client/connect/http.rs#L560>
|
||||
/// See: https://github.com/hyperium/hyper/blob/faf24c6ad8eee1c3d5ccc9a4d4835717b8e2903f/src/client/connect/http.rs#L560
|
||||
fn bind_local_address(
|
||||
socket: &socket2::Socket,
|
||||
dst_addr: &SocketAddr,
|
||||
|
||||
@@ -62,7 +62,7 @@ impl TransportBuilder for AsyncSmtpTransportBuilder {
|
||||
}
|
||||
}
|
||||
|
||||
/// Create a new `SmtpTransportBuilder` or `AsyncSmtpTransportBuilder` from a connection URL
|
||||
/// Create a new SmtpTransportBuilder or AsyncSmtpTransportBuilder from a connection URL
|
||||
pub(crate) fn from_connection_url<B: TransportBuilder>(connection_url: &str) -> Result<B, Error> {
|
||||
let connection_url = Url::parse(connection_url).map_err(error::connection)?;
|
||||
let tls: Option<String> = connection_url
|
||||
|
||||
@@ -78,7 +78,7 @@ impl<E: Executor> Pool<E> {
|
||||
|
||||
#[cfg(feature = "tracing")]
|
||||
let mut created = 0;
|
||||
for _ in count..(min_idle as usize) {
|
||||
for _ in count..=(min_idle as usize) {
|
||||
let conn = match pool.client.connection().await {
|
||||
Ok(conn) => conn,
|
||||
Err(err) => {
|
||||
|
||||
@@ -72,7 +72,7 @@ impl Pool {
|
||||
|
||||
#[cfg(feature = "tracing")]
|
||||
let mut created = 0;
|
||||
for _ in count..(min_idle as usize) {
|
||||
for _ in count..=(min_idle as usize) {
|
||||
let conn = match pool.client.connection() {
|
||||
Ok(conn) => conn,
|
||||
Err(err) => {
|
||||
|
||||
@@ -32,7 +32,7 @@ impl Transport for SmtpTransport {
|
||||
let result = conn.send(envelope, email)?;
|
||||
|
||||
#[cfg(not(feature = "pool"))]
|
||||
conn.abort();
|
||||
conn.quit()?;
|
||||
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ use std::fmt::{Display, Formatter, Result as FmtResult};
|
||||
#[derive(Debug)]
|
||||
pub struct XText<'a>(pub &'a str);
|
||||
|
||||
impl Display for XText<'_> {
|
||||
impl<'a> Display for XText<'a> {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
|
||||
let mut rest = self.0;
|
||||
while let Some(idx) = rest.find(|c| c < '!' || c == '+' || c == '=') {
|
||||
|
||||
Reference in New Issue
Block a user