Compare commits

...

48 Commits

Author SHA1 Message Date
tecc
0a81ab0309 change: Add From<Address> implementation for Mailbox (#879)
from-address: It's a simple implementation - it uses the address as the address and uses `None` for the name parameter.
2023-06-22 10:33:55 +02:00
Paolo Barbolini
ce363273fe Prepare 0.10.4 (#871) 2023-04-02 11:42:57 +02:00
Paolo Barbolini
e59ecc20e7 Bump rustls to 0.21 (#867) 2023-04-02 10:54:46 +02:00
Paolo Barbolini
4fb67a7da1 Prepare 0.10.3 (#860) 2023-02-20 11:56:28 +01:00
Paolo Barbolini
9041f210f4 Add Content-Type to all examples sending a basic text/plain message (#859) 2023-02-14 17:54:05 +00:00
Paolo Barbolini
77b7d40fb8 mailbox: replace serialize_str(&self.to_string()) with collect_str(self) (#858) 2023-02-14 18:35:29 +01:00
Paolo Barbolini
2b6d457f85 clippy: deny str_to_string and empty_structs_with_brackets (#857) 2023-02-14 18:33:10 +01:00
Stéphan Kochen
952c1b39df Add support for rustls-native-certs (#843) 2023-02-14 18:11:42 +01:00
Paolo Barbolini
7ecb87f9fd Prepare 0.10.2 (#853) 2023-01-29 14:58:41 +01:00
Paolo Barbolini
fd700b1717 cargo: switch to crates.io release of email-encoding v0.2 (#854) 2023-01-29 14:47:08 +01:00
Paolo Barbolini
f8f19d6af5 clippy: fix latest warnings (#855) 2023-01-29 13:46:57 +00:00
Paolo Barbolini
cc25223914 Update rsa to v0.8 (#852) 2023-01-24 10:26:25 +01:00
Paolo Barbolini
750573d38b Update base64 to v0.21 (#851) 2023-01-24 10:07:48 +01:00
finga
0734a96343 tracing: Write some logs when sending an email (#848)
Write a trace message when sending an email. Further, write a debug
message when using the sendmail transport method, containing which
program is called. And write a debug message containing the target
file name when the file transport method is used.

This should help improve #556 a tiny bit.

Co-authored-by: finga <finga@onders.org>
2023-01-24 09:14:31 +01:00
Christian Mandery
3c2f996856 Fix broken URL for IETF user-agent draft (#849) 2023-01-16 11:34:30 +00:00
lfuerderer
9cae29dd07 Add Content-Type header in documentation example (#841)
* Add Content-Type header in documentation example

In the example showing how to build a message from a pure string, set
the Content-Type to text/plain explicitly.
This header constant also includes the phrase charset=utf-8 so that
non-ascii characters will be displayed correctly.

* cargo fmt

* Fix generated email example

Co-authored-by: Paolo Barbolini <paolo.barbolini@m4ss.net>
2022-12-16 22:04:29 +00:00
Paolo Barbolini
e1a146c8f8 cargo: bump base64 to 0.20 (#840) 2022-12-10 22:29:01 +00:00
Paolo Barbolini
840a19784a cargo: require quoted_printable ^0.4.6 (#837) 2022-12-07 15:37:38 +00:00
Paolo Barbolini
5a61ba36b5 ci: bump nightly used for rustfmt (#817) 2022-11-12 19:51:24 +01:00
Paolo Barbolini
dbf0e53c31 Fix latest clippy warnings (#830) 2022-11-12 17:54:20 +00:00
Paolo Barbolini
c914a07379 Update dependencies (#829) 2022-11-12 17:45:19 +00:00
Paolo Barbolini
2c4fa39523 Use cargo weak dependency features to reduce tokio features (#785) 2022-11-12 17:34:55 +00:00
Clément DOUIN
28f0af16be Mailbox displays wrongly when containing a comma and a non-ascii char in its name (#827) 2022-11-12 17:23:06 +00:00
Paolo Barbolini
f0614be555 Bump MSRV to 1.60 (#828) 2022-11-12 17:13:14 +00:00
André Cruz
a3fcdf263d fix(transport): return whole smtp error string (#821)
We were only returning the first line of the error message,
but that may not be enough to get the whole context of the error.
Since we have already parsed the response, just return the whole error
to the user.

Related to #694
2022-09-22 16:30:04 +02:00
Paolo Barbolini
d4da2e1f14 ci: switch to Swatinem/rust-cache@v2 (#819) 2022-09-10 13:19:49 +02:00
Paolo Barbolini
5655958288 clippy: fix latest warning (#818) 2022-09-10 10:59:49 +00:00
Paolo Barbolini
11b4acf0cd Improve header encoding and wrapping (#811)
Also cleans up the encoder a lot, removing some
complicated logic introduced by the initial round
of implementation
2022-09-10 12:40:00 +02:00
Paolo Barbolini
b3b5df285a Bump idna to 0.3 (#816) 2022-09-09 07:38:59 +00:00
Tom Dryer
3c051d52e7 Remove dependency on regex crate (#815)
Replace implementation of DKIM body canonicalization to remove
dependency on the `regex` crate.

Fixes #768.
2022-08-22 09:44:10 +02:00
André Cruz
d6128a146e use a generic transport trait for async connections (#805)
Rely on a generic transport trait and allow passing in one. This
will enable use cases where we don't have a real Tokio TCP stream,
or have to bind a specific source address before establishing
the connection.
2022-07-27 09:40:13 +02:00
André Cruz
fab6680150 Fix clippy warnings (#807)
Depending on the features chosen these attributes were left
unused.
2022-07-25 18:00:17 +02:00
Paolo Barbolini
0c9fc6cb71 Prepare 0.10.1 (#804) 2022-07-20 10:44:55 +02:00
Paolo Barbolini
2228cbdf93 Fix SMTP dot stuffing (#803) 2022-07-19 23:20:44 +02:00
André Cruz
17c95b0fa8 Ensure connection is closed on abort (#801)
When aborting a connection, ensure the underlying stream is closed
before we return.
2022-07-19 21:13:51 +02:00
Paolo Barbolini
62725af00a Improve TlsVersion docs and remember to re-export it (#800) 2022-07-18 07:40:46 +00:00
André Cruz
758bf1a4a7 Configurable minimum TLS version (#799)
Added support for configuring the minimum accepted TLS version. The
supported versions differ between TLS toolkits.
2022-07-17 13:11:14 +02:00
Paolo Barbolini
054c79f914 Document the boring-tls features in lib.rs (#798) 2022-07-16 09:45:44 +00:00
André Cruz
985fa7edc4 Add support for boring TLS (#797)
In contexts where FIPS certification is mandatory, having the
ability to use the certified boring TLS library is sometimes necessary.
Added initial support for it, only one TLS toolkit can be enabled at
one time.
2022-07-16 11:28:14 +02:00
Paolo Barbolini
9004d4ccc5 Add documentation to undocumented items (#793) 2022-06-29 22:44:29 +02:00
Paolo Barbolini
10171f8c75 Prepare 0.10.0 release (#538) 2022-06-29 21:17:37 +02:00
Paolo Barbolini
99e805952d Make it possible to keep the Bcc header when building a message (#792) 2022-06-29 21:08:27 +02:00
Paolo Barbolini
2d21dde5a1 Add autoconfigure.rs example (#787) 2022-06-17 06:35:07 +00:00
Paolo Barbolini
6fec936c0c Remove useless vec! allocations (#786) 2022-06-16 18:17:19 +00:00
Paolo Barbolini
22dfa5aa96 MessageBuilder: improve order headers are defined in (#783) 2022-06-16 17:53:54 +00:00
Paolo Barbolini
44e4cfd622 clippy: make rules stricter (#784) 2022-06-16 19:42:13 +02:00
Paolo Barbolini
7ea3d38a00 Mailboxes: add FromIterator impl and optimize Extend impl (#782) 2022-06-11 18:18:55 +00:00
Paolo Barbolini
73b89f5a9f clippy: fix latest warnings (#781) 2022-06-11 16:31:12 +00:00
57 changed files with 1492 additions and 682 deletions

View File

@@ -13,7 +13,7 @@ env:
jobs: jobs:
rustfmt: rustfmt:
name: rustfmt / nightly-2022-02-11 name: rustfmt / nightly-2022-11-12
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
@@ -22,7 +22,7 @@ jobs:
- name: Install rust - name: Install rust
run: | run: |
rustup default nightly-2022-02-11 rustup default nightly-2022-11-12
rustup component add rustfmt rustup component add rustfmt
- name: cargo fmt - name: cargo fmt
@@ -52,17 +52,11 @@ jobs:
- name: Checkout - name: Checkout
uses: actions/checkout@v2 uses: actions/checkout@v2
- name: Setup cache
uses: actions/cache@v2
with:
path: |
~/.cargo/registry
~/.cargo/git
target
key: ${{ runner.os }}-cargo-check
- name: Install rust - name: Install rust
run: rustup update --no-self-update stable run: rustup update --no-self-update stable
- name: Setup cache
uses: Swatinem/rust-cache@v2
- name: Install cargo hack - name: Install cargo hack
run: cargo install cargo-hack --debug run: cargo install cargo-hack --debug
@@ -81,27 +75,21 @@ jobs:
rust: stable rust: stable
- name: beta - name: beta
rust: beta rust: beta
- name: 1.56.0 - name: 1.60.0
rust: 1.56.0 rust: 1.60.0
steps: steps:
- name: Checkout - name: Checkout
uses: actions/checkout@v2 uses: actions/checkout@v2
- name: Setup cache
uses: actions/cache@v2
with:
path: |
~/.cargo/registry
~/.cargo/git
target
key: ${{ runner.os }}-cargo-test-${{ matrix.rust }}
- name: Install rust - name: Install rust
run: | run: |
rustup default ${{ matrix.rust }} rustup default ${{ matrix.rust }}
rustup update --no-self-update ${{ matrix.rust }} rustup update --no-self-update ${{ matrix.rust }}
- name: Setup cache
uses: Swatinem/rust-cache@v2
- name: Install postfix - name: Install postfix
run: | run: |
DEBIAN_FRONTEND=noninteractive sudo apt-get update DEBIAN_FRONTEND=noninteractive sudo apt-get update
@@ -133,9 +121,12 @@ jobs:
- name: Test with default features - name: Test with default features
run: cargo test run: cargo test
- name: Test with all features - name: Test with all features (-native-tls)
run: cargo test --all-features run: cargo test --no-default-features --features async-std,async-std1,async-std1-rustls-tls,async-trait,base64,boring,boring-tls,builder,dkim,ed25519-dalek,email-encoding,fastrand,file-transport,file-transport-envelope,futures-io,futures-rustls,futures-util,hostname,httpdate,mime,mime03,nom,once_cell,pool,quoted_printable,rsa,rustls,rustls-native-certs,rustls-pemfile,rustls-tls,sendmail-transport,serde,serde_json,sha2,smtp-transport,socket2,tokio1,tokio1-boring-tls,tokio1-rustls-tls,tokio1_boring,tokio1_crate,tokio1_rustls,tracing,uuid,webpki-roots
- name: Test with all features (-boring-tls)
run: cargo test --no-default-features --features async-std,async-std1,async-std1-rustls-tls,async-trait,base64,builder,dkim,ed25519-dalek,email-encoding,fastrand,file-transport,file-transport-envelope,futures-io,futures-rustls,futures-util,hostname,httpdate,mime,mime03,native-tls,nom,once_cell,pool,quoted_printable,rsa,rustls,rustls-native-certs,rustls-pemfile,rustls-tls,sendmail-transport,serde,serde_json,sha2,smtp-transport,socket2,tokio1,tokio1-native-tls,tokio1-rustls-tls,tokio1_crate,tokio1_native_tls_crate,tokio1_rustls,tracing,uuid,webpki-roots
# coverage: # coverage:
# name: Coverage # name: Coverage
# runs-on: ubuntu-latest # runs-on: ubuntu-latest

View File

@@ -1,5 +1,103 @@
<a name="v0.10.4"></a>
### v0.10.4 (2023-04-02)
#### Misc
* Bumped rustls to 0.21 and all related dependencies ([#867])
[#867]: https://github.com/lettre/lettre/pull/867
<a name="v0.10.3"></a>
### v0.10.3 (2023-02-20)
#### Announcements
It was found that what had been used until now as a basic lettre 0.10
`MessageBuilder::body` example failed to mention that for maximum
compatibility with various email clients a `Content-Type` header
should always be present in the message.
##### Before
```rust
Message::builder()
// [...] some headers skipped for brevity
.body(String::from("A plaintext or html body"))?
```
##### Patch
```diff
Message::builder()
// [...] some headers skipped for brevity
+ .header(ContentType::TEXT_PLAIN) // or `TEXT_HTML` if the body is html
.body(String::from("A plaintext or html body"))?
```
#### Features
* Add support for rustls-native-certs when using rustls ([#843])
[#843]: https://github.com/lettre/lettre/pull/843
<a name="v0.10.2"></a>
### v0.10.2 (2023-01-29)
#### Upgrade notes
* MSRV is now 1.60 ([#828])
#### Features
* Allow providing a custom `tokio` stream for `AsyncSmtpTransport` ([#805])
* Return whole SMTP error message ([#821])
#### Bug fixes
* Mailbox displays wrongly when containing a comma and a non-ascii char in its name ([#827])
* Require `quoted_printable` ^0.4.6 in order to fix encoding of tabs and spaces at the end of line ([#837])
#### Misc
* Increase tracing ([#848])
* Bump `idna` to 0.3 ([#816])
* Update `base64` to 0.21 ([#840] and [#851])
* Update `rsa` to 0.8 ([#829] and [#852])
[#805]: https://github.com/lettre/lettre/pull/805
[#816]: https://github.com/lettre/lettre/pull/816
[#821]: https://github.com/lettre/lettre/pull/821
[#827]: https://github.com/lettre/lettre/pull/827
[#828]: https://github.com/lettre/lettre/pull/828
[#829]: https://github.com/lettre/lettre/pull/829
[#837]: https://github.com/lettre/lettre/pull/837
[#840]: https://github.com/lettre/lettre/pull/840
[#848]: https://github.com/lettre/lettre/pull/848
[#851]: https://github.com/lettre/lettre/pull/851
[#852]: https://github.com/lettre/lettre/pull/852
<a name="v0.10.1"></a>
### v0.10.1 (2022-07-20)
#### Features
* Add `boring-tls` support for `SmtpTransport` and `AsyncSmtpTransport`. The latter is only supported with the tokio runtime. ([#797]) ([#798])
* Make the minimum TLS version configurable. ([#799]) ([#800])
#### Bug Fixes
* Ensure connections are closed on abort. ([#801])
* Fix SMTP dot stuffing. ([#803])
[#797]: https://github.com/lettre/lettre/pull/797
[#798]: https://github.com/lettre/lettre/pull/798
[#799]: https://github.com/lettre/lettre/pull/799
[#800]: https://github.com/lettre/lettre/pull/800
[#801]: https://github.com/lettre/lettre/pull/801
[#803]: https://github.com/lettre/lettre/pull/803
<a name="v0.10.0"></a> <a name="v0.10.0"></a>
### v0.10.0 (unreleased) ### v0.10.0 (2022-06-29)
#### Upgrade notes #### Upgrade notes
@@ -29,6 +127,7 @@ Several breaking changes were made between 0.9 and 0.10, but changes should be s
* Refactor `TlsParameters` implementation to not expose the internal TLS library * Refactor `TlsParameters` implementation to not expose the internal TLS library
* `FileTransport` writes emails into `.eml` instead of `.json` * `FileTransport` writes emails into `.eml` instead of `.json`
* 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 `sendmail` and `file` transports aren't enabled by default anymore.
* 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 * The `SendmailTransport` now uses the `sendmail` command in current `PATH` by default instead of

View File

@@ -1,7 +1,7 @@
[package] [package]
name = "lettre" name = "lettre"
# remember to update html_root_url and README.md (Cargo.toml example and deps.rs badge) # remember to update html_root_url and README.md (Cargo.toml example and deps.rs badge)
version = "0.10.0-rc.7" version = "0.10.4"
description = "Email client" description = "Email client"
readme = "README.md" readme = "README.md"
homepage = "https://lettre.rs" homepage = "https://lettre.rs"
@@ -11,7 +11,7 @@ authors = ["Alexis Mousset <contact@amousset.me>", "Paolo Barbolini <paolo@paolo
categories = ["email", "network-programming"] categories = ["email", "network-programming"]
keywords = ["email", "smtp", "mailer", "message", "sendmail"] keywords = ["email", "smtp", "mailer", "message", "sendmail"]
edition = "2021" edition = "2021"
rust-version = "1.56" rust-version = "1.60"
[badges] [badges]
is-it-maintained-issue-resolution = { repository = "lettre/lettre" } is-it-maintained-issue-resolution = { repository = "lettre/lettre" }
@@ -19,7 +19,7 @@ is-it-maintained-open-issues = { repository = "lettre/lettre" }
maintenance = { status = "actively-developed" } maintenance = { status = "actively-developed" }
[dependencies] [dependencies]
idna = "0.2" idna = "0.3"
once_cell = { version = "1", optional = true } once_cell = { version = "1", optional = true }
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
@@ -27,9 +27,9 @@ tracing = { version = "0.1.16", default-features = false, features = ["std"], op
httpdate = { version = "1", optional = true } httpdate = { version = "1", optional = true }
mime = { version = "0.3.4", optional = true } 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.6", optional = true }
base64 = { version = "0.13", optional = true } base64 = { version = "0.21", optional = true }
email-encoding = { version = "0.1.1", optional = true } email-encoding = { version = "0.2", optional = true }
# file transport # file transport
uuid = { version = "1", features = ["v4"], optional = true } uuid = { version = "1", features = ["v4"], optional = true }
@@ -43,9 +43,11 @@ socket2 = { version = "0.4.4", optional = true }
## tls ## tls
native-tls = { version = "0.2", optional = true } # feature native-tls = { version = "0.2", optional = true } # feature
rustls = { version = "0.20", features = ["dangerous_configuration"], optional = true } rustls = { version = "0.21", features = ["dangerous_configuration"], optional = true }
rustls-pemfile = { version = "1", optional = true } rustls-pemfile = { version = "1", optional = true }
webpki-roots = { version = "0.22", optional = true } rustls-native-certs = { version = "0.6.2", optional = true }
webpki-roots = { version = "0.23", optional = true }
boring = { version = "2.0.0", optional = true }
# async # async
futures-io = { version = "0.3.7", optional = true } futures-io = { version = "0.3.7", optional = true }
@@ -53,34 +55,35 @@ futures-util = { version = "0.3.7", default-features = false, features = ["io"],
async-trait = { version = "0.1", optional = true } 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 }
#async-native-tls = { version = "0.3.3", optional = true } #async-native-tls = { version = "0.3.3", optional = true }
futures-rustls = { version = "0.22", optional = true } futures-rustls = { version = "0.24", optional = true }
## tokio ## tokio
tokio1_crate = { package = "tokio", version = "1", features = ["fs", "rt", "process", "time", "net", "io-util"], optional = true } tokio1_crate = { package = "tokio", version = "1", 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.23", optional = true } tokio1_rustls = { package = "tokio-rustls", version = "0.24", optional = true }
tokio1_boring = { package = "tokio-boring", version = "2.1.4", optional = true }
## dkim ## dkim
sha2 = { version = "0.10", optional = true } sha2 = { version = "0.10", optional = true, features = ["oid"] }
rsa = { version = "0.6.0", optional = true } rsa = { version = "0.8", optional = true }
ed25519-dalek = { version = "1.0.1", optional = true } ed25519-dalek = { version = "1.0.1", optional = true }
regex = { version = "1", default-features = false, features = ["std"], optional = true }
# email formats # email formats
email_address = { version = "0.2.1", default-features = false } email_address = { version = "0.2.1", default-features = false }
[dev-dependencies] [dev-dependencies]
pretty_assertions = "1" pretty_assertions = "1"
criterion = "0.3" criterion = "0.4"
tracing = { version = "0.1.16", default-features = false, features = ["std"] }
tracing-subscriber = "0.3" tracing-subscriber = "0.3"
glob = "0.3" glob = "0.3"
walkdir = "2" walkdir = "2"
tokio1_crate = { package = "tokio", version = "1", features = ["macros", "rt-multi-thread"] } tokio1_crate = { package = "tokio", version = "1", features = ["macros", "rt-multi-thread"] }
async-std = { version = "1.8", features = ["attributes"] } async-std = { version = "1.8", features = ["attributes"] }
serde_json = "1" serde_json = "1"
maud = "0.23" maud = "0.24"
[[bench]] [[bench]]
harness = false harness = false
@@ -92,15 +95,17 @@ builder = ["httpdate", "mime", "fastrand", "quoted_printable", "email-encoding"]
mime03 = ["mime"] mime03 = ["mime"]
# transports # transports
file-transport = ["uuid"] file-transport = ["uuid", "tokio1_crate?/fs", "tokio1_crate?/io-util"]
file-transport-envelope = ["serde", "serde_json", "file-transport"] file-transport-envelope = ["serde", "serde_json", "file-transport"]
sendmail-transport = [] sendmail-transport = ["tokio1_crate?/process", "tokio1_crate?/io-util", "async-std?/unstable"]
smtp-transport = ["base64", "nom", "socket2", "once_cell"] smtp-transport = ["base64", "nom", "socket2", "once_cell", "tokio1_crate?/rt", "tokio1_crate?/time", "tokio1_crate?/net"]
pool = ["futures-util"] pool = ["futures-util"]
rustls-tls = ["webpki-roots", "rustls", "rustls-pemfile"] rustls-tls = ["webpki-roots", "rustls", "rustls-pemfile"]
boring-tls = ["boring"]
# 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"]
@@ -108,13 +113,18 @@ 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"]
tokio1-boring-tls = ["tokio1", "boring-tls", "tokio1_boring"]
dkim = ["base64", "sha2", "rsa", "ed25519-dalek", "regex", "once_cell"] dkim = ["base64", "sha2", "rsa", "ed25519-dalek"]
[package.metadata.docs.rs] [package.metadata.docs.rs]
all-features = true all-features = true
rustdoc-args = ["--cfg", "docsrs", "--cfg", "lettre_ignore_tls_mismatch"] rustdoc-args = ["--cfg", "docsrs", "--cfg", "lettre_ignore_tls_mismatch"]
[[example]]
name = "autoconfigure"
required-features = ["smtp-transport", "native-tls"]
[[example]] [[example]]
name = "basic_html" name = "basic_html"
required-features = ["file-transport", "builder"] required-features = ["file-transport", "builder"]

View File

@@ -28,27 +28,14 @@
</div> </div>
<div align="center"> <div align="center">
<a href="https://deps.rs/crate/lettre/0.10.0-rc.7"> <a href="https://deps.rs/crate/lettre/0.10.4">
<img src="https://deps.rs/crate/lettre/0.10.0-rc.7/status.svg" <img src="https://deps.rs/crate/lettre/0.10.4/status.svg"
alt="dependency status" /> alt="dependency status" />
</a> </a>
</div> </div>
--- ---
**NOTE**: this readme refers to the 0.10 version of lettre, which is
in release candidate state. Use the [`v0.9.x`](https://github.com/lettre/lettre/tree/v0.9.x)
branch for the previous stable release.
0.10 is already widely used and is already thought to be more reliable than 0.9, so it should generally be used
for new projects.
We'd love to hear your feedback about 0.10 design and APIs before final release!
Start a [discussion](https://github.com/lettre/lettre/discussions) in the repository, whether for
feedback or if you need help or advice using or upgrading lettre 0.10.
---
## Features ## Features
Lettre provides the following features: Lettre provides the following features:
@@ -63,18 +50,24 @@ Lettre does not provide (for now):
* Email parsing * Email parsing
## 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.60, 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 ## Example
This library requires Rust 1.56.0 or newer. This library requires Rust 1.60 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.7" lettre = "0.10"
``` ```
```rust,no_run ```rust,no_run
use lettre::message::header::ContentType;
use lettre::transport::smtp::authentication::Credentials; use lettre::transport::smtp::authentication::Credentials;
use lettre::{Message, SmtpTransport, Transport}; use lettre::{Message, SmtpTransport, Transport};
@@ -83,10 +76,11 @@ let email = Message::builder()
.reply_to("Yuin <yuin@domain.tld>".parse().unwrap()) .reply_to("Yuin <yuin@domain.tld>".parse().unwrap())
.to("Hei <hei@domain.tld>".parse().unwrap()) .to("Hei <hei@domain.tld>".parse().unwrap())
.subject("Happy new year") .subject("Happy new year")
.header(ContentType::TEXT_PLAIN)
.body(String::from("Be happy!")) .body(String::from("Be happy!"))
.unwrap(); .unwrap();
let creds = Credentials::new("smtp_username".to_string(), "smtp_password".to_string()); let creds = Credentials::new("smtp_username".to_owned(), "smtp_password".to_owned());
// Open a remote connection to gmail // Open a remote connection to gmail
let mailer = SmtpTransport::relay("smtp.gmail.com") let mailer = SmtpTransport::relay("smtp.gmail.com")
@@ -101,6 +95,14 @@ match mailer.send(&email) {
} }
``` ```
## Not sure of which connect options to use?
Clone the lettre git repository and run the following command (replacing `SMTP_HOST` with your SMTP server's hostname)
```shell
cargo run --example autoconfigure SMTP_HOST
```
## Testing ## Testing
The `lettre` tests require an open mail server listening locally on port 2525 and the `sendmail` command. If you have python installed The `lettre` tests require an open mail server listening locally on port 2525 and the `sendmail` command. If you have python installed

View File

@@ -1,6 +1,6 @@
use lettre::{ use lettre::{
transport::smtp::authentication::Credentials, AsyncSmtpTransport, AsyncStd1Executor, message::header::ContentType, transport::smtp::authentication::Credentials, AsyncSmtpTransport,
AsyncTransport, Message, AsyncStd1Executor, AsyncTransport, Message,
}; };
#[async_std::main] #[async_std::main]
@@ -12,10 +12,11 @@ async fn main() {
.reply_to("Yuin <yuin@domain.tld>".parse().unwrap()) .reply_to("Yuin <yuin@domain.tld>".parse().unwrap())
.to("Hei <hei@domain.tld>".parse().unwrap()) .to("Hei <hei@domain.tld>".parse().unwrap())
.subject("Happy new async year") .subject("Happy new async year")
.header(ContentType::TEXT_PLAIN)
.body(String::from("Be happy with async!")) .body(String::from("Be happy with async!"))
.unwrap(); .unwrap();
let creds = Credentials::new("smtp_username".to_string(), "smtp_password".to_string()); let creds = Credentials::new("smtp_username".to_owned(), "smtp_password".to_owned());
// Open a remote connection to gmail using STARTTLS // Open a remote connection to gmail using STARTTLS
let mailer: AsyncSmtpTransport<AsyncStd1Executor> = let mailer: AsyncSmtpTransport<AsyncStd1Executor> =
@@ -27,6 +28,6 @@ async fn main() {
// Send the email // Send the email
match mailer.send(email).await { match mailer.send(email).await {
Ok(_) => println!("Email sent successfully!"), Ok(_) => println!("Email sent successfully!"),
Err(e) => panic!("Could not send email: {:?}", e), Err(e) => panic!("Could not send email: {e:?}"),
} }
} }

View File

@@ -1,6 +1,6 @@
use lettre::{ use lettre::{
transport::smtp::authentication::Credentials, AsyncSmtpTransport, AsyncStd1Executor, message::header::ContentType, transport::smtp::authentication::Credentials, AsyncSmtpTransport,
AsyncTransport, Message, AsyncStd1Executor, AsyncTransport, Message,
}; };
#[async_std::main] #[async_std::main]
@@ -12,10 +12,11 @@ async fn main() {
.reply_to("Yuin <yuin@domain.tld>".parse().unwrap()) .reply_to("Yuin <yuin@domain.tld>".parse().unwrap())
.to("Hei <hei@domain.tld>".parse().unwrap()) .to("Hei <hei@domain.tld>".parse().unwrap())
.subject("Happy new async year") .subject("Happy new async year")
.header(ContentType::TEXT_PLAIN)
.body(String::from("Be happy with async!")) .body(String::from("Be happy with async!"))
.unwrap(); .unwrap();
let creds = Credentials::new("smtp_username".to_string(), "smtp_password".to_string()); let creds = Credentials::new("smtp_username".to_owned(), "smtp_password".to_owned());
// Open a remote connection to gmail // Open a remote connection to gmail
let mailer: AsyncSmtpTransport<AsyncStd1Executor> = let mailer: AsyncSmtpTransport<AsyncStd1Executor> =
@@ -27,6 +28,6 @@ async fn main() {
// Send the email // Send the email
match mailer.send(email).await { match mailer.send(email).await {
Ok(_) => println!("Email sent successfully!"), Ok(_) => println!("Email sent successfully!"),
Err(e) => panic!("Could not send email: {:?}", e), Err(e) => panic!("Could not send email: {e:?}"),
} }
} }

93
examples/autoconfigure.rs Normal file
View File

@@ -0,0 +1,93 @@
use std::{env, process, time::Duration};
use lettre::SmtpTransport;
fn main() {
tracing_subscriber::fmt::init();
let smtp_host = match env::args().nth(1) {
Some(smtp_host) => smtp_host,
None => {
println!("Please provide the SMTP host as the first argument to this command");
process::exit(1);
}
};
// TLS wrapped connection
{
tracing::info!(
"Trying to establish a TLS wrapped connection to {}",
smtp_host
);
let transport = SmtpTransport::relay(&smtp_host)
.expect("build SmtpTransport::relay")
.timeout(Some(Duration::from_secs(10)))
.build();
match transport.test_connection() {
Ok(true) => {
tracing::info!("Successfully connected to {} via a TLS wrapped connection (SmtpTransport::relay). This is the fastest option available for connecting to an SMTP server", smtp_host);
}
Ok(false) => {
tracing::error!("Couldn't connect to {} via a TLS wrapped connection. No more information is available", smtp_host);
}
Err(err) => {
tracing::error!(err = %err, "Couldn't connect to {} via a TLS wrapped connection", smtp_host);
}
}
}
println!();
// Plaintext connection which MUST then successfully upgrade to TLS via STARTTLS
{
tracing::info!("Trying to establish a plaintext connection to {} and then updating it via the SMTP STARTTLS extension", smtp_host);
let transport = SmtpTransport::starttls_relay(&smtp_host)
.expect("build SmtpTransport::starttls_relay")
.timeout(Some(Duration::from_secs(10)))
.build();
match transport.test_connection() {
Ok(true) => {
tracing::info!("Successfully connected to {} via a plaintext connection which then got upgraded to TLS via the SMTP STARTTLS extension (SmtpTransport::starttls_relay). This is the second best option after the previous TLS wrapped option", smtp_host);
}
Ok(false) => {
tracing::error!(
"Couldn't connect to {} via STARTTLS. No more information is available",
smtp_host
);
}
Err(err) => {
tracing::error!(err = %err, "Couldn't connect to {} via STARTTLS", smtp_host);
}
}
}
println!();
// Plaintext connection (very insecure)
{
tracing::info!(
"Trying to establish a plaintext connection to {}",
smtp_host
);
let transport = SmtpTransport::builder_dangerous(&smtp_host)
.timeout(Some(Duration::from_secs(10)))
.build();
match transport.test_connection() {
Ok(true) => {
tracing::info!("Successfully connected to {} via a plaintext connection. This option is very insecure and shouldn't be used on the public internet (SmtpTransport::builder_dangerous)", smtp_host);
}
Ok(false) => {
tracing::error!(
"Couldn't connect to {} via a plaintext connection. No more information is available",
smtp_host
);
}
Err(err) => {
tracing::error!(err = %err, "Couldn't connect to {} via a plaintext connection", smtp_host);
}
}
}
}

View File

@@ -1,4 +1,4 @@
use lettre::{Message, SmtpTransport, Transport}; use lettre::{message::header::ContentType, Message, SmtpTransport, Transport};
fn main() { fn main() {
tracing_subscriber::fmt::init(); tracing_subscriber::fmt::init();
@@ -8,6 +8,7 @@ fn main() {
.reply_to("Yuin <yuin@domain.tld>".parse().unwrap()) .reply_to("Yuin <yuin@domain.tld>".parse().unwrap())
.to("Hei <hei@domain.tld>".parse().unwrap()) .to("Hei <hei@domain.tld>".parse().unwrap())
.subject("Happy new year") .subject("Happy new year")
.header(ContentType::TEXT_PLAIN)
.body(String::from("Be happy!")) .body(String::from("Be happy!"))
.unwrap(); .unwrap();
@@ -17,6 +18,6 @@ fn main() {
// Send the email // Send the email
match mailer.send(&email) { match mailer.send(&email) {
Ok(_) => println!("Email sent successfully!"), Ok(_) => println!("Email sent successfully!"),
Err(e) => panic!("Could not send email: {:?}", e), Err(e) => panic!("Could not send email: {e:?}"),
} }
} }

View File

@@ -1,6 +1,7 @@
use std::fs; use std::fs;
use lettre::{ use lettre::{
message::header::ContentType,
transport::smtp::{ transport::smtp::{
authentication::Credentials, authentication::Credentials,
client::{Certificate, Tls, TlsParameters}, client::{Certificate, Tls, TlsParameters},
@@ -16,18 +17,19 @@ fn main() {
.reply_to("Yuin <yuin@domain.tld>".parse().unwrap()) .reply_to("Yuin <yuin@domain.tld>".parse().unwrap())
.to("Hei <hei@domain.tld>".parse().unwrap()) .to("Hei <hei@domain.tld>".parse().unwrap())
.subject("Happy new year") .subject("Happy new year")
.header(ContentType::TEXT_PLAIN)
.body(String::from("Be happy!")) .body(String::from("Be happy!"))
.unwrap(); .unwrap();
// Use a custom certificate stored on disk to securely verify the server's certificate // Use a custom certificate stored on disk to securely verify the server's certificate
let pem_cert = fs::read("certificate.pem").unwrap(); let pem_cert = fs::read("certificate.pem").unwrap();
let cert = Certificate::from_pem(&pem_cert).unwrap(); let cert = Certificate::from_pem(&pem_cert).unwrap();
let tls = TlsParameters::builder("smtp.server.com".to_string()) let tls = TlsParameters::builder("smtp.server.com".to_owned())
.add_root_certificate(cert) .add_root_certificate(cert)
.build() .build()
.unwrap(); .unwrap();
let creds = Credentials::new("smtp_username".to_string(), "smtp_password".to_string()); let creds = Credentials::new("smtp_username".to_owned(), "smtp_password".to_owned());
// Open a remote connection to the smtp server // Open a remote connection to the smtp server
let mailer = SmtpTransport::builder_dangerous("smtp.server.com") let mailer = SmtpTransport::builder_dangerous("smtp.server.com")
@@ -39,6 +41,6 @@ fn main() {
// Send the email // Send the email
match mailer.send(&email) { match mailer.send(&email) {
Ok(_) => println!("Email sent successfully!"), Ok(_) => println!("Email sent successfully!"),
Err(e) => panic!("Could not send email: {:?}", e), Err(e) => panic!("Could not send email: {e:?}"),
} }
} }

View File

@@ -1,4 +1,7 @@
use lettre::{transport::smtp::authentication::Credentials, Message, SmtpTransport, Transport}; use lettre::{
message::header::ContentType, transport::smtp::authentication::Credentials, Message,
SmtpTransport, Transport,
};
fn main() { fn main() {
tracing_subscriber::fmt::init(); tracing_subscriber::fmt::init();
@@ -8,10 +11,11 @@ fn main() {
.reply_to("Yuin <yuin@domain.tld>".parse().unwrap()) .reply_to("Yuin <yuin@domain.tld>".parse().unwrap())
.to("Hei <hei@domain.tld>".parse().unwrap()) .to("Hei <hei@domain.tld>".parse().unwrap())
.subject("Happy new year") .subject("Happy new year")
.header(ContentType::TEXT_PLAIN)
.body(String::from("Be happy!")) .body(String::from("Be happy!"))
.unwrap(); .unwrap();
let creds = Credentials::new("smtp_username".to_string(), "smtp_password".to_string()); let creds = Credentials::new("smtp_username".to_owned(), "smtp_password".to_owned());
// Open a remote connection to gmail using STARTTLS // Open a remote connection to gmail using STARTTLS
let mailer = SmtpTransport::starttls_relay("smtp.gmail.com") let mailer = SmtpTransport::starttls_relay("smtp.gmail.com")
@@ -22,6 +26,6 @@ fn main() {
// Send the email // Send the email
match mailer.send(&email) { match mailer.send(&email) {
Ok(_) => println!("Email sent successfully!"), Ok(_) => println!("Email sent successfully!"),
Err(e) => panic!("Could not send email: {:?}", e), Err(e) => panic!("Could not send email: {e:?}"),
} }
} }

View File

@@ -1,4 +1,7 @@
use lettre::{transport::smtp::authentication::Credentials, Message, SmtpTransport, Transport}; use lettre::{
message::header::ContentType, transport::smtp::authentication::Credentials, Message,
SmtpTransport, Transport,
};
fn main() { fn main() {
tracing_subscriber::fmt::init(); tracing_subscriber::fmt::init();
@@ -8,10 +11,11 @@ fn main() {
.reply_to("Yuin <yuin@domain.tld>".parse().unwrap()) .reply_to("Yuin <yuin@domain.tld>".parse().unwrap())
.to("Hei <hei@domain.tld>".parse().unwrap()) .to("Hei <hei@domain.tld>".parse().unwrap())
.subject("Happy new year") .subject("Happy new year")
.header(ContentType::TEXT_PLAIN)
.body(String::from("Be happy!")) .body(String::from("Be happy!"))
.unwrap(); .unwrap();
let creds = Credentials::new("smtp_username".to_string(), "smtp_password".to_string()); let creds = Credentials::new("smtp_username".to_owned(), "smtp_password".to_owned());
// Open a remote connection to gmail // Open a remote connection to gmail
let mailer = SmtpTransport::relay("smtp.gmail.com") let mailer = SmtpTransport::relay("smtp.gmail.com")
@@ -22,6 +26,6 @@ fn main() {
// Send the email // Send the email
match mailer.send(&email) { match mailer.send(&email) {
Ok(_) => println!("Email sent successfully!"), Ok(_) => println!("Email sent successfully!"),
Err(e) => panic!("Could not send email: {:?}", e), Err(e) => panic!("Could not send email: {e:?}"),
} }
} }

View File

@@ -2,8 +2,8 @@
// since it uses Rust 2018 crate renaming to import tokio. // since it uses Rust 2018 crate renaming to import tokio.
// Won't be needed in user's code. // Won't be needed in user's code.
use lettre::{ use lettre::{
transport::smtp::authentication::Credentials, AsyncSmtpTransport, AsyncTransport, Message, message::header::ContentType, transport::smtp::authentication::Credentials, AsyncSmtpTransport,
Tokio1Executor, AsyncTransport, Message, Tokio1Executor,
}; };
use tokio1_crate as tokio; use tokio1_crate as tokio;
@@ -16,10 +16,11 @@ async fn main() {
.reply_to("Yuin <yuin@domain.tld>".parse().unwrap()) .reply_to("Yuin <yuin@domain.tld>".parse().unwrap())
.to("Hei <hei@domain.tld>".parse().unwrap()) .to("Hei <hei@domain.tld>".parse().unwrap())
.subject("Happy new async year") .subject("Happy new async year")
.header(ContentType::TEXT_PLAIN)
.body(String::from("Be happy with async!")) .body(String::from("Be happy with async!"))
.unwrap(); .unwrap();
let creds = Credentials::new("smtp_username".to_string(), "smtp_password".to_string()); let creds = Credentials::new("smtp_username".to_owned(), "smtp_password".to_owned());
// Open a remote connection to gmail using STARTTLS // Open a remote connection to gmail using STARTTLS
let mailer: AsyncSmtpTransport<Tokio1Executor> = let mailer: AsyncSmtpTransport<Tokio1Executor> =
@@ -31,6 +32,6 @@ async fn main() {
// Send the email // Send the email
match mailer.send(email).await { match mailer.send(email).await {
Ok(_) => println!("Email sent successfully!"), Ok(_) => println!("Email sent successfully!"),
Err(e) => panic!("Could not send email: {:?}", e), Err(e) => panic!("Could not send email: {e:?}"),
} }
} }

View File

@@ -2,8 +2,8 @@
// since it uses Rust 2018 crate renaming to import tokio. // since it uses Rust 2018 crate renaming to import tokio.
// Won't be needed in user's code. // Won't be needed in user's code.
use lettre::{ use lettre::{
transport::smtp::authentication::Credentials, AsyncSmtpTransport, AsyncTransport, Message, message::header::ContentType, transport::smtp::authentication::Credentials, AsyncSmtpTransport,
Tokio1Executor, AsyncTransport, Message, Tokio1Executor,
}; };
use tokio1_crate as tokio; use tokio1_crate as tokio;
@@ -16,10 +16,11 @@ async fn main() {
.reply_to("Yuin <yuin@domain.tld>".parse().unwrap()) .reply_to("Yuin <yuin@domain.tld>".parse().unwrap())
.to("Hei <hei@domain.tld>".parse().unwrap()) .to("Hei <hei@domain.tld>".parse().unwrap())
.subject("Happy new async year") .subject("Happy new async year")
.header(ContentType::TEXT_PLAIN)
.body(String::from("Be happy with async!")) .body(String::from("Be happy with async!"))
.unwrap(); .unwrap();
let creds = Credentials::new("smtp_username".to_string(), "smtp_password".to_string()); let creds = Credentials::new("smtp_username".to_owned(), "smtp_password".to_owned());
// Open a remote connection to gmail // Open a remote connection to gmail
let mailer: AsyncSmtpTransport<Tokio1Executor> = let mailer: AsyncSmtpTransport<Tokio1Executor> =
@@ -31,6 +32,6 @@ async fn main() {
// Send the email // Send the email
match mailer.send(email).await { match mailer.send(email).await {
Ok(_) => println!("Email sent successfully!"), Ok(_) => println!("Email sent successfully!"),
Err(e) => panic!("Could not send email: {:?}", e), Err(e) => panic!("Could not send email: {e:?}"),
} }
} }

View File

@@ -184,7 +184,7 @@ where
let domain = domain.as_ref(); let domain = domain.as_ref();
Address::check_domain(domain)?; Address::check_domain(domain)?;
let serialized = format!("{}@{}", user, domain); let serialized = format!("{user}@{domain}");
Ok(Address { Ok(Address {
serialized, serialized,
at_start: user.len(), at_start: user.len(),
@@ -226,7 +226,7 @@ fn check_address(val: &str) -> Result<usize, AddressError> {
Ok(user.len()) Ok(user.len())
} }
#[derive(Debug, PartialEq, Clone, Copy)] #[derive(Debug, PartialEq, Eq, Clone, Copy)]
/// Errors in email addresses parsing /// Errors in email addresses parsing
pub enum AddressError { pub enum AddressError {
/// Missing domain or user /// Missing domain or user

12
src/base64.rs Normal file
View File

@@ -0,0 +1,12 @@
use ::base64::{
engine::{general_purpose::STANDARD, Engine},
DecodeError,
};
pub(crate) fn encode<T: AsRef<[u8]>>(input: T) -> String {
STANDARD.encode(input)
}
pub(crate) fn decode<T: AsRef<[u8]>>(input: T) -> Result<Vec<u8>, DecodeError> {
STANDARD.decode(input)
}

View File

@@ -109,7 +109,6 @@ impl Executor for Tokio1Executor {
#[cfg(feature = "smtp-transport")] #[cfg(feature = "smtp-transport")]
type Sleep = tokio1_crate::time::Sleep; type Sleep = tokio1_crate::time::Sleep;
#[doc(hidden)]
#[cfg(feature = "smtp-transport")] #[cfg(feature = "smtp-transport")]
fn spawn<F>(fut: F) -> Self::Handle fn spawn<F>(fut: F) -> Self::Handle
where where
@@ -119,13 +118,11 @@ impl Executor for Tokio1Executor {
tokio1_crate::spawn(fut) tokio1_crate::spawn(fut)
} }
#[doc(hidden)]
#[cfg(feature = "smtp-transport")] #[cfg(feature = "smtp-transport")]
fn sleep(duration: Duration) -> Self::Sleep { fn sleep(duration: Duration) -> Self::Sleep {
tokio1_crate::time::sleep(duration) tokio1_crate::time::sleep(duration)
} }
#[doc(hidden)]
#[cfg(feature = "smtp-transport")] #[cfg(feature = "smtp-transport")]
async fn connect( async fn connect(
hostname: &str, hostname: &str,
@@ -166,13 +163,11 @@ impl Executor for Tokio1Executor {
Ok(conn) Ok(conn)
} }
#[doc(hidden)]
#[cfg(feature = "file-transport-envelope")] #[cfg(feature = "file-transport-envelope")]
async fn fs_read(path: &Path) -> IoResult<Vec<u8>> { async fn fs_read(path: &Path) -> IoResult<Vec<u8>> {
tokio1_crate::fs::read(path).await tokio1_crate::fs::read(path).await
} }
#[doc(hidden)]
#[cfg(feature = "file-transport")] #[cfg(feature = "file-transport")]
async fn fs_write(path: &Path, contents: &[u8]) -> IoResult<()> { async fn fs_write(path: &Path, contents: &[u8]) -> IoResult<()> {
tokio1_crate::fs::write(path, contents).await tokio1_crate::fs::write(path, contents).await
@@ -210,7 +205,6 @@ impl Executor for AsyncStd1Executor {
#[cfg(feature = "smtp-transport")] #[cfg(feature = "smtp-transport")]
type Sleep = BoxFuture<'static, ()>; type Sleep = BoxFuture<'static, ()>;
#[doc(hidden)]
#[cfg(feature = "smtp-transport")] #[cfg(feature = "smtp-transport")]
fn spawn<F>(fut: F) -> Self::Handle fn spawn<F>(fut: F) -> Self::Handle
where where
@@ -220,14 +214,12 @@ impl Executor for AsyncStd1Executor {
async_std::task::spawn(fut) async_std::task::spawn(fut)
} }
#[doc(hidden)]
#[cfg(feature = "smtp-transport")] #[cfg(feature = "smtp-transport")]
fn sleep(duration: Duration) -> Self::Sleep { fn sleep(duration: Duration) -> Self::Sleep {
let fut = async move { async_std::task::sleep(duration).await }; let fut = async move { async_std::task::sleep(duration).await };
Box::pin(fut) Box::pin(fut)
} }
#[doc(hidden)]
#[cfg(feature = "smtp-transport")] #[cfg(feature = "smtp-transport")]
async fn connect( async fn connect(
hostname: &str, hostname: &str,
@@ -267,13 +259,11 @@ impl Executor for AsyncStd1Executor {
Ok(conn) Ok(conn)
} }
#[doc(hidden)]
#[cfg(feature = "file-transport-envelope")] #[cfg(feature = "file-transport-envelope")]
async fn fs_read(path: &Path) -> IoResult<Vec<u8>> { async fn fs_read(path: &Path) -> IoResult<Vec<u8>> {
async_std::fs::read(path).await async_std::fs::read(path).await
} }
#[doc(hidden)]
#[cfg(feature = "file-transport")] #[cfg(feature = "file-transport")]
async fn fs_write(path: &Path, contents: &[u8]) -> IoResult<()> { async fn fs_write(path: &Path, contents: &[u8]) -> IoResult<()> {
async_std::fs::write(path, contents).await async_std::fs::write(path, contents).await
@@ -289,15 +279,13 @@ impl SpawnHandle for async_std::task::JoinHandle<()> {
} }
mod private { mod private {
use super::*;
pub trait Sealed {} pub trait Sealed {}
#[cfg(feature = "tokio1")] #[cfg(feature = "tokio1")]
impl Sealed for Tokio1Executor {} impl Sealed for super::Tokio1Executor {}
#[cfg(feature = "async-std1")] #[cfg(feature = "async-std1")]
impl Sealed for AsyncStd1Executor {} impl Sealed for super::AsyncStd1Executor {}
#[cfg(all(feature = "smtp-transport", feature = "tokio1"))] #[cfg(all(feature = "smtp-transport", feature = "tokio1"))]
impl Sealed for tokio1_crate::task::JoinHandle<()> {} impl Sealed for tokio1_crate::task::JoinHandle<()> {}

View File

@@ -6,7 +6,7 @@
//! * Secure defaults //! * Secure defaults
//! * Async support //! * Async support
//! //!
//! Lettre requires Rust 1.56.0 or newer. //! Lettre requires Rust 1.60 or newer.
//! //!
//! ## Features //! ## Features
//! //!
@@ -41,6 +41,15 @@
//! //!
//! NOTE: native-tls isn't supported with `async-std` //! NOTE: native-tls isn't supported with `async-std`
//! //!
//! #### SMTP over TLS via the boring crate (Boring TLS)
//!
//! _Secure SMTP connections using TLS from the `boring-tls` crate_
//!
//! * **boring-tls**: TLS support for the synchronous version of the API
//! * **tokio1-boring-tls**: TLS support for the `tokio1` async version of the API
//!
//! NOTE: boring-tls isn't supported with `async-std`
//!
//! #### SMTP over TLS via the rustls crate //! #### SMTP over TLS via the rustls crate
//! //!
//! _Secure SMTP connections using TLS from the `rustls-tls` crate_ //! _Secure SMTP connections using TLS from the `rustls-tls` crate_
@@ -100,7 +109,7 @@
//! [mime 0.3]: https://docs.rs/mime/0.3 //! [mime 0.3]: https://docs.rs/mime/0.3
//! [DKIM]: https://datatracker.ietf.org/doc/html/rfc6376 //! [DKIM]: https://datatracker.ietf.org/doc/html/rfc6376
#![doc(html_root_url = "https://docs.rs/crate/lettre/0.10.0-rc.7")] #![doc(html_root_url = "https://docs.rs/crate/lettre/0.10.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)]
@@ -112,12 +121,32 @@
unused_import_braces, unused_import_braces,
rust_2018_idioms, rust_2018_idioms,
clippy::string_add, clippy::string_add,
clippy::string_add_assign clippy::string_add_assign,
clippy::clone_on_ref_ptr,
clippy::verbose_file_reads,
clippy::unnecessary_self_imports,
clippy::string_to_string,
clippy::mem_forget,
clippy::cast_lossless,
clippy::inefficient_to_string,
clippy::inline_always,
clippy::linkedlist,
clippy::macro_use_imports,
clippy::manual_assert,
clippy::unnecessary_join,
clippy::wildcard_imports,
clippy::str_to_string,
clippy::empty_structs_with_brackets,
clippy::zero_sized_map_values
)] )]
#![cfg_attr(docsrs, feature(doc_cfg))] #![cfg_attr(docsrs, feature(doc_cfg))]
#[cfg(not(lettre_ignore_tls_mismatch))] #[cfg(not(lettre_ignore_tls_mismatch))]
mod compiletime_checks { mod compiletime_checks {
#[cfg(all(feature = "native-tls", feature = "boring-tls"))]
compile_error!("feature \"native-tls\" and feature \"boring-tls\" cannot be enabled at the same time, otherwise
the executable will fail to link.");
#[cfg(all( #[cfg(all(
feature = "tokio1", feature = "tokio1",
feature = "native-tls", feature = "native-tls",
@@ -136,6 +165,15 @@ mod compiletime_checks {
If you'd like to use `native-tls` make sure that the `rustls-tls` feature hasn't been enabled by mistake. If you'd like to use `native-tls` make sure that the `rustls-tls` feature hasn't been enabled by mistake.
Make sure to apply the same to any of your crate dependencies that use the `lettre` crate."); Make sure to apply the same to any of your crate dependencies that use the `lettre` crate.");
#[cfg(all(
feature = "tokio1",
feature = "boring-tls",
not(feature = "tokio1-boring-tls")
))]
compile_error!("Lettre is being built with the `tokio1` and the `boring-tls` features, but the `tokio1-boring-tls` feature hasn't been turned on.
If you'd like to use `boring-tls` make sure that the `rustls-tls` feature hasn't been enabled by mistake.
Make sure to apply the same to any of your crate dependencies that use the `lettre` crate.");
/* /*
#[cfg(all( #[cfg(all(
feature = "async-std1", feature = "async-std1",
@@ -167,6 +205,8 @@ Make sure to apply the same to any of your crate dependencies that use the `lett
} }
pub mod address; pub mod address;
#[cfg(any(feature = "smtp-transport", feature = "dkim"))]
mod base64;
pub mod error; pub mod error;
#[cfg(any(feature = "tokio1", feature = "async-std1"))] #[cfg(any(feature = "tokio1", feature = "async-std1"))]
mod executor; mod executor;

View File

@@ -96,7 +96,7 @@ impl Attachment {
builder.header(header::ContentDisposition::attachment(&filename)) builder.header(header::ContentDisposition::attachment(&filename))
} }
Disposition::Inline(content_id) => builder Disposition::Inline(content_id) => builder
.header(header::ContentId::from(format!("<{}>", content_id))) .header(header::ContentId::from(format!("<{content_id}>")))
.header(header::ContentDisposition::inline()), .header(header::ContentDisposition::inline()),
}; };
builder = builder.header(content_type); builder = builder.header(content_type);

View File

@@ -7,9 +7,7 @@ use std::{
}; };
use ed25519_dalek::Signer; use ed25519_dalek::Signer;
use once_cell::sync::Lazy; use rsa::{pkcs1::DecodeRsaPrivateKey, pkcs1v15::Pkcs1v15Sign, RsaPrivateKey};
use regex::bytes::Regex;
use rsa::{pkcs1::DecodeRsaPrivateKey, Hash, PaddingScheme, RsaPrivateKey};
use sha2::{Digest, Sha256}; use sha2::{Digest, Sha256};
use crate::message::{ use crate::message::{
@@ -96,9 +94,9 @@ impl Display for DkimSigningKeyError {
impl StdError for DkimSigningKeyError { impl StdError for DkimSigningKeyError {
fn source(&self) -> Option<&(dyn StdError + 'static)> { fn source(&self) -> Option<&(dyn StdError + 'static)> {
Some(match &self.0 { Some(match &self.0 {
InnerDkimSigningKeyError::Base64(err) => &*err, InnerDkimSigningKeyError::Base64(err) => err,
InnerDkimSigningKeyError::Rsa(err) => &*err, InnerDkimSigningKeyError::Rsa(err) => err,
InnerDkimSigningKeyError::Ed25519(err) => &*err, InnerDkimSigningKeyError::Ed25519(err) => err,
}) })
} }
} }
@@ -123,14 +121,14 @@ impl DkimSigningKey {
RsaPrivateKey::from_pkcs1_pem(private_key) RsaPrivateKey::from_pkcs1_pem(private_key)
.map_err(|err| DkimSigningKeyError(InnerDkimSigningKeyError::Rsa(err)))?, .map_err(|err| DkimSigningKeyError(InnerDkimSigningKeyError::Rsa(err)))?,
), ),
DkimSigningAlgorithm::Ed25519 => { DkimSigningAlgorithm::Ed25519 => InnerDkimSigningKey::Ed25519(
InnerDkimSigningKey::Ed25519( ed25519_dalek::Keypair::from_bytes(
ed25519_dalek::Keypair::from_bytes(&base64::decode(private_key).map_err( &crate::base64::decode(private_key).map_err(|err| {
|err| DkimSigningKeyError(InnerDkimSigningKeyError::Base64(err)), DkimSigningKeyError(InnerDkimSigningKeyError::Base64(err))
)?) })?,
.map_err(|err| DkimSigningKeyError(InnerDkimSigningKeyError::Ed25519(err)))?,
) )
} .map_err(|err| DkimSigningKeyError(InnerDkimSigningKeyError::Ed25519(err)))?,
),
})) }))
} }
fn get_signing_algorithm(&self) -> DkimSigningAlgorithm { fn get_signing_algorithm(&self) -> DkimSigningAlgorithm {
@@ -219,24 +217,34 @@ fn dkim_header_format(
/// Canonicalize the body of an email /// Canonicalize the body of an email
fn dkim_canonicalize_body( fn dkim_canonicalize_body(
body: &[u8], mut body: &[u8],
canonicalization: DkimCanonicalizationType, canonicalization: DkimCanonicalizationType,
) -> Cow<'_, [u8]> { ) -> Cow<'_, [u8]> {
static RE: Lazy<Regex> = Lazy::new(|| Regex::new("(\r\n)+$").unwrap());
static RE_DOUBLE_SPACE: Lazy<Regex> = Lazy::new(|| Regex::new("[\\t ]+").unwrap());
static RE_SPACE_EOL: Lazy<Regex> = Lazy::new(|| Regex::new("[\t ]\r\n").unwrap());
match canonicalization { match canonicalization {
DkimCanonicalizationType::Simple => RE.replace(body, &b"\r\n"[..]), DkimCanonicalizationType::Simple => {
DkimCanonicalizationType::Relaxed => { // Remove empty lines at end
let body = RE_DOUBLE_SPACE.replace_all(body, &b" "[..]); while body.ends_with(b"\r\n\r\n") {
let body = match RE_SPACE_EOL.replace_all(&body, &b"\r\n"[..]) { body = &body[..body.len() - 2];
Cow::Borrowed(_body) => body,
Cow::Owned(body) => Cow::Owned(body),
};
match RE.replace(&body, &b"\r\n"[..]) {
Cow::Borrowed(_body) => body,
Cow::Owned(body) => Cow::Owned(body),
} }
Cow::Borrowed(body)
}
DkimCanonicalizationType::Relaxed => {
let mut out = Vec::with_capacity(body.len());
loop {
match body {
[b' ' | b'\t', b'\r', b'\n', ..] => {}
[b' ' | b'\t', b' ' | b'\t', ..] => {}
[b' ' | b'\t', ..] => out.push(b' '),
[c, ..] => out.push(*c),
[] => break,
}
body = &body[1..];
}
// Remove empty lines at end
while out.ends_with(b"\r\n\r\n") {
out.truncate(out.len() - 2);
}
Cow::Owned(out)
} }
} }
} }
@@ -245,7 +253,7 @@ fn dkim_canonicalize_headers_relaxed(headers: &str) -> String {
let mut r = String::with_capacity(headers.len()); let mut r = String::with_capacity(headers.len());
fn skip_whitespace(h: &str) -> &str { fn skip_whitespace(h: &str) -> &str {
match h.as_bytes().get(0) { match h.as_bytes().first() {
Some(b' ' | b'\t') => skip_whitespace(&h[1..]), Some(b' ' | b'\t') => skip_whitespace(&h[1..]),
_ => h, _ => h,
} }
@@ -346,11 +354,11 @@ fn dkim_sign_fixed_time(message: &mut Message, dkim_config: &DkimConfig, timesta
.unwrap() .unwrap()
.as_secs(); .as_secs();
let headers = message.headers(); let headers = message.headers();
let body_hash = Sha256::digest(&dkim_canonicalize_body( let body_hash = Sha256::digest(dkim_canonicalize_body(
&message.body_raw(), &message.body_raw(),
dkim_config.canonicalization.body, dkim_config.canonicalization.body,
)); ));
let bh = base64::encode(body_hash); let bh = crate::base64::encode(body_hash);
let mut signed_headers_list = let mut signed_headers_list =
dkim_config dkim_config
.headers .headers
@@ -382,16 +390,13 @@ fn dkim_sign_fixed_time(message: &mut Message, dkim_config: &DkimConfig, timesta
hashed_headers.update(canonicalized_dkim_header.trim_end().as_bytes()); hashed_headers.update(canonicalized_dkim_header.trim_end().as_bytes());
let hashed_headers = hashed_headers.finalize(); let hashed_headers = hashed_headers.finalize();
let signature = match &dkim_config.private_key.0 { let signature = match &dkim_config.private_key.0 {
InnerDkimSigningKey::Rsa(private_key) => base64::encode( InnerDkimSigningKey::Rsa(private_key) => crate::base64::encode(
private_key private_key
.sign( .sign(Pkcs1v15Sign::new::<Sha256>(), &hashed_headers)
PaddingScheme::new_pkcs1v15_sign(Some(Hash::SHA2_256)),
&hashed_headers,
)
.unwrap(), .unwrap(),
), ),
InnerDkimSigningKey::Ed25519(private_key) => { InnerDkimSigningKey::Ed25519(private_key) => {
base64::encode(private_key.sign(&hashed_headers).to_bytes()) crate::base64::encode(private_key.sign(&hashed_headers).to_bytes())
} }
}; };
let dkim_header = dkim_header_format( let dkim_header = dkim_header_format(
@@ -416,8 +421,9 @@ mod test {
header::{HeaderName, HeaderValue}, header::{HeaderName, HeaderValue},
Header, Message, Header, Message,
}, },
dkim_canonicalize_headers, dkim_sign_fixed_time, DkimCanonicalization, dkim_canonicalize_body, dkim_canonicalize_headers, dkim_sign_fixed_time,
DkimCanonicalizationType, DkimConfig, DkimSigningAlgorithm, DkimSigningKey, DkimCanonicalization, DkimCanonicalizationType, DkimConfig, DkimSigningAlgorithm,
DkimSigningKey,
}; };
use crate::StdError; use crate::StdError;
@@ -471,9 +477,9 @@ cJ5Ku0OTwRtSMaseRPX+T4EfG1Caa/eunPPN4rh+CSup2BVVarOT
.from("Test O'Leary <test+ezrz@example.net>".parse().unwrap()) .from("Test O'Leary <test+ezrz@example.net>".parse().unwrap())
.to("Test2 <test2@example.org>".parse().unwrap()) .to("Test2 <test2@example.org>".parse().unwrap())
.date(std::time::UNIX_EPOCH) .date(std::time::UNIX_EPOCH)
.header(TestHeader("test test very very long with spaces and extra spaces \twill be folded to several lines ".to_string())) .header(TestHeader("test test very very long with spaces and extra spaces \twill be folded to several lines ".to_owned()))
.subject("Test with utf-8 ë") .subject("Test with utf-8 ë")
.body("test\r\n\r\ntest \ttest\r\n\r\n\r\n".to_string()).unwrap() .body("test\r\n\r\ntest \ttest\r\n\r\n\r\n".to_owned()).unwrap()
} }
#[test] #[test]
@@ -490,6 +496,24 @@ cJ5Ku0OTwRtSMaseRPX+T4EfG1Caa/eunPPN4rh+CSup2BVVarOT
assert_eq!(dkim_canonicalize_headers(["From", "Test"], &message.headers, DkimCanonicalizationType::Relaxed),"from:=?utf-8?b?VGVzdCBPJ0xlYXJ5?= <test+ezrz@example.net>\r\ntest:test test very very long with spaces and extra spaces will be folded to several lines\r\n") assert_eq!(dkim_canonicalize_headers(["From", "Test"], &message.headers, DkimCanonicalizationType::Relaxed),"from:=?utf-8?b?VGVzdCBPJ0xlYXJ5?= <test+ezrz@example.net>\r\ntest:test test very very long with spaces and extra spaces will be folded to several lines\r\n")
} }
#[test]
fn test_body_simple_canonicalize() {
let body = b" C \r\nD \t E\r\n\r\n\r\n";
assert_eq!(
dkim_canonicalize_body(body, DkimCanonicalizationType::Simple).into_owned(),
b" C \r\nD \t E\r\n"
);
}
#[test]
fn test_body_relaxed_canonicalize() {
let body = b" C \r\nD \t E\r\n\tF\r\n\t\r\n\r\n\r\n";
assert_eq!(
dkim_canonicalize_body(body, DkimCanonicalizationType::Relaxed).into_owned(),
b" C\r\nD E\r\n F\r\n"
);
}
#[test] #[test]
fn test_signature_rsa_simple() { fn test_signature_rsa_simple() {
let mut message = test_message(); let mut message = test_message();
@@ -497,8 +521,8 @@ cJ5Ku0OTwRtSMaseRPX+T4EfG1Caa/eunPPN4rh+CSup2BVVarOT
dkim_sign_fixed_time( dkim_sign_fixed_time(
&mut message, &mut message,
&DkimConfig::new( &DkimConfig::new(
"dkimtest".to_string(), "dkimtest".to_owned(),
"example.org".to_string(), "example.org".to_owned(),
signing_key, signing_key,
vec![ vec![
HeaderName::new_from_ascii_str("Date"), HeaderName::new_from_ascii_str("Date"),
@@ -546,8 +570,8 @@ cJ5Ku0OTwRtSMaseRPX+T4EfG1Caa/eunPPN4rh+CSup2BVVarOT
dkim_sign_fixed_time( dkim_sign_fixed_time(
&mut message, &mut message,
&DkimConfig::new( &DkimConfig::new(
"dkimtest".to_string(), "dkimtest".to_owned(),
"example.org".to_string(), "example.org".to_owned(),
signing_key, signing_key,
vec![ vec![
HeaderName::new_from_ascii_str("Date"), HeaderName::new_from_ascii_str("Date"),
@@ -564,7 +588,7 @@ cJ5Ku0OTwRtSMaseRPX+T4EfG1Caa/eunPPN4rh+CSup2BVVarOT
); );
let signed = message.formatted(); let signed = message.formatted();
let signed = std::str::from_utf8(&signed).unwrap(); let signed = std::str::from_utf8(&signed).unwrap();
println!("{}", signed); println!("{signed}");
assert_eq!( assert_eq!(
signed, signed,
std::concat!( std::concat!(

View File

@@ -99,7 +99,7 @@ mod test {
headers.insert_raw(HeaderValue::new( headers.insert_raw(HeaderValue::new(
HeaderName::new_from_ascii_str("Content-Transfer-Encoding"), HeaderName::new_from_ascii_str("Content-Transfer-Encoding"),
"7bit".to_string(), "7bit".to_owned(),
)); ));
assert_eq!( assert_eq!(
@@ -109,7 +109,7 @@ mod test {
headers.insert_raw(HeaderValue::new( headers.insert_raw(HeaderValue::new(
HeaderName::new_from_ascii_str("Content-Transfer-Encoding"), HeaderName::new_from_ascii_str("Content-Transfer-Encoding"),
"base64".to_string(), "base64".to_owned(),
)); ));
assert_eq!( assert_eq!(

View File

@@ -16,8 +16,8 @@ impl ContentDisposition {
pub fn inline() -> Self { pub fn inline() -> Self {
Self(HeaderValue::dangerous_new_pre_encoded( Self(HeaderValue::dangerous_new_pre_encoded(
Self::name(), Self::name(),
"inline".to_string(), "inline".to_owned(),
"inline".to_string(), "inline".to_owned(),
)) ))
} }
@@ -33,17 +33,19 @@ impl ContentDisposition {
} }
fn with_name(kind: &str, file_name: &str) -> Self { fn with_name(kind: &str, file_name: &str) -> Self {
let raw_value = format!("{}; filename=\"{}\"", kind, file_name); let raw_value = format!("{kind}; filename=\"{file_name}\"");
let mut encoded_value = String::new(); let mut encoded_value = String::new();
let line_len = "Content-Disposition: ".len(); let line_len = "Content-Disposition: ".len();
let mut w = EmailWriter::new(&mut encoded_value, line_len, false); {
w.write_str(kind).expect("writing `kind` returned an error"); let mut w = EmailWriter::new(&mut encoded_value, line_len, 0, false, false);
w.write_char(';').expect("writing `;` returned an error"); w.write_str(kind).expect("writing `kind` returned an error");
w.space(); w.write_char(';').expect("writing `;` returned an error");
w.optional_breakpoint();
email_encoding::headers::rfc2231::encode("filename", file_name, &mut w) email_encoding::headers::rfc2231::encode("filename", file_name, &mut w)
.expect("some Write implementation returned an error"); .expect("some Write implementation returned an error");
}
Self(HeaderValue::dangerous_new_pre_encoded( Self(HeaderValue::dangerous_new_pre_encoded(
Self::name(), Self::name(),
@@ -88,12 +90,12 @@ mod test {
headers.set(ContentDisposition::inline()); headers.set(ContentDisposition::inline());
assert_eq!(format!("{}", headers), "Content-Disposition: inline\r\n"); assert_eq!(format!("{headers}"), "Content-Disposition: inline\r\n");
headers.set(ContentDisposition::attachment("something.txt")); headers.set(ContentDisposition::attachment("something.txt"));
assert_eq!( assert_eq!(
format!("{}", headers), format!("{headers}"),
"Content-Disposition: attachment; filename=\"something.txt\"\r\n" "Content-Disposition: attachment; filename=\"something.txt\"\r\n"
); );
} }
@@ -104,7 +106,7 @@ mod test {
headers.insert_raw(HeaderValue::new( headers.insert_raw(HeaderValue::new(
HeaderName::new_from_ascii_str("Content-Disposition"), HeaderName::new_from_ascii_str("Content-Disposition"),
"inline".to_string(), "inline".to_owned(),
)); ));
assert_eq!( assert_eq!(
@@ -114,7 +116,7 @@ mod test {
headers.insert_raw(HeaderValue::new( headers.insert_raw(HeaderValue::new(
HeaderName::new_from_ascii_str("Content-Disposition"), HeaderName::new_from_ascii_str("Content-Disposition"),
"attachment; filename=\"something.txt\"".to_string(), "attachment; filename=\"something.txt\"".to_owned(),
)); ));
assert_eq!( assert_eq!(

View File

@@ -135,8 +135,7 @@ mod serde {
match ContentType::parse(mime) { match ContentType::parse(mime) {
Ok(content_type) => Ok(content_type), Ok(content_type) => Ok(content_type),
Err(_) => Err(E::custom(format!( Err(_) => Err(E::custom(format!(
"Couldn't parse the following MIME-Type: {}", "Couldn't parse the following MIME-Type: {mime}"
mime
))), ))),
} }
} }
@@ -179,14 +178,14 @@ mod test {
headers.insert_raw(HeaderValue::new( headers.insert_raw(HeaderValue::new(
HeaderName::new_from_ascii_str("Content-Type"), HeaderName::new_from_ascii_str("Content-Type"),
"text/plain; charset=utf-8".to_string(), "text/plain; charset=utf-8".to_owned(),
)); ));
assert_eq!(headers.get::<ContentType>(), Some(ContentType::TEXT_PLAIN)); assert_eq!(headers.get::<ContentType>(), Some(ContentType::TEXT_PLAIN));
headers.insert_raw(HeaderValue::new( headers.insert_raw(HeaderValue::new(
HeaderName::new_from_ascii_str("Content-Type"), HeaderName::new_from_ascii_str("Content-Type"),
"text/html; charset=utf-8".to_string(), "text/html; charset=utf-8".to_owned(),
)); ));
assert_eq!(headers.get::<ContentType>(), Some(ContentType::TEXT_HTML)); assert_eq!(headers.get::<ContentType>(), Some(ContentType::TEXT_HTML));

View File

@@ -8,7 +8,7 @@ use crate::BoxError;
/// Message `Date` header /// Message `Date` header
/// ///
/// Defined in [RFC2822](https://tools.ietf.org/html/rfc2822#section-3.3) /// Defined in [RFC2822](https://tools.ietf.org/html/rfc2822#section-3.3)
#[derive(Debug, Clone, Copy, PartialEq)] #[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Date(HttpDate); pub struct Date(HttpDate);
impl Date { impl Date {
@@ -90,7 +90,7 @@ mod test {
assert_eq!( assert_eq!(
headers.to_string(), headers.to_string(),
"Date: Tue, 15 Nov 1994 08:12:31 +0000\r\n".to_string() "Date: Tue, 15 Nov 1994 08:12:31 +0000\r\n".to_owned()
); );
// Tue, 15 Nov 1994 08:12:32 GMT // Tue, 15 Nov 1994 08:12:32 GMT
@@ -110,7 +110,7 @@ mod test {
headers.insert_raw(HeaderValue::new( headers.insert_raw(HeaderValue::new(
HeaderName::new_from_ascii_str("Date"), HeaderName::new_from_ascii_str("Date"),
"Tue, 15 Nov 1994 08:12:31 +0000".to_string(), "Tue, 15 Nov 1994 08:12:31 +0000".to_owned(),
)); ));
assert_eq!( assert_eq!(
@@ -122,7 +122,7 @@ mod test {
headers.insert_raw(HeaderValue::new( headers.insert_raw(HeaderValue::new(
HeaderName::new_from_ascii_str("Date"), HeaderName::new_from_ascii_str("Date"),
"Tue, 15 Nov 1994 08:12:32 +0000".to_string(), "Tue, 15 Nov 1994 08:12:32 +0000".to_owned(),
)); ));
assert_eq!( assert_eq!(

View File

@@ -14,7 +14,7 @@ pub trait MailboxesHeader {
macro_rules! mailbox_header { macro_rules! mailbox_header {
($(#[$doc:meta])*($type_name: ident, $header_name: expr)) => { ($(#[$doc:meta])*($type_name: ident, $header_name: expr)) => {
$(#[$doc])* $(#[$doc])*
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq, Eq)]
pub struct $type_name(Mailbox); pub struct $type_name(Mailbox);
impl Header for $type_name { impl Header for $type_name {
@@ -30,8 +30,10 @@ macro_rules! mailbox_header {
fn display(&self) -> HeaderValue { fn display(&self) -> HeaderValue {
let mut encoded_value = String::new(); let mut encoded_value = String::new();
let line_len = $header_name.len() + ": ".len(); let line_len = $header_name.len() + ": ".len();
let mut w = EmailWriter::new(&mut encoded_value, line_len, false); {
self.0.encode(&mut w).expect("writing `Mailbox` returned an error"); let mut w = EmailWriter::new(&mut encoded_value, line_len, 0, false, false);
self.0.encode(&mut w).expect("writing `Mailbox` returned an error");
}
HeaderValue::dangerous_new_pre_encoded(Self::name(), self.0.to_string(), encoded_value) HeaderValue::dangerous_new_pre_encoded(Self::name(), self.0.to_string(), encoded_value)
} }
@@ -56,7 +58,7 @@ macro_rules! mailbox_header {
macro_rules! mailboxes_header { macro_rules! mailboxes_header {
($(#[$doc:meta])*($type_name: ident, $header_name: expr)) => { ($(#[$doc:meta])*($type_name: ident, $header_name: expr)) => {
$(#[$doc])* $(#[$doc])*
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq, Eq)]
pub struct $type_name(pub(crate) Mailboxes); pub struct $type_name(pub(crate) Mailboxes);
impl MailboxesHeader for $type_name { impl MailboxesHeader for $type_name {
@@ -78,8 +80,10 @@ macro_rules! mailboxes_header {
fn display(&self) -> HeaderValue { fn display(&self) -> HeaderValue {
let mut encoded_value = String::new(); let mut encoded_value = String::new();
let line_len = $header_name.len() + ": ".len(); let line_len = $header_name.len() + ": ".len();
let mut w = EmailWriter::new(&mut encoded_value, line_len, false); {
self.0.encode(&mut w).expect("writing `Mailboxes` returned an error"); let mut w = EmailWriter::new(&mut encoded_value, line_len, 0, false, false);
self.0.encode(&mut w).expect("writing `Mailboxes` returned an error");
}
HeaderValue::dangerous_new_pre_encoded(Self::name(), self.0.to_string(), encoded_value) HeaderValue::dangerous_new_pre_encoded(Self::name(), self.0.to_string(), encoded_value)
} }
@@ -248,7 +252,7 @@ mod test {
let mut headers = Headers::new(); let mut headers = Headers::new();
headers.insert_raw(HeaderValue::new( headers.insert_raw(HeaderValue::new(
HeaderName::new_from_ascii_str("From"), HeaderName::new_from_ascii_str("From"),
"kayo@example.com".to_string(), "kayo@example.com".to_owned(),
)); ));
assert_eq!(headers.get::<From>(), Some(From(from))); assert_eq!(headers.get::<From>(), Some(From(from)));
@@ -261,7 +265,7 @@ mod test {
let mut headers = Headers::new(); let mut headers = Headers::new();
headers.insert_raw(HeaderValue::new( headers.insert_raw(HeaderValue::new(
HeaderName::new_from_ascii_str("From"), HeaderName::new_from_ascii_str("From"),
"K. <kayo@example.com>".to_string(), "K. <kayo@example.com>".to_owned(),
)); ));
assert_eq!(headers.get::<From>(), Some(From(from))); assert_eq!(headers.get::<From>(), Some(From(from)));
@@ -277,7 +281,7 @@ mod test {
let mut headers = Headers::new(); let mut headers = Headers::new();
headers.insert_raw(HeaderValue::new( headers.insert_raw(HeaderValue::new(
HeaderName::new_from_ascii_str("From"), HeaderName::new_from_ascii_str("From"),
"kayo@example.com, pony@domain.tld".to_string(), "kayo@example.com, pony@domain.tld".to_owned(),
)); ));
assert_eq!(headers.get::<From>(), Some(From(from.into()))); assert_eq!(headers.get::<From>(), Some(From(from.into())));
@@ -293,7 +297,7 @@ mod test {
let mut headers = Headers::new(); let mut headers = Headers::new();
headers.insert_raw(HeaderValue::new( headers.insert_raw(HeaderValue::new(
HeaderName::new_from_ascii_str("From"), HeaderName::new_from_ascii_str("From"),
"K. <kayo@example.com>, Pony P. <pony@domain.tld>".to_string(), "K. <kayo@example.com>, Pony P. <pony@domain.tld>".to_owned(),
)); ));
assert_eq!(headers.get::<From>(), Some(From(from.into()))); assert_eq!(headers.get::<From>(), Some(From(from.into())));
@@ -309,7 +313,7 @@ mod test {
let mut headers = Headers::new(); let mut headers = Headers::new();
headers.insert_raw(HeaderValue::new( headers.insert_raw(HeaderValue::new(
HeaderName::new_from_ascii_str("From"), HeaderName::new_from_ascii_str("From"),
"Test, test <1@example.com>, Test2, test2 <2@example.com>".to_string(), "Test, test <1@example.com>, Test2, test2 <2@example.com>".to_owned(),
)); ));
assert_eq!(headers.get::<From>(), Some(From(from.into()))); assert_eq!(headers.get::<From>(), Some(From(from.into())));
@@ -320,7 +324,7 @@ mod test {
let mut headers = Headers::new(); let mut headers = Headers::new();
headers.insert_raw(HeaderValue::new( headers.insert_raw(HeaderValue::new(
HeaderName::new_from_ascii_str("From"), HeaderName::new_from_ascii_str("From"),
"Test, test <1@example.com>, Test2, test2".to_string(), "Test, test <1@example.com>, Test2, test2".to_owned(),
)); ));
assert_eq!(headers.get::<From>(), None); assert_eq!(headers.get::<From>(), None);

View File

@@ -277,6 +277,7 @@ impl PartialEq<HeaderName> for &str {
} }
} }
/// A safe for use header value
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
pub struct HeaderValue { pub struct HeaderValue {
name: HeaderName, name: HeaderName,
@@ -285,6 +286,12 @@ pub struct HeaderValue {
} }
impl HeaderValue { impl HeaderValue {
/// Construct a new `HeaderValue` and encode it
///
/// Takes the header `name` and the `raw_value` and encodes
/// it via `RFC2047` and line folds it.
///
/// [`RFC2047`]: https://datatracker.ietf.org/doc/html/rfc2047
pub fn new(name: HeaderName, raw_value: String) -> Self { pub fn new(name: HeaderName, raw_value: String) -> Self {
let mut encoded_value = String::with_capacity(raw_value.len()); let mut encoded_value = String::with_capacity(raw_value.len());
HeaderValueEncoder::encode(&name, &raw_value, &mut encoded_value).unwrap(); HeaderValueEncoder::encode(&name, &raw_value, &mut encoded_value).unwrap();
@@ -296,6 +303,14 @@ impl HeaderValue {
} }
} }
/// Construct a new `HeaderValue` using a pre-encoded header value
///
/// This method is _extremely_ dangerous as it opens up
/// the encoder to header injection attacks, but is sometimes
/// acceptable for use if `encoded_value` contains only ascii
/// printable characters and is already line folded.
///
/// When in doubt use [`HeaderValue::new`].
pub fn dangerous_new_pre_encoded( pub fn dangerous_new_pre_encoded(
name: HeaderName, name: HeaderName,
raw_value: String, raw_value: String,
@@ -308,10 +323,12 @@ impl HeaderValue {
} }
} }
#[cfg(feature = "dkim")]
pub(crate) fn get_raw(&self) -> &str { pub(crate) fn get_raw(&self) -> &str {
&self.raw_value &self.raw_value
} }
#[cfg(feature = "dkim")]
pub(crate) fn get_encoded(&self) -> &str { pub(crate) fn get_encoded(&self) -> &str {
&self.encoded_value &self.encoded_value
} }
@@ -325,28 +342,21 @@ struct HeaderValueEncoder<'a> {
impl<'a> HeaderValueEncoder<'a> { impl<'a> HeaderValueEncoder<'a> {
fn encode(name: &str, value: &'a str, f: &'a mut impl fmt::Write) -> fmt::Result { fn encode(name: &str, value: &'a str, f: &'a mut impl fmt::Write) -> fmt::Result {
let (words_iter, encoder) = Self::new(name, value, f); let encoder = Self::new(name, f);
encoder.format(words_iter) encoder.format(value.split_inclusive(' '))
} }
fn new( fn new(name: &str, writer: &'a mut dyn Write) -> Self {
name: &str,
value: &'a str,
writer: &'a mut dyn Write,
) -> (WordsPlusFillIterator<'a>, Self) {
let line_len = name.len() + ": ".len(); let line_len = name.len() + ": ".len();
let writer = EmailWriter::new(writer, line_len, false); let writer = EmailWriter::new(writer, line_len, 0, false, false);
( Self {
WordsPlusFillIterator { s: value }, writer,
Self { encode_buf: String::new(),
writer, }
encode_buf: String::new(),
},
)
} }
fn format(mut self, words_iter: WordsPlusFillIterator<'_>) -> fmt::Result { fn format(mut self, words_iter: impl Iterator<Item = &'a str>) -> fmt::Result {
for next_word in words_iter { for next_word in words_iter {
let allowed = allowed_str(next_word); let allowed = allowed_str(next_word);
@@ -374,71 +384,20 @@ impl<'a> HeaderValueEncoder<'a> {
return Ok(()); return Ok(());
} }
// It is important that we don't encode leading whitespace otherwise it breaks wrapping. let prefix = self.encode_buf.trim_end_matches(' ');
let first_not_allowed = self email_encoding::headers::rfc2047::encode(prefix, &mut self.writer)?;
.encode_buf
.bytes()
.enumerate()
.find(|(_i, c)| !allowed_char(*c))
.map(|(i, _)| i);
// May as well also write the tail in plain text.
let last_not_allowed = self
.encode_buf
.bytes()
.enumerate()
.rev()
.find(|(_i, c)| !allowed_char(*c))
.map(|(i, _)| i + 1);
let (prefix, to_encode, suffix) = match first_not_allowed { // TODO: add a better API for doing this in email-encoding
Some(first_not_allowed) => { let spaces = self.encode_buf.len() - prefix.len();
let last_not_allowed = last_not_allowed.unwrap(); for _ in 0..spaces {
self.writer.space();
let (remaining, suffix) = self.encode_buf.split_at(last_not_allowed); }
let (prefix, to_encode) = remaining.split_at(first_not_allowed);
(prefix, to_encode, suffix)
}
None => ("", self.encode_buf.as_str(), ""),
};
self.writer.folding().write_str(prefix)?;
email_encoding::headers::rfc2047::encode(to_encode, &mut self.writer)?;
self.writer.folding().write_str(suffix)?;
self.encode_buf.clear(); self.encode_buf.clear();
Ok(()) Ok(())
} }
} }
/// Iterator yielding a string split by space, but spaces are included before the next word.
struct WordsPlusFillIterator<'a> {
s: &'a str,
}
impl<'a> Iterator for WordsPlusFillIterator<'a> {
type Item = &'a str;
fn next(&mut self) -> Option<Self::Item> {
if self.s.is_empty() {
return None;
}
let next_word = self
.s
.bytes()
.enumerate()
.skip(1)
.find(|&(_i, c)| c == b' ')
.map(|(i, _)| i)
.unwrap_or(self.s.len());
let word = &self.s[..next_word];
self.s = &self.s[word.len()..];
Some(word)
}
}
fn allowed_str(s: &str) -> bool { fn allowed_str(s: &str) -> bool {
s.bytes().all(allowed_char) s.bytes().all(allowed_char)
} }
@@ -451,7 +410,8 @@ const fn allowed_char(c: u8) -> bool {
mod tests { mod tests {
use pretty_assertions::assert_eq; use pretty_assertions::assert_eq;
use super::{HeaderName, HeaderValue, Headers}; use super::{HeaderName, HeaderValue, Headers, To};
use crate::message::Mailboxes;
#[test] #[test]
fn valid_headername() { fn valid_headername() {
@@ -514,7 +474,7 @@ mod tests {
let mut headers = Headers::new(); let mut headers = Headers::new();
headers.insert_raw(HeaderValue::new( headers.insert_raw(HeaderValue::new(
HeaderName::new_from_ascii_str("To"), HeaderName::new_from_ascii_str("To"),
"John Doe <example@example.com>, Jean Dupont <jean@example.com>".to_string(), "John Doe <example@example.com>, Jean Dupont <jean@example.com>".to_owned(),
)); ));
assert_eq!( assert_eq!(
@@ -528,7 +488,7 @@ mod tests {
let mut headers = Headers::new(); let mut headers = Headers::new();
headers.insert_raw(HeaderValue::new( headers.insert_raw(HeaderValue::new(
HeaderName::new_from_ascii_str("To"), HeaderName::new_from_ascii_str("To"),
"Ascii <example@example.com>, John Doe <johndoe@example.com, John Smith <johnsmith@example.com>, Pinco Pallino <pincopallino@example.com>, Jemand <jemand@example.com>, Jean Dupont <jean@example.com>".to_string(), "Ascii <example@example.com>, John Doe <johndoe@example.com, John Smith <johnsmith@example.com>, Pinco Pallino <pincopallino@example.com>, Jemand <jemand@example.com>, Jean Dupont <jean@example.com>".to_owned(),
)); ));
assert_eq!( assert_eq!(
@@ -546,7 +506,7 @@ mod tests {
let mut headers = Headers::new(); let mut headers = Headers::new();
headers.insert_raw(HeaderValue::new( headers.insert_raw(HeaderValue::new(
HeaderName::new_from_ascii_str("Subject"), HeaderName::new_from_ascii_str("Subject"),
"Hello! This is lettre, and this IsAVeryLongLineDoYouKnowWhatsGoingToHappenIGuessWeAreGoingToFindOut. Ok I guess that's it!".to_string() "Hello! This is lettre, and this IsAVeryLongLineDoYouKnowWhatsGoingToHappenIGuessWeAreGoingToFindOut. Ok I guess that's it!".to_owned()
)); ));
assert_eq!( assert_eq!(
@@ -565,7 +525,7 @@ mod tests {
headers.insert_raw( headers.insert_raw(
HeaderValue::new( HeaderValue::new(
HeaderName::new_from_ascii_str("Subject"), HeaderName::new_from_ascii_str("Subject"),
"Hello! IGuessTheLastLineWasntLongEnoughSoLetsTryAgainShallWeWhatDoYouThinkItsGoingToHappenIGuessWereAboutToFindOut! I don't know".to_string() "Hello! IGuessTheLastLineWasntLongEnoughSoLetsTryAgainShallWeWhatDoYouThinkItsGoingToHappenIGuessWereAboutToFindOut! I don't know".to_owned()
)); ));
assert_eq!( assert_eq!(
@@ -583,7 +543,7 @@ mod tests {
let mut headers = Headers::new(); let mut headers = Headers::new();
headers.insert_raw(HeaderValue::new( headers.insert_raw(HeaderValue::new(
HeaderName::new_from_ascii_str("Subject"), HeaderName::new_from_ascii_str("Subject"),
"1abcdefghijklmnopqrstuvwxyz2abcdefghijklmnopqrstuvwxyz3abcdefghijklmnopqrstuvwxyz4abcdefghijklmnopqrstuvwxyz5abcdefghijklmnopqrstuvwxyz6abcdefghijklmnopqrstuvwxyz".to_string() "1abcdefghijklmnopqrstuvwxyz2abcdefghijklmnopqrstuvwxyz3abcdefghijklmnopqrstuvwxyz4abcdefghijklmnopqrstuvwxyz5abcdefghijklmnopqrstuvwxyz6abcdefghijklmnopqrstuvwxyz".to_owned()
)); ));
assert_eq!( assert_eq!(
@@ -597,12 +557,12 @@ mod tests {
let mut headers = Headers::new(); let mut headers = Headers::new();
headers.insert_raw(HeaderValue::new( headers.insert_raw(HeaderValue::new(
HeaderName::new_from_ascii_str("To"), HeaderName::new_from_ascii_str("To"),
"Seán <sean@example.com>".to_string(), "Seán <sean@example.com>".to_owned(),
)); ));
assert_eq!( assert_eq!(
headers.to_string(), headers.to_string(),
"To: Se=?utf-8?b?w6E=?=n <sean@example.com>\r\n" "To: =?utf-8?b?U2XDoW4=?= <sean@example.com>\r\n"
); );
} }
@@ -611,7 +571,7 @@ mod tests {
let mut headers = Headers::new(); let mut headers = Headers::new();
headers.insert_raw(HeaderValue::new( headers.insert_raw(HeaderValue::new(
HeaderName::new_from_ascii_str("To"), HeaderName::new_from_ascii_str("To"),
"🌎 <world@example.com>".to_string(), "🌎 <world@example.com>".to_owned(),
)); ));
assert_eq!( assert_eq!(
@@ -623,19 +583,46 @@ mod tests {
#[test] #[test]
fn format_special_with_folding() { fn format_special_with_folding() {
let mut headers = Headers::new(); let mut headers = Headers::new();
headers.insert_raw(HeaderValue::new( let to = To::from(Mailboxes::from_iter([
HeaderName::new_from_ascii_str("To"), "🌍 <world@example.com>".parse().unwrap(),
"🌍 <world@example.com>, 🦆 Everywhere <ducks@example.com>, Иванов Иван Иванович <ivanov@example.com>, Jānis Bērziņš <janis@example.com>, Seán Ó Rudaí <sean@example.com>".to_string(), "🦆 Everywhere <ducks@example.com>".parse().unwrap(),
) ); "Иванов Иван Иванович <ivanov@example.com>".parse().unwrap(),
"Jānis Bērziņš <janis@example.com>".parse().unwrap(),
"Seán Ó Rudaí <sean@example.com>".parse().unwrap(),
]));
headers.set(to);
assert_eq!( assert_eq!(
headers.to_string(), headers.to_string(),
concat!( concat!(
"To: =?utf-8?b?8J+MjQ==?= <world@example.com>, =?utf-8?b?8J+mhg==?= Everywhere\r\n", "To: =?utf-8?b?8J+MjQ==?= <world@example.com>, =?utf-8?b?8J+mhiBFdmVyeXdo?=\r\n",
" <ducks@example.com>, =?utf-8?b?0JjQstCw0L3QvtCyINCY0LLQsNC9INCY0LLQsNC9?=\r\n", " =?utf-8?b?ZXJl?= <ducks@example.com>, =?utf-8?b?0JjQstCw0L3QvtCyINCY0LI=?=\r\n",
" =?utf-8?b?0L7QstC40Yc=?= <ivanov@example.com>, J=?utf-8?b?xIFuaXMgQsST?=\r\n", " =?utf-8?b?0LDQvSDQmNCy0LDQvdC+0LLQuNGH?= <ivanov@example.com>,\r\n",
" =?utf-8?b?cnppxYbFoQ==?= <janis@example.com>, Se=?utf-8?b?w6FuIMOTIFJ1?=\r\n", " =?utf-8?b?SsSBbmlzIELEk3J6acWGxaE=?= <janis@example.com>, =?utf-8?b?U2U=?=\r\n",
" =?utf-8?b?ZGHDrQ==?= <sean@example.com>\r\n", " =?utf-8?b?w6FuIMOTIFJ1ZGHDrQ==?= <sean@example.com>\r\n",
)
);
}
#[test]
fn format_special_with_folding_raw() {
let mut headers = Headers::new();
headers.insert_raw(HeaderValue::new(
HeaderName::new_from_ascii_str("To"),
"🌍 <world@example.com>, 🦆 Everywhere <ducks@example.com>, Иванов Иван Иванович <ivanov@example.com>, Jānis Bērziņš <janis@example.com>, Seán Ó Rudaí <sean@example.com>".to_owned(),
));
// TODO: fix the fact that the encoder doesn't know that
// the space between the name and the address should be
// removed when wrapping.
assert_eq!(
headers.to_string(),
concat!(
"To: =?utf-8?b?8J+MjQ==?= <world@example.com>, =?utf-8?b?8J+mhg==?=\r\n",
" Everywhere <ducks@example.com>, =?utf-8?b?0JjQstCw0L3QvtCyINCY0LLQsNC9?=\r\n",
" =?utf-8?b?INCY0LLQsNC90L7QstC40Yc=?= <ivanov@example.com>,\r\n",
" =?utf-8?b?SsSBbmlzIELEk3J6acWGxaE=?= <janis@example.com>,\r\n",
" =?utf-8?b?U2XDoW4gw5MgUnVkYcOt?= <sean@example.com>\r\n",
) )
); );
} }
@@ -646,7 +633,7 @@ mod tests {
headers.insert_raw( headers.insert_raw(
HeaderValue::new( HeaderValue::new(
HeaderName::new_from_ascii_str("Subject"), HeaderName::new_from_ascii_str("Subject"),
"🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳".to_string(),) "🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳".to_owned(),)
); );
assert_eq!( assert_eq!(
@@ -667,7 +654,7 @@ mod tests {
let mut headers = Headers::new(); let mut headers = Headers::new();
headers.insert_raw(HeaderValue::new( headers.insert_raw(HeaderValue::new(
HeaderName::new_from_ascii_str("Subject"), HeaderName::new_from_ascii_str("Subject"),
"Hello! \r\n This is \" bad \0. 👋".to_string(), "Hello! \r\n This is \" bad \0. 👋".to_owned(),
)); ));
assert_eq!( assert_eq!(
@@ -682,35 +669,38 @@ mod tests {
headers.insert_raw( headers.insert_raw(
HeaderValue::new( HeaderValue::new(
HeaderName::new_from_ascii_str("Subject"), HeaderName::new_from_ascii_str("Subject"),
"Hello! This is lettre, and this IsAVeryLongLineDoYouKnowWhatsGoingToHappenIGuessWeAreGoingToFindOut. Ok I guess that's it!".to_string() "Hello! This is lettre, and this IsAVeryLongLineDoYouKnowWhatsGoingToHappenIGuessWeAreGoingToFindOut. Ok I guess that's it!".to_owned()
) )
); );
headers.insert_raw( headers.insert_raw(
HeaderValue::new( HeaderValue::new(
HeaderName::new_from_ascii_str("To"), HeaderName::new_from_ascii_str("To"),
"🌍 <world@example.com>, 🦆 Everywhere <ducks@example.com>, Иванов Иван Иванович <ivanov@example.com>, Jānis Bērziņš <janis@example.com>, Seán Ó Rudaí <sean@example.com>".to_string(), "🌍 <world@example.com>, 🦆 Everywhere <ducks@example.com>, Иванов Иван Иванович <ivanov@example.com>, Jānis Bērziņš <janis@example.com>, Seán Ó Rudaí <sean@example.com>".to_owned(),
) )
); );
headers.insert_raw(HeaderValue::new( headers.insert_raw(HeaderValue::new(
HeaderName::new_from_ascii_str("From"), HeaderName::new_from_ascii_str("From"),
"Someone <somewhere@example.com>".to_string(), "Someone <somewhere@example.com>".to_owned(),
)); ));
headers.insert_raw(HeaderValue::new( headers.insert_raw(HeaderValue::new(
HeaderName::new_from_ascii_str("Content-Transfer-Encoding"), HeaderName::new_from_ascii_str("Content-Transfer-Encoding"),
"quoted-printable".to_string(), "quoted-printable".to_owned(),
)); ));
// TODO: fix the fact that the encoder doesn't know that
// the space between the name and the address should be
// removed when wrapping.
assert_eq!( assert_eq!(
headers.to_string(), headers.to_string(),
concat!( concat!(
"Subject: Hello! This is lettre, and this\r\n", "Subject: Hello! This is lettre, and this\r\n",
" IsAVeryLongLineDoYouKnowWhatsGoingToHappenIGuessWeAreGoingToFindOut. Ok I\r\n", " IsAVeryLongLineDoYouKnowWhatsGoingToHappenIGuessWeAreGoingToFindOut. Ok I\r\n",
" guess that's it!\r\n", " guess that's it!\r\n",
"To: =?utf-8?b?8J+MjQ==?= <world@example.com>, =?utf-8?b?8J+mhg==?= Everywhere\r\n", "To: =?utf-8?b?8J+MjQ==?= <world@example.com>, =?utf-8?b?8J+mhg==?=\r\n",
" <ducks@example.com>, =?utf-8?b?0JjQstCw0L3QvtCyINCY0LLQsNC9INCY0LLQsNC9?=\r\n", " Everywhere <ducks@example.com>, =?utf-8?b?0JjQstCw0L3QvtCyINCY0LLQsNC9?=\r\n",
" =?utf-8?b?0L7QstC40Yc=?= <ivanov@example.com>, J=?utf-8?b?xIFuaXMgQsST?=\r\n", " =?utf-8?b?INCY0LLQsNC90L7QstC40Yc=?= <ivanov@example.com>,\r\n",
" =?utf-8?b?cnppxYbFoQ==?= <janis@example.com>, Se=?utf-8?b?w6FuIMOTIFJ1?=\r\n", " =?utf-8?b?SsSBbmlzIELEk3J6acWGxaE=?= <janis@example.com>,\r\n",
" =?utf-8?b?ZGHDrQ==?= <sean@example.com>\r\n", " =?utf-8?b?U2XDoW4gw5MgUnVkYcOt?= <sean@example.com>\r\n",
"From: Someone <somewhere@example.com>\r\n", "From: Someone <somewhere@example.com>\r\n",
"Content-Transfer-Encoding: quoted-printable\r\n", "Content-Transfer-Encoding: quoted-printable\r\n",
) )
@@ -722,14 +712,14 @@ mod tests {
let mut headers = Headers::new(); let mut headers = Headers::new();
headers.insert_raw(HeaderValue::new( headers.insert_raw(HeaderValue::new(
HeaderName::new_from_ascii_str("Subject"), HeaderName::new_from_ascii_str("Subject"),
"+仮名 :a;go; ;;;;;s;;;;;;;;;;;;;;;;fffeinmjggggggggg".to_string(), "+仮名 :a;go; ;;;;;s;;;;;;;;;;;;;;;;fffeinmjggggggggg".to_owned(),
)); ));
assert_eq!( assert_eq!(
headers.to_string(), headers.to_string(),
concat!( concat!(
"Subject: =?utf-8?b?77yL5Luu5ZCN?= :a;go;\r\n", "Subject: =?utf-8?b?77yL5Luu5ZCN?= :a;go; =?utf-8?b?Ozs7OztzOzs7Ozs7Ozs7?=\r\n",
" ;;;;;s;;;;;;;;;;;;;;;;fffeinmjggggggggg=?utf-8?b?772G44Gj?=\r\n" " =?utf-8?b?Ozs7Ozs7O2ZmZmVpbm1qZ2dnZ2dnZ2dn772G44Gj?=\r\n",
) )
); );
} }

View File

@@ -4,7 +4,7 @@ use crate::{
}; };
/// Message format version, defined in [RFC2045](https://tools.ietf.org/html/rfc2045#section-4) /// Message format version, defined in [RFC2045](https://tools.ietf.org/html/rfc2045#section-4)
#[derive(Debug, Copy, Clone, PartialEq)] #[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct MimeVersion { pub struct MimeVersion {
major: u8, major: u8,
minor: u8, minor: u8,
@@ -16,15 +16,18 @@ pub struct MimeVersion {
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 {
/// Build a new `MimeVersion` header
pub const fn new(major: u8, minor: u8) -> Self { pub const fn new(major: u8, minor: u8) -> Self {
MimeVersion { major, minor } MimeVersion { major, minor }
} }
/// Get the `major` value of this `MimeVersion` header.
#[inline] #[inline]
pub const fn major(self) -> u8 { pub const fn major(self) -> u8 {
self.major self.major
} }
/// Get the `minor` value of this `MimeVersion` header.
#[inline] #[inline]
pub const fn minor(self) -> u8 { pub const fn minor(self) -> u8 {
self.minor self.minor
@@ -88,14 +91,14 @@ mod test {
headers.insert_raw(HeaderValue::new( headers.insert_raw(HeaderValue::new(
HeaderName::new_from_ascii_str("MIME-Version"), HeaderName::new_from_ascii_str("MIME-Version"),
"1.0".to_string(), "1.0".to_owned(),
)); ));
assert_eq!(headers.get::<MimeVersion>(), Some(MIME_VERSION_1_0)); assert_eq!(headers.get::<MimeVersion>(), Some(MIME_VERSION_1_0));
headers.insert_raw(HeaderValue::new( headers.insert_raw(HeaderValue::new(
HeaderName::new_from_ascii_str("MIME-Version"), HeaderName::new_from_ascii_str("MIME-Version"),
"0.1".to_string(), "0.1".to_owned(),
)); ));
assert_eq!(headers.get::<MimeVersion>(), Some(MimeVersion::new(0, 1))); assert_eq!(headers.get::<MimeVersion>(), Some(MimeVersion::new(0, 1)));

View File

@@ -4,7 +4,7 @@ use crate::BoxError;
macro_rules! text_header { macro_rules! text_header {
($(#[$attr:meta])* Header($type_name: ident, $header_name: expr )) => { ($(#[$attr:meta])* Header($type_name: ident, $header_name: expr )) => {
$(#[$attr])* $(#[$attr])*
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq, Eq)]
pub struct $type_name(String); pub struct $type_name(String);
impl Header for $type_name { impl Header for $type_name {
@@ -109,12 +109,23 @@ mod test {
); );
} }
#[test]
fn format_utf8_word() {
let mut headers = Headers::new();
headers.set(Subject("Administratör".into()));
assert_eq!(
headers.to_string(),
"Subject: =?utf-8?b?QWRtaW5pc3RyYXTDtnI=?=\r\n"
);
}
#[test] #[test]
fn parse_ascii() { fn parse_ascii() {
let mut headers = Headers::new(); let mut headers = Headers::new();
headers.insert_raw(HeaderValue::new( headers.insert_raw(HeaderValue::new(
HeaderName::new_from_ascii_str("Subject"), HeaderName::new_from_ascii_str("Subject"),
"Sample subject".to_string(), "Sample subject".to_owned(),
)); ));
assert_eq!( assert_eq!(

View File

@@ -13,7 +13,7 @@ impl Serialize for Mailbox {
where where
S: Serializer, S: Serializer,
{ {
serializer.serialize_str(&self.to_string()) serializer.collect_str(self)
} }
} }
@@ -111,7 +111,7 @@ impl Serialize for Mailboxes {
where where
S: Serializer, S: Serializer,
{ {
serializer.serialize_str(&self.to_string()) serializer.collect_str(self)
} }
} }

View File

@@ -70,7 +70,7 @@ impl Mailbox {
pub(crate) fn encode(&self, w: &mut EmailWriter<'_>) -> FmtResult { pub(crate) fn encode(&self, w: &mut EmailWriter<'_>) -> FmtResult {
if let Some(name) = &self.name { if let Some(name) = &self.name {
email_encoding::headers::quoted_string::encode(name, w)?; email_encoding::headers::quoted_string::encode(name, w)?;
w.space(); w.optional_breakpoint();
w.write_char('<')?; w.write_char('<')?;
} }
@@ -145,6 +145,12 @@ impl FromStr for Mailbox {
} }
} }
impl From<Address> for Mailbox {
fn from(value: Address) -> Self {
Self::new(None, value)
}
}
/// Represents a sequence of [`Mailbox`] instances. /// Represents a sequence of [`Mailbox`] instances.
/// ///
/// This type contains a sequence of mailboxes (_Some Name \<user@domain.tld\>, Another Name \<other@domain.tld\>, withoutname@domain.tld, ..._). /// This type contains a sequence of mailboxes (_Some Name \<user@domain.tld\>, Another Name \<other@domain.tld\>, withoutname@domain.tld, ..._).
@@ -275,7 +281,7 @@ impl Mailboxes {
for mailbox in self.iter() { for mailbox in self.iter() {
if !mem::take(&mut first) { if !mem::take(&mut first) {
w.write_char(',')?; w.write_char(',')?;
w.space(); w.optional_breakpoint();
} }
mailbox.encode(w)?; mailbox.encode(w)?;
@@ -315,6 +321,18 @@ impl From<Mailboxes> for Vec<Mailbox> {
} }
} }
impl FromIterator<Mailbox> for Mailboxes {
fn from_iter<T: IntoIterator<Item = Mailbox>>(iter: T) -> Self {
Self(Vec::from_iter(iter))
}
}
impl Extend<Mailbox> for Mailboxes {
fn extend<T: IntoIterator<Item = Mailbox>>(&mut self, iter: T) {
self.0.extend(iter);
}
}
impl IntoIterator for Mailboxes { impl IntoIterator for Mailboxes {
type Item = Mailbox; type Item = Mailbox;
type IntoIter = ::std::vec::IntoIter<Mailbox>; type IntoIter = ::std::vec::IntoIter<Mailbox>;
@@ -324,14 +342,6 @@ impl IntoIterator for Mailboxes {
} }
} }
impl Extend<Mailbox> for Mailboxes {
fn extend<T: IntoIterator<Item = Mailbox>>(&mut self, iter: T) {
for elem in iter {
self.0.push(elem);
}
}
}
impl Display for Mailboxes { impl Display for Mailboxes {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult { fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
let mut iter = self.iter(); let mut iter = self.iter();
@@ -393,7 +403,7 @@ fn write_word(f: &mut Formatter<'_>, s: &str) -> FmtResult {
} else { } else {
// Quoted string: https://datatracker.ietf.org/doc/html/rfc2822#section-3.2.5 // Quoted string: https://datatracker.ietf.org/doc/html/rfc2822#section-3.2.5
f.write_char('"')?; f.write_char('"')?;
for &c in s.as_bytes() { for c in s.chars() {
write_quoted_string_char(f, c)?; write_quoted_string_char(f, c)?;
} }
f.write_char('"')?; f.write_char('"')?;
@@ -437,34 +447,37 @@ fn is_valid_atom_char(c: u8) -> bool {
} }
// https://datatracker.ietf.org/doc/html/rfc2822#section-3.2.5 // https://datatracker.ietf.org/doc/html/rfc2822#section-3.2.5
fn write_quoted_string_char(f: &mut Formatter<'_>, c: u8) -> FmtResult { fn write_quoted_string_char(f: &mut Formatter<'_>, c: char) -> FmtResult {
match c { match c {
// NO-WS-CTL: https://datatracker.ietf.org/doc/html/rfc2822#section-3.2.1 // Can not be encoded.
1..=8 | 11 | 12 | 14..=31 | 127 | '\n' | '\r' => Err(std::fmt::Error),
// Note, not qcontent but can be put before or after any qcontent. // Note, not qcontent but can be put before or after any qcontent.
b'\t' | '\t' | ' ' => f.write_char(c),
b' ' |
// The rest of the US-ASCII except \ and " c if match c as u32 {
33 | // NO-WS-CTL: https://datatracker.ietf.org/doc/html/rfc2822#section-3.2.1
35..=91 | 1..=8 | 11 | 12 | 14..=31 | 127 |
93..=126 |
// Non-ascii characters will be escaped separately later. // The rest of the US-ASCII except \ and "
128..=255 33 |
35..=91 |
93..=126 |
=> f.write_char(c.into()), // Non-ascii characters will be escaped separately later.
128.. => true,
_ => false,
} =>
{
f.write_char(c)
}
// Can not be encoded. _ => {
b'\n' | b'\r' => Err(std::fmt::Error), // quoted-pair https://datatracker.ietf.org/doc/html/rfc2822#section-3.2.2
f.write_char('\\')?;
c => { f.write_char(c)
// quoted-pair https://datatracker.ietf.org/doc/html/rfc2822#section-3.2.2 }
f.write_char('\\')?; }
f.write_char(c.into())
}
}
} }
#[cfg(test)] #[cfg(test)]
@@ -511,6 +524,34 @@ mod test {
); );
} }
#[test]
fn mailbox_format_address_with_comma_and_non_ascii() {
assert_eq!(
format!(
"{}",
Mailbox::new(
Some("Laşt, First".into()),
"kayo@example.com".parse().unwrap()
)
),
r#""Laşt, First" <kayo@example.com>"#
);
}
#[test]
fn mailbox_format_address_with_comma_and_quoted_non_ascii() {
assert_eq!(
format!(
"{}",
Mailbox::new(
Some(r#"Laşt, "First""#.into()),
"kayo@example.com".parse().unwrap()
)
),
r#""Laşt, \"First\"" <kayo@example.com>"#
);
}
#[test] #[test]
fn mailbox_format_address_with_color() { fn mailbox_format_address_with_color() {
assert_eq!( assert_eq!(
@@ -585,7 +626,7 @@ mod test {
#[test] #[test]
fn parse_address_from_tuple() { fn parse_address_from_tuple() {
assert_eq!( assert_eq!(
("K.".to_string(), "kayo@example.com".to_string()).try_into(), ("K.".to_owned(), "kayo@example.com".to_owned()).try_into(),
Ok(Mailbox::new( Ok(Mailbox::new(
Some("K.".into()), Some("K.".into()),
"kayo@example.com".parse().unwrap() "kayo@example.com".parse().unwrap()

View File

@@ -190,9 +190,9 @@ impl MultiPartKind {
}, },
boundary, boundary,
match self { match self {
Self::Encrypted { protocol } => format!("; protocol=\"{}\"", protocol), Self::Encrypted { protocol } => format!("; protocol=\"{protocol}\""),
Self::Signed { protocol, micalg } => Self::Signed { protocol, micalg } =>
format!("; protocol=\"{}\"; micalg=\"{}\"", protocol, micalg), format!("; protocol=\"{protocol}\"; micalg=\"{micalg}\""),
_ => String::new(), _ => String::new(),
} }
) )

View File

@@ -14,7 +14,7 @@
//! The easiest way of creating a message, which uses a plain text body. //! The easiest way of creating a message, which uses a plain text body.
//! //!
//! ```rust //! ```rust
//! use lettre::message::Message; //! use lettre::message::{header::ContentType, Message};
//! //!
//! # use std::error::Error; //! # use std::error::Error;
//! # fn main() -> Result<(), Box<dyn Error>> { //! # fn main() -> Result<(), Box<dyn Error>> {
@@ -23,6 +23,7 @@
//! .reply_to("Yuin <yuin@domain.tld>".parse()?) //! .reply_to("Yuin <yuin@domain.tld>".parse()?)
//! .to("Hei <hei@domain.tld>".parse()?) //! .to("Hei <hei@domain.tld>".parse()?)
//! .subject("Happy new year") //! .subject("Happy new year")
//! .header(ContentType::TEXT_PLAIN)
//! .body(String::from("Be happy!"))?; //! .body(String::from("Be happy!"))?;
//! # Ok(()) //! # Ok(())
//! # } //! # }
@@ -38,6 +39,7 @@
//! To: Hei <hei@domain.tld> //! To: Hei <hei@domain.tld>
//! Subject: Happy new year //! Subject: Happy new year
//! Date: Sat, 12 Dec 2020 16:33:19 GMT //! Date: Sat, 12 Dec 2020 16:33:19 GMT
//! Content-Type: text/plain; charset=utf-8
//! Content-Transfer-Encoding: 7bit //! Content-Transfer-Encoding: 7bit
//! //!
//! Be happy! //! Be happy!
@@ -232,6 +234,7 @@ trait EmailFormat {
pub struct MessageBuilder { pub struct MessageBuilder {
headers: Headers, headers: Headers,
envelope: Option<Envelope>, envelope: Option<Envelope>,
drop_bcc: bool,
} }
impl MessageBuilder { impl MessageBuilder {
@@ -240,24 +243,26 @@ impl MessageBuilder {
Self { Self {
headers: Headers::new(), headers: Headers::new(),
envelope: None, envelope: None,
drop_bcc: true,
} }
} }
/// Set custom header to message /// Set or add mailbox to `From` header
pub fn header<H: Header>(mut self, header: H) -> Self { ///
self.headers.set(header); /// Defined in [RFC5322](https://tools.ietf.org/html/rfc5322#section-3.6.2).
self ///
/// Shortcut for `self.mailbox(header::From(mbox))`.
pub fn from(self, mbox: Mailbox) -> Self {
self.mailbox(header::From::from(Mailboxes::from(mbox)))
} }
/// Add mailbox to header /// Set `Sender` header. Should be used when providing several `From` mailboxes.
pub fn mailbox<H: Header + MailboxesHeader>(self, header: H) -> Self { ///
match self.headers.get::<H>() { /// Defined in [RFC5322](https://tools.ietf.org/html/rfc5322#section-3.6.2).
Some(mut header_) => { ///
header_.join_mailboxes(header); /// Shortcut for `self.header(header::Sender(mbox))`.
self.header(header_) pub fn sender(self, mbox: Mailbox) -> Self {
} self.header(header::Sender::from(mbox))
None => self.header(header),
}
} }
/// Add `Date` header to message /// Add `Date` header to message
@@ -275,41 +280,6 @@ impl MessageBuilder {
self.date(SystemTime::now()) self.date(SystemTime::now())
} }
/// Set `Subject` header to message
///
/// Shortcut for `self.header(header::Subject(subject.into()))`.
pub fn subject<S: Into<String>>(self, subject: S) -> Self {
let s: String = subject.into();
self.header(header::Subject::from(s))
}
/// Set `MIME-Version` header to 1.0
///
/// Shortcut for `self.header(header::MIME_VERSION_1_0)`.
///
/// Not exposed as it is set by body methods
fn mime_1_0(self) -> Self {
self.header(header::MIME_VERSION_1_0)
}
/// Set `Sender` header. Should be used when providing several `From` mailboxes.
///
/// Defined in [RFC5322](https://tools.ietf.org/html/rfc5322#section-3.6.2).
///
/// Shortcut for `self.header(header::Sender(mbox))`.
pub fn sender(self, mbox: Mailbox) -> Self {
self.header(header::Sender::from(mbox))
}
/// Set or add mailbox to `From` header
///
/// Defined in [RFC5322](https://tools.ietf.org/html/rfc5322#section-3.6.2).
///
/// Shortcut for `self.mailbox(header::From(mbox))`.
pub fn from(self, mbox: Mailbox) -> Self {
self.mailbox(header::From::from(Mailboxes::from(mbox)))
}
/// Set or add mailbox to `ReplyTo` header /// Set or add mailbox to `ReplyTo` header
/// ///
/// Defined in [RFC5322](https://tools.ietf.org/html/rfc5322#section-3.6.2). /// Defined in [RFC5322](https://tools.ietf.org/html/rfc5322#section-3.6.2).
@@ -352,6 +322,14 @@ impl MessageBuilder {
self.header(header::References::from(id)) self.header(header::References::from(id))
} }
/// Set `Subject` header to message
///
/// Shortcut for `self.header(header::Subject(subject.into()))`.
pub fn subject<S: Into<String>>(self, subject: S) -> Self {
let s: String = subject.into();
self.header(header::Subject::from(s))
}
/// Set [Message-ID /// Set [Message-ID
/// header](https://tools.ietf.org/html/rfc5322#section-3.6.4) /// header](https://tools.ietf.org/html/rfc5322#section-3.6.4)
/// ///
@@ -367,9 +345,9 @@ impl MessageBuilder {
let hostname = hostname::get() let hostname = hostname::get()
.map_err(|_| ()) .map_err(|_| ())
.and_then(|s| s.into_string().map_err(|_| ())) .and_then(|s| s.into_string().map_err(|_| ()))
.unwrap_or_else(|_| DEFAULT_MESSAGE_ID_DOMAIN.to_string()); .unwrap_or_else(|_| DEFAULT_MESSAGE_ID_DOMAIN.to_owned());
#[cfg(not(feature = "hostname"))] #[cfg(not(feature = "hostname"))]
let hostname = DEFAULT_MESSAGE_ID_DOMAIN.to_string(); let hostname = DEFAULT_MESSAGE_ID_DOMAIN.to_owned();
self.header(header::MessageId::from( self.header(header::MessageId::from(
// https://tools.ietf.org/html/rfc5322#section-3.6.4 // https://tools.ietf.org/html/rfc5322#section-3.6.4
@@ -380,17 +358,48 @@ impl MessageBuilder {
} }
/// Set [User-Agent /// Set [User-Agent
/// header](https://tools.ietf.org/html/draft-melnikov-email-user-agent-004) /// header](https://tools.ietf.org/html/draft-melnikov-email-user-agent-00)
pub fn user_agent(self, id: String) -> Self { pub fn user_agent(self, id: String) -> Self {
self.header(header::UserAgent::from(id)) self.header(header::UserAgent::from(id))
} }
/// Set custom header to message
pub fn header<H: Header>(mut self, header: H) -> Self {
self.headers.set(header);
self
}
/// Add mailbox to header
pub fn mailbox<H: Header + MailboxesHeader>(self, header: H) -> Self {
match self.headers.get::<H>() {
Some(mut header_) => {
header_.join_mailboxes(header);
self.header(header_)
}
None => self.header(header),
}
}
/// Force specific envelope (by default it is derived from headers) /// Force specific envelope (by default it is derived from headers)
pub fn envelope(mut self, envelope: Envelope) -> Self { pub fn envelope(mut self, envelope: Envelope) -> Self {
self.envelope = Some(envelope); self.envelope = Some(envelope);
self self
} }
/// Keep the `Bcc` header
///
/// By default the `Bcc` header is removed from the email after
/// using it to generate the message envelope. In some cases though,
/// like when saving the email as an `.eml`, or sending through
/// some transports (like the Gmail API) that don't take a separate
/// envelope value, it becomes necessary to keep the `Bcc` header.
///
/// Calling this method overrides the default behaviour.
pub fn keep_bcc(mut self) -> Self {
self.drop_bcc = false;
self
}
// TODO: High-level methods for attachments and embedded files // TODO: High-level methods for attachments and embedded files
/// Create message from body /// Create message from body
@@ -423,8 +432,10 @@ impl MessageBuilder {
None => Envelope::try_from(&res.headers)?, None => Envelope::try_from(&res.headers)?,
}; };
// Remove `Bcc` headers now the envelope is set if res.drop_bcc {
res.headers.remove::<header::Bcc>(); // Remove `Bcc` headers now the envelope is set
res.headers.remove::<header::Bcc>();
}
Ok(Message { Ok(Message {
headers: res.headers, headers: res.headers,
@@ -455,6 +466,15 @@ impl MessageBuilder {
pub fn singlepart(self, part: SinglePart) -> Result<Message, EmailError> { pub fn singlepart(self, part: SinglePart) -> Result<Message, EmailError> {
self.mime_1_0().build(MessageBody::Mime(Part::Single(part))) self.mime_1_0().build(MessageBody::Mime(Part::Single(part)))
} }
/// Set `MIME-Version` header to 1.0
///
/// Shortcut for `self.header(header::MIME_VERSION_1_0)`.
///
/// Not exposed as it is set by body methods
fn mime_1_0(self) -> Self {
self.header(header::MIME_VERSION_1_0)
}
} }
/// Email message which can be formatted /// Email message which can be formatted
@@ -521,7 +541,7 @@ impl Message {
/// .reply_to("Bob <bob@example.org>".parse().unwrap()) /// .reply_to("Bob <bob@example.org>".parse().unwrap())
/// .to("Carla <carla@example.net>".parse().unwrap()) /// .to("Carla <carla@example.net>".parse().unwrap())
/// .subject("Hello") /// .subject("Hello")
/// .body("Hi there, it's a test email, with utf-8 chars ë!\n\n\n".to_string()) /// .body("Hi there, it's a test email, with utf-8 chars ë!\n\n\n".to_owned())
/// .unwrap(); /// .unwrap();
/// let key = "-----BEGIN RSA PRIVATE KEY----- /// let key = "-----BEGIN RSA PRIVATE KEY-----
/// MIIEowIBAAKCAQEAt2gawjoybf0mAz0mSX0cq1ah5F9cPazZdCwLnFBhRufxaZB8 /// MIIEowIBAAKCAQEAt2gawjoybf0mAz0mSX0cq1ah5F9cPazZdCwLnFBhRufxaZB8
@@ -552,8 +572,8 @@ impl Message {
/// -----END RSA PRIVATE KEY-----"; /// -----END RSA PRIVATE KEY-----";
/// let signing_key = DkimSigningKey::new(key, DkimSigningAlgorithm::Rsa).unwrap(); /// let signing_key = DkimSigningKey::new(key, DkimSigningAlgorithm::Rsa).unwrap();
/// message.sign(&DkimConfig::default_config( /// message.sign(&DkimConfig::default_config(
/// "dkimtest".to_string(), /// "dkimtest".to_owned(),
/// "example.org".to_string(), /// "example.org".to_owned(),
/// signing_key, /// signing_key,
/// )); /// ));
/// println!( /// println!(
@@ -628,7 +648,7 @@ mod test {
} }
#[test] #[test]
fn email_message() { fn email_message_no_bcc() {
// Tue, 15 Nov 1994 08:12:31 GMT // Tue, 15 Nov 1994 08:12:31 GMT
let date = SystemTime::UNIX_EPOCH + Duration::from_secs(784887151); let date = SystemTime::UNIX_EPOCH + Duration::from_secs(784887151);
@@ -655,7 +675,45 @@ mod test {
"Date: Tue, 15 Nov 1994 08:12:31 +0000\r\n", "Date: Tue, 15 Nov 1994 08:12:31 +0000\r\n",
"From: =?utf-8?b?0JrQsNC4?= <kayo@example.com>\r\n", "From: =?utf-8?b?0JrQsNC4?= <kayo@example.com>\r\n",
"To: \"Pony O.P.\" <pony@domain.tld>\r\n", "To: \"Pony O.P.\" <pony@domain.tld>\r\n",
"Subject: =?utf-8?b?0Y/So9CwINC10Lsg0LHQtdC705nQvQ==?=!\r\n", "Subject: =?utf-8?b?0Y/So9CwINC10Lsg0LHQtdC705nQvSE=?=\r\n",
"Content-Transfer-Encoding: 7bit\r\n",
"\r\n",
"Happy new year!"
)
);
}
#[test]
fn email_message_keep_bcc() {
// Tue, 15 Nov 1994 08:12:31 GMT
let date = SystemTime::UNIX_EPOCH + Duration::from_secs(784887151);
let email = Message::builder()
.date(date)
.bcc("hidden@example.com".parse().unwrap())
.keep_bcc()
.header(header::From(
vec![Mailbox::new(
Some("Каи".into()),
"kayo@example.com".parse().unwrap(),
)]
.into(),
))
.header(header::To(
vec!["Pony O.P. <pony@domain.tld>".parse().unwrap()].into(),
))
.header(header::Subject::from(String::from("яңа ел белән!")))
.body(String::from("Happy new year!"))
.unwrap();
assert_eq!(
String::from_utf8(email.formatted()).unwrap(),
concat!(
"Date: Tue, 15 Nov 1994 08:12:31 +0000\r\n",
"Bcc: hidden@example.com\r\n",
"From: =?utf-8?b?0JrQsNC4?= <kayo@example.com>\r\n",
"To: \"Pony O.P.\" <pony@domain.tld>\r\n",
"Subject: =?utf-8?b?0Y/So9CwINC10Lsg0LHQtdC705nQvSE=?=\r\n",
"Content-Transfer-Encoding: 7bit\r\n", "Content-Transfer-Encoding: 7bit\r\n",
"\r\n", "\r\n",
"Happy new year!" "Happy new year!"

View File

@@ -71,7 +71,7 @@ impl fmt::Display for Error {
}; };
if let Some(ref e) = self.inner.source { if let Some(ref e) = self.inner.source {
write!(f, ": {}", e)?; write!(f, ": {e}")?;
} }
Ok(()) Ok(())

View File

@@ -208,18 +208,18 @@ impl FileTransport {
pub fn read(&self, email_id: &str) -> Result<(Envelope, Vec<u8>), Error> { pub fn read(&self, email_id: &str) -> Result<(Envelope, Vec<u8>), Error> {
use std::fs; use std::fs;
let eml_file = self.path.join(format!("{}.eml", email_id)); let eml_file = self.path.join(format!("{email_id}.eml"));
let eml = fs::read(eml_file).map_err(error::io)?; let eml = fs::read(eml_file).map_err(error::io)?;
let json_file = self.path.join(format!("{}.json", email_id)); let json_file = self.path.join(format!("{email_id}.json"));
let json = fs::read(&json_file).map_err(error::io)?; let json = fs::read(json_file).map_err(error::io)?;
let envelope = serde_json::from_slice(&json).map_err(error::envelope)?; let envelope = serde_json::from_slice(&json).map_err(error::envelope)?;
Ok((envelope, eml)) Ok((envelope, eml))
} }
fn path(&self, email_id: &Uuid, extension: &str) -> PathBuf { fn path(&self, email_id: &Uuid, extension: &str) -> PathBuf {
self.path.join(format!("{}.{}", email_id, extension)) self.path.join(format!("{email_id}.{extension}"))
} }
} }
@@ -255,10 +255,10 @@ where
/// Reads the envelope and the raw message content. /// Reads the envelope and the raw message content.
#[cfg(feature = "file-transport-envelope")] #[cfg(feature = "file-transport-envelope")]
pub async fn read(&self, email_id: &str) -> Result<(Envelope, Vec<u8>), Error> { pub async fn read(&self, email_id: &str) -> Result<(Envelope, Vec<u8>), Error> {
let eml_file = self.inner.path.join(format!("{}.eml", email_id)); let eml_file = self.inner.path.join(format!("{email_id}.eml"));
let eml = E::fs_read(&eml_file).await.map_err(error::io)?; let eml = E::fs_read(&eml_file).await.map_err(error::io)?;
let json_file = self.inner.path.join(format!("{}.json", email_id)); let json_file = self.inner.path.join(format!("{email_id}.json"));
let json = E::fs_read(&json_file).await.map_err(error::io)?; let json = E::fs_read(&json_file).await.map_err(error::io)?;
let envelope = serde_json::from_slice(&json).map_err(error::envelope)?; let envelope = serde_json::from_slice(&json).map_err(error::envelope)?;
@@ -276,6 +276,8 @@ impl Transport for FileTransport {
let email_id = Uuid::new_v4(); let email_id = Uuid::new_v4();
let file = self.path(&email_id, "eml"); let file = self.path(&email_id, "eml");
#[cfg(feature = "tracing")]
tracing::debug!(?file, "writing email to");
fs::write(file, email).map_err(error::io)?; fs::write(file, email).map_err(error::io)?;
#[cfg(feature = "file-transport-envelope")] #[cfg(feature = "file-transport-envelope")]
@@ -306,6 +308,8 @@ where
let email_id = Uuid::new_v4(); let email_id = Uuid::new_v4();
let file = self.inner.path(&email_id, "eml"); let file = self.inner.path(&email_id, "eml");
#[cfg(feature = "tracing")]
tracing::debug!(?file, "writing email to");
E::fs_write(&file, email).await.map_err(error::io)?; E::fs_write(&file, email).await.map_err(error::io)?;
#[cfg(feature = "file-transport-envelope")] #[cfg(feature = "file-transport-envelope")]

View File

@@ -65,7 +65,7 @@
//! .subject("Happy new year") //! .subject("Happy new year")
//! .body(String::from("Be happy!"))?; //! .body(String::from("Be happy!"))?;
//! //!
//! let creds = Credentials::new("smtp_username".to_string(), "smtp_password".to_string()); //! let creds = Credentials::new("smtp_username".to_owned(), "smtp_password".to_owned());
//! //!
//! // Open a remote connection to the SMTP relay server //! // Open a remote connection to the SMTP relay server
//! let mailer = SmtpTransport::relay("smtp.gmail.com")? //! let mailer = SmtpTransport::relay("smtp.gmail.com")?
@@ -127,6 +127,9 @@ pub trait Transport {
#[cfg(feature = "builder")] #[cfg(feature = "builder")]
#[cfg_attr(docsrs, doc(cfg(feature = "builder")))] #[cfg_attr(docsrs, doc(cfg(feature = "builder")))]
fn send(&self, message: &Message) -> Result<Self::Ok, Self::Error> { fn send(&self, message: &Message) -> Result<Self::Ok, Self::Error> {
#[cfg(feature = "tracing")]
tracing::trace!("starting to send an email");
let raw = message.formatted(); let raw = message.formatted();
self.send_raw(message.envelope(), &raw) self.send_raw(message.envelope(), &raw)
} }
@@ -149,6 +152,9 @@ pub trait AsyncTransport {
#[cfg_attr(docsrs, doc(cfg(feature = "builder")))] #[cfg_attr(docsrs, doc(cfg(feature = "builder")))]
// TODO take &Message // TODO take &Message
async fn send(&self, message: Message) -> Result<Self::Ok, Self::Error> { async fn send(&self, message: Message) -> Result<Self::Ok, Self::Error> {
#[cfg(feature = "tracing")]
tracing::trace!("starting to send an email");
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

View File

@@ -68,7 +68,7 @@ impl fmt::Display for Error {
}; };
if let Some(ref e) = self.inner.source { if let Some(ref e) = self.inner.source {
write!(f, ": {}", e)?; write!(f, ": {e}")?;
} }
Ok(()) Ok(())

View File

@@ -232,6 +232,9 @@ impl Transport for SendmailTransport {
type Error = Error; type Error = Error;
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 = "tracing")]
tracing::debug!(command = ?self.command, "sending email with");
// Spawn the sendmail command // Spawn the sendmail command
let mut process = self.command(envelope).spawn().map_err(error::client)?; let mut process = self.command(envelope).spawn().map_err(error::client)?;
@@ -261,6 +264,9 @@ impl AsyncTransport for AsyncSendmailTransport<AsyncStd1Executor> {
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> {
use async_std::io::prelude::WriteExt; use async_std::io::prelude::WriteExt;
#[cfg(feature = "tracing")]
tracing::debug!(command = ?self.inner.command, "sending email with");
let mut command = self.async_std_command(envelope); let mut command = self.async_std_command(envelope);
// Spawn the sendmail command // Spawn the sendmail command
@@ -293,6 +299,9 @@ impl AsyncTransport for AsyncSendmailTransport<Tokio1Executor> {
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> {
use tokio1_crate::io::AsyncWriteExt; use tokio1_crate::io::AsyncWriteExt;
#[cfg(feature = "tracing")]
tracing::debug!(command = ?self.inner.command, "sending email with");
let mut command = self.tokio1_command(envelope); let mut command = self.tokio1_command(envelope);
// Spawn the sendmail command // Spawn the sendmail command

View File

@@ -1,7 +1,8 @@
#[cfg(feature = "pool")]
use std::sync::Arc;
use std::{ use std::{
fmt::{self, Debug}, fmt::{self, Debug},
marker::PhantomData, marker::PhantomData,
sync::Arc,
time::Duration, time::Duration,
}; };
@@ -198,6 +199,9 @@ where
{ {
fn clone(&self) -> Self { fn clone(&self) -> Self {
Self { Self {
#[cfg(feature = "pool")]
inner: Arc::clone(&self.inner),
#[cfg(not(feature = "pool"))]
inner: self.inner.clone(), inner: self.inner.clone(),
} }
} }

View File

@@ -98,12 +98,12 @@ impl Mechanism {
let decoded_challenge = challenge let decoded_challenge = challenge
.ok_or_else(|| error::client("This mechanism does expect a challenge"))?; .ok_or_else(|| error::client("This mechanism does expect a challenge"))?;
if vec!["User Name", "Username:", "Username"].contains(&decoded_challenge) { if ["User Name", "Username:", "Username"].contains(&decoded_challenge) {
return Ok(credentials.authentication_identity.to_string()); return Ok(credentials.authentication_identity.clone());
} }
if vec!["Password", "Password:"].contains(&decoded_challenge) { if ["Password", "Password:"].contains(&decoded_challenge) {
return Ok(credentials.secret.to_string()); return Ok(credentials.secret.clone());
} }
Err(error::client("Unrecognized challenge")) Err(error::client("Unrecognized challenge"))
@@ -127,7 +127,7 @@ mod test {
fn test_plain() { fn test_plain() {
let mechanism = Mechanism::Plain; let mechanism = Mechanism::Plain;
let credentials = Credentials::new("username".to_string(), "password".to_string()); let credentials = Credentials::new("username".to_owned(), "password".to_owned());
assert_eq!( assert_eq!(
mechanism.response(&credentials, None).unwrap(), mechanism.response(&credentials, None).unwrap(),
@@ -140,7 +140,7 @@ mod test {
fn test_login() { fn test_login() {
let mechanism = Mechanism::Login; let mechanism = Mechanism::Login;
let credentials = Credentials::new("alice".to_string(), "wonderland".to_string()); let credentials = Credentials::new("alice".to_owned(), "wonderland".to_owned());
assert_eq!( assert_eq!(
mechanism.response(&credentials, Some("Username")).unwrap(), mechanism.response(&credentials, Some("Username")).unwrap(),
@@ -158,8 +158,8 @@ mod test {
let mechanism = Mechanism::Xoauth2; let mechanism = Mechanism::Xoauth2;
let credentials = Credentials::new( let credentials = Credentials::new(
"username".to_string(), "username".to_owned(),
"vF9dft4qmTc2Nvb3RlckBhdHRhdmlzdGEuY29tCg==".to_string(), "vF9dft4qmTc2Nvb3RlckBhdHRhdmlzdGEuY29tCg==".to_owned(),
); );
assert_eq!( assert_eq!(
@@ -172,7 +172,7 @@ mod test {
#[test] #[test]
fn test_from_user_pass_for_credentials() { fn test_from_user_pass_for_credentials() {
assert_eq!( assert_eq!(
Credentials::new("alice".to_string(), "wonderland".to_string()), Credentials::new("alice".to_owned(), "wonderland".to_owned()),
Credentials::from(("alice", "wonderland")) Credentials::from(("alice", "wonderland"))
); );
} }

View File

@@ -2,13 +2,15 @@ use std::{fmt::Display, net::IpAddr, time::Duration};
use futures_util::io::{AsyncBufReadExt, AsyncWriteExt, BufReader}; use futures_util::io::{AsyncBufReadExt, AsyncWriteExt, BufReader};
#[cfg(feature = "tokio1")]
use super::async_net::AsyncTokioStream;
#[cfg(feature = "tracing")] #[cfg(feature = "tracing")]
use super::escape_crlf; use super::escape_crlf;
use super::{AsyncNetworkStream, ClientCodec, TlsParameters}; use super::{AsyncNetworkStream, ClientCodec, TlsParameters};
use crate::{ use crate::{
transport::smtp::{ transport::smtp::{
authentication::{Credentials, Mechanism}, authentication::{Credentials, Mechanism},
commands::*, commands::{Auth, Data, Ehlo, Mail, Noop, Quit, Rcpt, Starttls},
error, error,
error::Error, error::Error,
extension::{ClientId, Extension, MailBodyParameter, MailParameter, ServerInfo}, extension::{ClientId, Extension, MailBodyParameter, MailParameter, ServerInfo},
@@ -41,10 +43,23 @@ pub struct AsyncSmtpConnection {
} }
impl AsyncSmtpConnection { impl AsyncSmtpConnection {
/// Get information about the server
pub fn server_info(&self) -> &ServerInfo { pub fn server_info(&self) -> &ServerInfo {
&self.server_info &self.server_info
} }
/// Connects with existing async stream
///
/// Sends EHLO and parses server information
#[cfg(feature = "tokio1")]
pub async fn connect_with_transport(
stream: Box<dyn AsyncTokioStream>,
hello_name: &ClientId,
) -> Result<AsyncSmtpConnection, Error> {
let stream = AsyncNetworkStream::use_existing_tokio1(stream);
Self::connect_impl(stream, hello_name).await
}
/// Connects to the configured server /// Connects to the configured server
/// ///
/// Sends EHLO and parses server information /// Sends EHLO and parses server information
@@ -193,6 +208,7 @@ impl AsyncSmtpConnection {
self.panic = true; self.panic = true;
let _ = self.command(Quit).await; let _ = self.command(Quit).await;
} }
let _ = self.stream.close().await;
} }
/// Sets the underlying stream /// Sets the underlying stream
@@ -301,7 +317,7 @@ impl AsyncSmtpConnection {
} else { } else {
Err(error::code( Err(error::code(
response.code(), response.code(),
response.first_line().map(|s| s.to_owned()), Some(response.message().collect()),
)) ))
} }
} }
@@ -319,7 +335,7 @@ impl AsyncSmtpConnection {
} }
/// The X509 certificate of the server (DER encoded) /// The X509 certificate of the server (DER encoded)
#[cfg(any(feature = "native-tls", feature = "rustls-tls"))] #[cfg(any(feature = "native-tls", feature = "rustls-tls", feature = "boring-tls"))]
pub fn peer_certificate(&self) -> Result<Vec<u8>, Error> { pub fn peer_certificate(&self) -> Result<Vec<u8>, Error> {
self.stream.get_ref().peer_certificate() self.stream.get_ref().peer_certificate()
} }

View File

@@ -1,5 +1,5 @@
use std::{ use std::{
io, mem, fmt, io, mem,
net::{IpAddr, SocketAddr}, net::{IpAddr, SocketAddr},
pin::Pin, pin::Pin,
task::{Context, Poll}, task::{Context, Poll},
@@ -16,8 +16,10 @@ use futures_io::{
}; };
#[cfg(feature = "async-std1-rustls-tls")] #[cfg(feature = "async-std1-rustls-tls")]
use futures_rustls::client::TlsStream as AsyncStd1RustlsTlsStream; use futures_rustls::client::TlsStream as AsyncStd1RustlsTlsStream;
#[cfg(feature = "tokio1-boring-tls")]
use tokio1_boring::SslStream as Tokio1SslStream;
#[cfg(feature = "tokio1")] #[cfg(feature = "tokio1")]
use tokio1_crate::io::{AsyncRead as _, AsyncWrite as _, ReadBuf as Tokio1ReadBuf}; use tokio1_crate::io::{AsyncRead, AsyncWrite, ReadBuf as Tokio1ReadBuf};
#[cfg(feature = "tokio1")] #[cfg(feature = "tokio1")]
use tokio1_crate::net::{ use tokio1_crate::net::{
TcpSocket as Tokio1TcpSocket, TcpStream as Tokio1TcpStream, TcpSocket as Tokio1TcpSocket, TcpStream as Tokio1TcpStream,
@@ -31,6 +33,7 @@ use tokio1_rustls::client::TlsStream as Tokio1RustlsTlsStream;
#[cfg(any( #[cfg(any(
feature = "tokio1-native-tls", feature = "tokio1-native-tls",
feature = "tokio1-rustls-tls", feature = "tokio1-rustls-tls",
feature = "tokio1-boring-tls",
feature = "async-std1-native-tls", feature = "async-std1-native-tls",
feature = "async-std1-rustls-tls" feature = "async-std1-rustls-tls"
))] ))]
@@ -41,25 +44,42 @@ use crate::transport::smtp::client::net::resolved_address_filter;
use crate::transport::smtp::{error, Error}; use crate::transport::smtp::{error, Error};
/// A network stream /// A network stream
#[derive(Debug)]
pub struct AsyncNetworkStream { pub struct AsyncNetworkStream {
inner: InnerAsyncNetworkStream, inner: InnerAsyncNetworkStream,
} }
#[cfg(feature = "tokio1")]
pub trait AsyncTokioStream: AsyncRead + AsyncWrite + Send + Sync + Unpin + fmt::Debug {
fn peer_addr(&self) -> io::Result<SocketAddr>;
}
#[cfg(feature = "tokio1")]
impl AsyncTokioStream for Tokio1TcpStream {
fn peer_addr(&self) -> io::Result<SocketAddr> {
self.peer_addr()
}
}
/// Represents the different types of underlying network streams /// Represents the different types of underlying network streams
// usually only one TLS backend at a time is going to be enabled, // usually only one TLS backend at a time is going to be enabled,
// so clippy::large_enum_variant doesn't make sense here // so clippy::large_enum_variant doesn't make sense here
#[allow(clippy::large_enum_variant)] #[allow(clippy::large_enum_variant)]
#[allow(dead_code)] #[allow(dead_code)]
#[derive(Debug)]
enum InnerAsyncNetworkStream { enum InnerAsyncNetworkStream {
/// Plain Tokio 1.x TCP stream /// Plain Tokio 1.x TCP stream
#[cfg(feature = "tokio1")] #[cfg(feature = "tokio1")]
Tokio1Tcp(Tokio1TcpStream), Tokio1Tcp(Box<dyn AsyncTokioStream>),
/// Encrypted Tokio 1.x TCP stream /// Encrypted Tokio 1.x TCP stream
#[cfg(feature = "tokio1-native-tls")] #[cfg(feature = "tokio1-native-tls")]
Tokio1NativeTls(Tokio1TlsStream<Tokio1TcpStream>), Tokio1NativeTls(Tokio1TlsStream<Box<dyn AsyncTokioStream>>),
/// Encrypted Tokio 1.x TCP stream /// Encrypted Tokio 1.x TCP stream
#[cfg(feature = "tokio1-rustls-tls")] #[cfg(feature = "tokio1-rustls-tls")]
Tokio1RustlsTls(Tokio1RustlsTlsStream<Tokio1TcpStream>), Tokio1RustlsTls(Tokio1RustlsTlsStream<Box<dyn AsyncTokioStream>>),
/// Encrypted Tokio 1.x TCP stream
#[cfg(feature = "tokio1-boring-tls")]
Tokio1BoringTls(Tokio1SslStream<Box<dyn AsyncTokioStream>>),
/// Plain Tokio 1.x TCP stream /// Plain Tokio 1.x TCP stream
#[cfg(feature = "async-std1")] #[cfg(feature = "async-std1")]
AsyncStd1Tcp(AsyncStd1TcpStream), AsyncStd1Tcp(AsyncStd1TcpStream),
@@ -93,6 +113,8 @@ impl AsyncNetworkStream {
} }
#[cfg(feature = "tokio1-rustls-tls")] #[cfg(feature = "tokio1-rustls-tls")]
InnerAsyncNetworkStream::Tokio1RustlsTls(ref s) => s.get_ref().0.peer_addr(), InnerAsyncNetworkStream::Tokio1RustlsTls(ref s) => s.get_ref().0.peer_addr(),
#[cfg(feature = "tokio1-boring-tls")]
InnerAsyncNetworkStream::Tokio1BoringTls(ref s) => s.get_ref().peer_addr(),
#[cfg(feature = "async-std1")] #[cfg(feature = "async-std1")]
InnerAsyncNetworkStream::AsyncStd1Tcp(ref s) => s.peer_addr(), InnerAsyncNetworkStream::AsyncStd1Tcp(ref s) => s.peer_addr(),
#[cfg(feature = "async-std1-native-tls")] #[cfg(feature = "async-std1-native-tls")]
@@ -109,6 +131,11 @@ impl AsyncNetworkStream {
} }
} }
#[cfg(feature = "tokio1")]
pub fn use_existing_tokio1(stream: Box<dyn AsyncTokioStream>) -> AsyncNetworkStream {
AsyncNetworkStream::new(InnerAsyncNetworkStream::Tokio1Tcp(stream))
}
#[cfg(feature = "tokio1")] #[cfg(feature = "tokio1")]
pub async fn connect_tokio1<T: Tokio1ToSocketAddrs>( pub async fn connect_tokio1<T: Tokio1ToSocketAddrs>(
server: T, server: T,
@@ -167,7 +194,8 @@ impl AsyncNetworkStream {
} }
let tcp_stream = try_connect(server, timeout, local_addr).await?; let tcp_stream = try_connect(server, timeout, local_addr).await?;
let mut stream = AsyncNetworkStream::new(InnerAsyncNetworkStream::Tokio1Tcp(tcp_stream)); let mut stream =
AsyncNetworkStream::new(InnerAsyncNetworkStream::Tokio1Tcp(Box::new(tcp_stream)));
if let Some(tls_parameters) = tls_parameters { if let Some(tls_parameters) = tls_parameters {
stream.upgrade_tls(tls_parameters).await?; stream.upgrade_tls(tls_parameters).await?;
} }
@@ -229,14 +257,22 @@ impl AsyncNetworkStream {
match &self.inner { match &self.inner {
#[cfg(all( #[cfg(all(
feature = "tokio1", feature = "tokio1",
not(any(feature = "tokio1-native-tls", feature = "tokio1-rustls-tls")) not(any(
feature = "tokio1-native-tls",
feature = "tokio1-rustls-tls",
feature = "tokio1-boring-tls"
))
))] ))]
InnerAsyncNetworkStream::Tokio1Tcp(_) => { InnerAsyncNetworkStream::Tokio1Tcp(_) => {
let _ = tls_parameters; let _ = tls_parameters;
panic!("Trying to upgrade an AsyncNetworkStream without having enabled either the tokio1-native-tls or the tokio1-rustls-tls feature"); panic!("Trying to upgrade an AsyncNetworkStream without having enabled either the tokio1-native-tls or the tokio1-rustls-tls feature");
} }
#[cfg(any(feature = "tokio1-native-tls", feature = "tokio1-rustls-tls"))] #[cfg(any(
feature = "tokio1-native-tls",
feature = "tokio1-rustls-tls",
feature = "tokio1-boring-tls"
))]
InnerAsyncNetworkStream::Tokio1Tcp(_) => { InnerAsyncNetworkStream::Tokio1Tcp(_) => {
// get owned TcpStream // get owned TcpStream
let tcp_stream = mem::replace(&mut self.inner, InnerAsyncNetworkStream::None); let tcp_stream = mem::replace(&mut self.inner, InnerAsyncNetworkStream::None);
@@ -278,12 +314,16 @@ impl AsyncNetworkStream {
} }
#[allow(unused_variables)] #[allow(unused_variables)]
#[cfg(any(feature = "tokio1-native-tls", feature = "tokio1-rustls-tls"))] #[cfg(any(
feature = "tokio1-native-tls",
feature = "tokio1-rustls-tls",
feature = "tokio1-boring-tls"
))]
async fn upgrade_tokio1_tls( async fn upgrade_tokio1_tls(
tcp_stream: Tokio1TcpStream, tcp_stream: Box<dyn AsyncTokioStream>,
tls_parameters: TlsParameters, tls_parameters: TlsParameters,
) -> Result<InnerAsyncNetworkStream, Error> { ) -> Result<InnerAsyncNetworkStream, Error> {
let domain = tls_parameters.domain().to_string(); let domain = tls_parameters.domain().to_owned();
match tls_parameters.connector { match tls_parameters.connector {
#[cfg(feature = "native-tls")] #[cfg(feature = "native-tls")]
@@ -324,11 +364,31 @@ impl AsyncNetworkStream {
Ok(InnerAsyncNetworkStream::Tokio1RustlsTls(stream)) Ok(InnerAsyncNetworkStream::Tokio1RustlsTls(stream))
}; };
} }
#[cfg(feature = "boring-tls")]
InnerTlsParameters::BoringTls(connector) => {
#[cfg(not(feature = "tokio1-boring-tls"))]
panic!("built without the tokio1-boring-tls feature");
#[cfg(feature = "tokio1-boring-tls")]
return {
let mut config = connector.configure().map_err(error::connection)?;
config.set_verify_hostname(tls_parameters.accept_invalid_hostnames);
let stream = tokio1_boring::connect(config, &domain, tcp_stream)
.await
.map_err(error::connection)?;
Ok(InnerAsyncNetworkStream::Tokio1BoringTls(stream))
};
}
} }
} }
#[allow(unused_variables)] #[allow(unused_variables)]
#[cfg(any(feature = "async-std1-native-tls", feature = "async-std1-rustls-tls"))] #[cfg(any(
feature = "async-std1-native-tls",
feature = "async-std1-rustls-tls",
feature = "async-std1-boring-tls"
))]
async fn upgrade_asyncstd1_tls( async fn upgrade_asyncstd1_tls(
tcp_stream: AsyncStd1TcpStream, tcp_stream: AsyncStd1TcpStream,
mut tls_parameters: TlsParameters, mut tls_parameters: TlsParameters,
@@ -377,6 +437,10 @@ impl AsyncNetworkStream {
Ok(InnerAsyncNetworkStream::AsyncStd1RustlsTls(stream)) Ok(InnerAsyncNetworkStream::AsyncStd1RustlsTls(stream))
}; };
} }
#[cfg(feature = "boring-tls")]
InnerTlsParameters::BoringTls(connector) => {
panic!("boring-tls isn't supported with async-std yet.");
}
} }
} }
@@ -388,6 +452,8 @@ impl AsyncNetworkStream {
InnerAsyncNetworkStream::Tokio1NativeTls(_) => true, InnerAsyncNetworkStream::Tokio1NativeTls(_) => true,
#[cfg(feature = "tokio1-rustls-tls")] #[cfg(feature = "tokio1-rustls-tls")]
InnerAsyncNetworkStream::Tokio1RustlsTls(_) => true, InnerAsyncNetworkStream::Tokio1RustlsTls(_) => true,
#[cfg(feature = "tokio1-boring-tls")]
InnerAsyncNetworkStream::Tokio1BoringTls(_) => true,
#[cfg(feature = "async-std1")] #[cfg(feature = "async-std1")]
InnerAsyncNetworkStream::AsyncStd1Tcp(_) => false, InnerAsyncNetworkStream::AsyncStd1Tcp(_) => false,
#[cfg(feature = "async-std1-native-tls")] #[cfg(feature = "async-std1-native-tls")]
@@ -422,6 +488,13 @@ impl AsyncNetworkStream {
.unwrap() .unwrap()
.clone() .clone()
.0), .0),
#[cfg(feature = "tokio1-boring-tls")]
InnerAsyncNetworkStream::Tokio1BoringTls(stream) => Ok(stream
.ssl()
.peer_certificate()
.unwrap()
.to_der()
.map_err(error::tls)?),
#[cfg(feature = "async-std1")] #[cfg(feature = "async-std1")]
InnerAsyncNetworkStream::AsyncStd1Tcp(_) => { InnerAsyncNetworkStream::AsyncStd1Tcp(_) => {
Err(error::client("Connection is not encrypted")) Err(error::client("Connection is not encrypted"))
@@ -477,6 +550,15 @@ impl FuturesAsyncRead for AsyncNetworkStream {
Poll::Pending => Poll::Pending, Poll::Pending => Poll::Pending,
} }
} }
#[cfg(feature = "tokio1-boring-tls")]
InnerAsyncNetworkStream::Tokio1BoringTls(ref mut s) => {
let mut b = Tokio1ReadBuf::new(buf);
match Pin::new(s).poll_read(cx, &mut b) {
Poll::Ready(Ok(())) => Poll::Ready(Ok(b.filled().len())),
Poll::Ready(Err(err)) => Poll::Ready(Err(err)),
Poll::Pending => Poll::Pending,
}
}
#[cfg(feature = "async-std1")] #[cfg(feature = "async-std1")]
InnerAsyncNetworkStream::AsyncStd1Tcp(ref mut s) => Pin::new(s).poll_read(cx, buf), InnerAsyncNetworkStream::AsyncStd1Tcp(ref mut s) => Pin::new(s).poll_read(cx, buf),
#[cfg(feature = "async-std1-native-tls")] #[cfg(feature = "async-std1-native-tls")]
@@ -508,6 +590,8 @@ impl FuturesAsyncWrite for AsyncNetworkStream {
InnerAsyncNetworkStream::Tokio1NativeTls(ref mut s) => Pin::new(s).poll_write(cx, buf), InnerAsyncNetworkStream::Tokio1NativeTls(ref mut s) => Pin::new(s).poll_write(cx, buf),
#[cfg(feature = "tokio1-rustls-tls")] #[cfg(feature = "tokio1-rustls-tls")]
InnerAsyncNetworkStream::Tokio1RustlsTls(ref mut s) => Pin::new(s).poll_write(cx, buf), InnerAsyncNetworkStream::Tokio1RustlsTls(ref mut s) => Pin::new(s).poll_write(cx, buf),
#[cfg(feature = "tokio1-boring-tls")]
InnerAsyncNetworkStream::Tokio1BoringTls(ref mut s) => Pin::new(s).poll_write(cx, buf),
#[cfg(feature = "async-std1")] #[cfg(feature = "async-std1")]
InnerAsyncNetworkStream::AsyncStd1Tcp(ref mut s) => Pin::new(s).poll_write(cx, buf), InnerAsyncNetworkStream::AsyncStd1Tcp(ref mut s) => Pin::new(s).poll_write(cx, buf),
#[cfg(feature = "async-std1-native-tls")] #[cfg(feature = "async-std1-native-tls")]
@@ -533,6 +617,8 @@ impl FuturesAsyncWrite for AsyncNetworkStream {
InnerAsyncNetworkStream::Tokio1NativeTls(ref mut s) => Pin::new(s).poll_flush(cx), InnerAsyncNetworkStream::Tokio1NativeTls(ref mut s) => Pin::new(s).poll_flush(cx),
#[cfg(feature = "tokio1-rustls-tls")] #[cfg(feature = "tokio1-rustls-tls")]
InnerAsyncNetworkStream::Tokio1RustlsTls(ref mut s) => Pin::new(s).poll_flush(cx), InnerAsyncNetworkStream::Tokio1RustlsTls(ref mut s) => Pin::new(s).poll_flush(cx),
#[cfg(feature = "tokio1-boring-tls")]
InnerAsyncNetworkStream::Tokio1BoringTls(ref mut s) => Pin::new(s).poll_flush(cx),
#[cfg(feature = "async-std1")] #[cfg(feature = "async-std1")]
InnerAsyncNetworkStream::AsyncStd1Tcp(ref mut s) => Pin::new(s).poll_flush(cx), InnerAsyncNetworkStream::AsyncStd1Tcp(ref mut s) => Pin::new(s).poll_flush(cx),
#[cfg(feature = "async-std1-native-tls")] #[cfg(feature = "async-std1-native-tls")]
@@ -554,6 +640,8 @@ impl FuturesAsyncWrite for AsyncNetworkStream {
InnerAsyncNetworkStream::Tokio1NativeTls(ref mut s) => Pin::new(s).poll_shutdown(cx), InnerAsyncNetworkStream::Tokio1NativeTls(ref mut s) => Pin::new(s).poll_shutdown(cx),
#[cfg(feature = "tokio1-rustls-tls")] #[cfg(feature = "tokio1-rustls-tls")]
InnerAsyncNetworkStream::Tokio1RustlsTls(ref mut s) => Pin::new(s).poll_shutdown(cx), InnerAsyncNetworkStream::Tokio1RustlsTls(ref mut s) => Pin::new(s).poll_shutdown(cx),
#[cfg(feature = "tokio1-boring-tls")]
InnerAsyncNetworkStream::Tokio1BoringTls(ref mut s) => Pin::new(s).poll_shutdown(cx),
#[cfg(feature = "async-std1")] #[cfg(feature = "async-std1")]
InnerAsyncNetworkStream::AsyncStd1Tcp(ref mut s) => Pin::new(s).poll_close(cx), InnerAsyncNetworkStream::AsyncStd1Tcp(ref mut s) => Pin::new(s).poll_close(cx),
#[cfg(feature = "async-std1-native-tls")] #[cfg(feature = "async-std1-native-tls")]

View File

@@ -12,7 +12,7 @@ use crate::{
address::Envelope, address::Envelope,
transport::smtp::{ transport::smtp::{
authentication::{Credentials, Mechanism}, authentication::{Credentials, Mechanism},
commands::*, commands::{Auth, Data, Ehlo, Mail, Noop, Quit, Rcpt, Starttls},
error, error,
error::Error, error::Error,
extension::{ClientId, Extension, MailBodyParameter, MailParameter, ServerInfo}, extension::{ClientId, Extension, MailBodyParameter, MailParameter, ServerInfo},
@@ -44,6 +44,7 @@ pub struct SmtpConnection {
} }
impl SmtpConnection { impl SmtpConnection {
/// Get information about the server
pub fn server_info(&self) -> &ServerInfo { pub fn server_info(&self) -> &ServerInfo {
&self.server_info &self.server_info
} }
@@ -142,7 +143,7 @@ impl SmtpConnection {
hello_name: &ClientId, hello_name: &ClientId,
) -> Result<(), Error> { ) -> Result<(), Error> {
if self.server_info.supports_feature(Extension::StartTls) { if self.server_info.supports_feature(Extension::StartTls) {
#[cfg(any(feature = "native-tls", feature = "rustls-tls"))] #[cfg(any(feature = "native-tls", feature = "rustls-tls", feature = "boring-tls"))]
{ {
try_smtp!(self.command(Starttls), self); try_smtp!(self.command(Starttls), self);
self.stream.get_mut().upgrade_tls(tls_parameters)?; self.stream.get_mut().upgrade_tls(tls_parameters)?;
@@ -152,7 +153,11 @@ impl SmtpConnection {
try_smtp!(self.ehlo(hello_name), self); try_smtp!(self.ehlo(hello_name), self);
Ok(()) Ok(())
} }
#[cfg(not(any(feature = "native-tls", feature = "rustls-tls")))] #[cfg(not(any(
feature = "native-tls",
feature = "rustls-tls",
feature = "boring-tls"
)))]
// This should never happen as `Tls` can only be created // This should never happen as `Tls` can only be created
// when a TLS library is enabled // when a TLS library is enabled
unreachable!("TLS support required but not supported"); unreachable!("TLS support required but not supported");
@@ -178,6 +183,7 @@ impl SmtpConnection {
self.panic = true; self.panic = true;
let _ = self.command(Quit); let _ = self.command(Quit);
} }
let _ = self.stream.get_mut().shutdown(std::net::Shutdown::Both);
} }
/// Sets the underlying stream /// Sets the underlying stream
@@ -237,11 +243,12 @@ impl SmtpConnection {
/// Sends the message content /// Sends the message content
pub fn message(&mut self, message: &[u8]) -> Result<Response, Error> { pub fn message(&mut self, message: &[u8]) -> Result<Response, Error> {
let mut out_buf: Vec<u8> = vec![];
let mut codec = ClientCodec::new(); let mut codec = ClientCodec::new();
let mut out_buf = Vec::with_capacity(message.len());
codec.encode(message, &mut out_buf); codec.encode(message, &mut out_buf);
self.write(out_buf.as_slice())?; self.write(out_buf.as_slice())?;
self.write(b"\r\n.\r\n")?; self.write(b"\r\n.\r\n")?;
self.read_response() self.read_response()
} }
@@ -278,7 +285,7 @@ impl SmtpConnection {
} else { } else {
Err(error::code( Err(error::code(
response.code(), response.code(),
response.first_line().map(|s| s.to_owned()), Some(response.message().collect()),
)) ))
}; };
} }
@@ -296,7 +303,7 @@ impl SmtpConnection {
} }
/// The X509 certificate of the server (DER encoded) /// The X509 certificate of the server (DER encoded)
#[cfg(any(feature = "native-tls", feature = "rustls-tls"))] #[cfg(any(feature = "native-tls", feature = "rustls-tls", feature = "boring-tls"))]
pub fn peer_certificate(&self) -> Result<Vec<u8>, Error> { pub fn peer_certificate(&self) -> Result<Vec<u8>, Error> {
self.stream.get_ref().peer_certificate() self.stream.get_ref().peer_certificate()
} }

View File

@@ -11,7 +11,7 @@
//! client::SmtpConnection, commands::*, extension::ClientId, SMTP_PORT, //! client::SmtpConnection, commands::*, extension::ClientId, SMTP_PORT,
//! }; //! };
//! //!
//! let hello = ClientId::Domain("my_hostname".to_string()); //! let hello = ClientId::Domain("my_hostname".to_owned());
//! let mut client = SmtpConnection::connect(&("localhost", SMTP_PORT), None, &hello, None, None)?; //! let mut client = SmtpConnection::connect(&("localhost", SMTP_PORT), None, &hello, None, None)?;
//! client.command(Mail::new(Some("user@example.com".parse()?), vec![]))?; //! client.command(Mail::new(Some("user@example.com".parse()?), vec![]))?;
//! client.command(Rcpt::new("user@example.org".parse()?, vec![]))?; //! client.command(Rcpt::new("user@example.org".parse()?, vec![]))?;
@@ -29,12 +29,16 @@ use std::fmt::Debug;
pub use self::async_connection::AsyncSmtpConnection; pub use self::async_connection::AsyncSmtpConnection;
#[cfg(any(feature = "tokio1", feature = "async-std1"))] #[cfg(any(feature = "tokio1", feature = "async-std1"))]
pub use self::async_net::AsyncNetworkStream; pub use self::async_net::AsyncNetworkStream;
#[cfg(feature = "tokio1")]
pub use self::async_net::AsyncTokioStream;
use self::net::NetworkStream; use self::net::NetworkStream;
#[cfg(any(feature = "native-tls", feature = "rustls-tls"))] #[cfg(any(feature = "native-tls", feature = "rustls-tls", feature = "boring-tls"))]
pub(super) use self::tls::InnerTlsParameters; pub(super) use self::tls::InnerTlsParameters;
#[cfg(any(feature = "native-tls", feature = "rustls-tls", feature = "boring-tls"))]
pub use self::tls::TlsVersion;
pub use self::{ pub use self::{
connection::SmtpConnection, connection::SmtpConnection,
tls::{Certificate, Tls, TlsParameters, TlsParametersBuilder}, tls::{Certificate, CertificateStore, Tls, TlsParameters, TlsParametersBuilder},
}; };
#[cfg(any(feature = "tokio1", feature = "async-std1"))] #[cfg(any(feature = "tokio1", feature = "async-std1"))]
@@ -46,60 +50,57 @@ mod net;
mod tls; mod tls;
/// The codec used for transparency /// The codec used for transparency
#[derive(Default, Clone, Copy, Debug)] #[derive(Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
struct ClientCodec { struct ClientCodec {
escape_count: u8, status: CodecStatus,
} }
impl ClientCodec { impl ClientCodec {
/// Creates a new client codec /// Creates a new client codec
pub fn new() -> Self { pub fn new() -> Self {
ClientCodec::default() Self {
status: CodecStatus::StartOfNewLine,
}
} }
/// Adds transparency /// Adds transparency
fn encode(&mut self, frame: &[u8], buf: &mut Vec<u8>) { fn encode(&mut self, frame: &[u8], buf: &mut Vec<u8>) {
match frame.len() { for &b in frame {
0 => { buf.push(b);
match self.escape_count { match (b, self.status) {
0 => buf.extend_from_slice(b"\r\n.\r\n"), (b'\r', _) => {
1 => buf.extend_from_slice(b"\n.\r\n"), self.status = CodecStatus::StartingNewLine;
2 => buf.extend_from_slice(b".\r\n"),
_ => unreachable!(),
} }
self.escape_count = 0; (b'\n', CodecStatus::StartingNewLine) => {
} self.status = CodecStatus::StartOfNewLine;
_ => {
let mut start = 0;
for (idx, byte) in frame.iter().enumerate() {
match self.escape_count {
0 => self.escape_count = if *byte == b'\r' { 1 } else { 0 },
1 => self.escape_count = if *byte == b'\n' { 2 } else { 0 },
2 => {
self.escape_count = if *byte == b'.' {
3
} else if *byte == b'\r' {
1
} else {
0
}
}
_ => unreachable!(),
}
if self.escape_count == 3 {
self.escape_count = 0;
buf.extend_from_slice(&frame[start..idx]);
buf.extend_from_slice(b".");
start = idx;
}
} }
buf.extend_from_slice(&frame[start..]); (_, CodecStatus::StartingNewLine) => {
self.status = CodecStatus::MiddleOfLine;
}
(b'.', CodecStatus::StartOfNewLine) => {
self.status = CodecStatus::MiddleOfLine;
buf.push(b'.');
}
(_, CodecStatus::StartOfNewLine) => {
self.status = CodecStatus::MiddleOfLine;
}
_ => {}
} }
} }
} }
} }
#[derive(Debug, Copy, Clone)]
#[allow(clippy::enum_variant_names)]
enum CodecStatus {
/// We are past the first character of the current line
MiddleOfLine,
/// We just read a `\r` character
StartingNewLine,
/// We are at the start of a new line
StartOfNewLine,
}
/// Returns the string replacing all the CRLF with "\<CRLF\>" /// Returns the string replacing all the CRLF with "\<CRLF\>"
/// Used for debug displays /// Used for debug displays
#[cfg(feature = "tracing")] #[cfg(feature = "tracing")]
@@ -113,9 +114,10 @@ mod test {
#[test] #[test]
fn test_codec() { fn test_codec() {
let mut buf = Vec::new();
let mut codec = ClientCodec::new(); let mut codec = ClientCodec::new();
let mut buf: Vec<u8> = vec![];
codec.encode(b".\r\n", &mut buf);
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"test\r\n\r\n", &mut buf);
codec.encode(b".\r\n", &mut buf); codec.encode(b".\r\n", &mut buf);
@@ -126,9 +128,13 @@ mod test {
codec.encode(b"test\n", &mut buf); codec.encode(b"test\n", &mut buf);
codec.encode(b".test\n", &mut buf); codec.encode(b".test\n", &mut buf);
codec.encode(b"test", &mut buf); codec.encode(b"test", &mut buf);
codec.encode(b"test", &mut buf);
codec.encode(b"test\r\n", &mut buf);
codec.encode(b".test\r\n", &mut buf);
codec.encode(b"test.\r\n", &mut buf);
assert_eq!( assert_eq!(
String::from_utf8(buf).unwrap(), String::from_utf8(buf).unwrap(),
"test\r\ntest\r\n\r\n..\r\n\r\ntestte\r\n..\r\nsttesttest.test\n.test\ntest" "..\r\ntest\r\ntest\r\n\r\n..\r\n\r\ntestte\r\n..\r\nsttesttest.test\n.test\ntesttesttest\r\n..test\r\ntest.\r\n"
); );
} }

View File

@@ -1,3 +1,5 @@
#[cfg(feature = "rustls-tls")]
use std::sync::Arc;
use std::{ use std::{
io::{self, Read, Write}, io::{self, Read, Write},
mem, mem,
@@ -5,13 +7,15 @@ use std::{
time::Duration, time::Duration,
}; };
#[cfg(feature = "boring-tls")]
use boring::ssl::SslStream;
#[cfg(feature = "native-tls")] #[cfg(feature = "native-tls")]
use native_tls::TlsStream; use native_tls::TlsStream;
#[cfg(feature = "rustls-tls")] #[cfg(feature = "rustls-tls")]
use rustls::{ClientConnection, ServerName, StreamOwned}; use rustls::{ClientConnection, ServerName, StreamOwned};
use socket2::{Domain, Protocol, Type}; use socket2::{Domain, Protocol, Type};
#[cfg(any(feature = "native-tls", feature = "rustls-tls"))] #[cfg(any(feature = "native-tls", feature = "rustls-tls", feature = "boring-tls"))]
use super::InnerTlsParameters; use super::InnerTlsParameters;
use super::TlsParameters; use super::TlsParameters;
use crate::transport::smtp::{error, Error}; use crate::transport::smtp::{error, Error};
@@ -34,6 +38,8 @@ enum InnerNetworkStream {
/// Encrypted TCP stream /// Encrypted TCP stream
#[cfg(feature = "rustls-tls")] #[cfg(feature = "rustls-tls")]
RustlsTls(StreamOwned<ClientConnection, TcpStream>), RustlsTls(StreamOwned<ClientConnection, TcpStream>),
#[cfg(feature = "boring-tls")]
BoringTls(SslStream<TcpStream>),
/// Can't be built /// Can't be built
None, None,
} }
@@ -55,6 +61,8 @@ impl NetworkStream {
InnerNetworkStream::NativeTls(ref s) => s.get_ref().peer_addr(), InnerNetworkStream::NativeTls(ref s) => s.get_ref().peer_addr(),
#[cfg(feature = "rustls-tls")] #[cfg(feature = "rustls-tls")]
InnerNetworkStream::RustlsTls(ref s) => s.get_ref().peer_addr(), InnerNetworkStream::RustlsTls(ref s) => s.get_ref().peer_addr(),
#[cfg(feature = "boring-tls")]
InnerNetworkStream::BoringTls(ref s) => s.get_ref().peer_addr(),
InnerNetworkStream::None => { InnerNetworkStream::None => {
debug_assert!(false, "InnerNetworkStream::None must never be built"); debug_assert!(false, "InnerNetworkStream::None must never be built");
Ok(SocketAddr::V4(SocketAddrV4::new( Ok(SocketAddr::V4(SocketAddrV4::new(
@@ -73,6 +81,8 @@ impl NetworkStream {
InnerNetworkStream::NativeTls(ref s) => s.get_ref().shutdown(how), InnerNetworkStream::NativeTls(ref s) => s.get_ref().shutdown(how),
#[cfg(feature = "rustls-tls")] #[cfg(feature = "rustls-tls")]
InnerNetworkStream::RustlsTls(ref s) => s.get_ref().shutdown(how), InnerNetworkStream::RustlsTls(ref s) => s.get_ref().shutdown(how),
#[cfg(feature = "boring-tls")]
InnerNetworkStream::BoringTls(ref s) => s.get_ref().shutdown(how),
InnerNetworkStream::None => { InnerNetworkStream::None => {
debug_assert!(false, "InnerNetworkStream::None must never be built"); debug_assert!(false, "InnerNetworkStream::None must never be built");
Ok(()) Ok(())
@@ -136,13 +146,17 @@ impl NetworkStream {
pub fn upgrade_tls(&mut self, tls_parameters: &TlsParameters) -> Result<(), Error> { pub fn upgrade_tls(&mut self, tls_parameters: &TlsParameters) -> Result<(), Error> {
match &self.inner { match &self.inner {
#[cfg(not(any(feature = "native-tls", feature = "rustls-tls")))] #[cfg(not(any(
feature = "native-tls",
feature = "rustls-tls",
feature = "boring-tls"
)))]
InnerNetworkStream::Tcp(_) => { InnerNetworkStream::Tcp(_) => {
let _ = tls_parameters; let _ = tls_parameters;
panic!("Trying to upgrade an NetworkStream without having enabled either the native-tls or the rustls-tls feature"); panic!("Trying to upgrade an NetworkStream without having enabled either the native-tls or the rustls-tls feature");
} }
#[cfg(any(feature = "native-tls", feature = "rustls-tls"))] #[cfg(any(feature = "native-tls", feature = "rustls-tls", feature = "boring-tls"))]
InnerNetworkStream::Tcp(_) => { InnerNetworkStream::Tcp(_) => {
// get owned TcpStream // get owned TcpStream
let tcp_stream = mem::replace(&mut self.inner, InnerNetworkStream::None); let tcp_stream = mem::replace(&mut self.inner, InnerNetworkStream::None);
@@ -158,7 +172,7 @@ impl NetworkStream {
} }
} }
#[cfg(any(feature = "native-tls", feature = "rustls-tls"))] #[cfg(any(feature = "native-tls", feature = "rustls-tls", feature = "boring-tls"))]
fn upgrade_tls_impl( fn upgrade_tls_impl(
tcp_stream: TcpStream, tcp_stream: TcpStream,
tls_parameters: &TlsParameters, tls_parameters: &TlsParameters,
@@ -175,11 +189,21 @@ impl NetworkStream {
InnerTlsParameters::RustlsTls(connector) => { InnerTlsParameters::RustlsTls(connector) => {
let domain = ServerName::try_from(tls_parameters.domain()) let domain = ServerName::try_from(tls_parameters.domain())
.map_err(|_| error::connection("domain isn't a valid DNS name"))?; .map_err(|_| error::connection("domain isn't a valid DNS name"))?;
let connection = let connection = ClientConnection::new(Arc::clone(connector), domain)
ClientConnection::new(connector.clone(), domain).map_err(error::connection)?; .map_err(error::connection)?;
let stream = StreamOwned::new(connection, tcp_stream); let stream = StreamOwned::new(connection, tcp_stream);
InnerNetworkStream::RustlsTls(stream) InnerNetworkStream::RustlsTls(stream)
} }
#[cfg(feature = "boring-tls")]
InnerTlsParameters::BoringTls(connector) => {
let stream = connector
.configure()
.map_err(error::connection)?
.verify_hostname(tls_parameters.accept_invalid_hostnames)
.connect(tls_parameters.domain(), tcp_stream)
.map_err(error::connection)?;
InnerNetworkStream::BoringTls(stream)
}
}) })
} }
@@ -190,6 +214,8 @@ impl NetworkStream {
InnerNetworkStream::NativeTls(_) => true, InnerNetworkStream::NativeTls(_) => true,
#[cfg(feature = "rustls-tls")] #[cfg(feature = "rustls-tls")]
InnerNetworkStream::RustlsTls(_) => true, InnerNetworkStream::RustlsTls(_) => true,
#[cfg(feature = "boring-tls")]
InnerNetworkStream::BoringTls(_) => true,
InnerNetworkStream::None => { InnerNetworkStream::None => {
debug_assert!(false, "InnerNetworkStream::None must never be built"); debug_assert!(false, "InnerNetworkStream::None must never be built");
false false
@@ -197,7 +223,7 @@ impl NetworkStream {
} }
} }
#[cfg(any(feature = "native-tls", feature = "rustls-tls"))] #[cfg(any(feature = "native-tls", feature = "rustls-tls", feature = "boring-tls"))]
pub fn peer_certificate(&self) -> Result<Vec<u8>, Error> { pub fn peer_certificate(&self) -> Result<Vec<u8>, Error> {
match &self.inner { match &self.inner {
InnerNetworkStream::Tcp(_) => Err(error::client("Connection is not encrypted")), InnerNetworkStream::Tcp(_) => Err(error::client("Connection is not encrypted")),
@@ -217,6 +243,13 @@ impl NetworkStream {
.unwrap() .unwrap()
.clone() .clone()
.0), .0),
#[cfg(feature = "boring-tls")]
InnerNetworkStream::BoringTls(stream) => Ok(stream
.ssl()
.peer_certificate()
.unwrap()
.to_der()
.map_err(error::tls)?),
InnerNetworkStream::None => panic!("InnerNetworkStream::None must never be built"), InnerNetworkStream::None => panic!("InnerNetworkStream::None must never be built"),
} }
} }
@@ -232,6 +265,10 @@ impl NetworkStream {
InnerNetworkStream::RustlsTls(ref mut stream) => { InnerNetworkStream::RustlsTls(ref mut stream) => {
stream.get_ref().set_read_timeout(duration) stream.get_ref().set_read_timeout(duration)
} }
#[cfg(feature = "boring-tls")]
InnerNetworkStream::BoringTls(ref mut stream) => {
stream.get_ref().set_read_timeout(duration)
}
InnerNetworkStream::None => { InnerNetworkStream::None => {
debug_assert!(false, "InnerNetworkStream::None must never be built"); debug_assert!(false, "InnerNetworkStream::None must never be built");
Ok(()) Ok(())
@@ -252,7 +289,10 @@ impl NetworkStream {
InnerNetworkStream::RustlsTls(ref mut stream) => { InnerNetworkStream::RustlsTls(ref mut stream) => {
stream.get_ref().set_write_timeout(duration) stream.get_ref().set_write_timeout(duration)
} }
#[cfg(feature = "boring-tls")]
InnerNetworkStream::BoringTls(ref mut stream) => {
stream.get_ref().set_write_timeout(duration)
}
InnerNetworkStream::None => { InnerNetworkStream::None => {
debug_assert!(false, "InnerNetworkStream::None must never be built"); debug_assert!(false, "InnerNetworkStream::None must never be built");
Ok(()) Ok(())
@@ -269,6 +309,8 @@ impl Read for NetworkStream {
InnerNetworkStream::NativeTls(ref mut s) => s.read(buf), InnerNetworkStream::NativeTls(ref mut s) => s.read(buf),
#[cfg(feature = "rustls-tls")] #[cfg(feature = "rustls-tls")]
InnerNetworkStream::RustlsTls(ref mut s) => s.read(buf), InnerNetworkStream::RustlsTls(ref mut s) => s.read(buf),
#[cfg(feature = "boring-tls")]
InnerNetworkStream::BoringTls(ref mut s) => s.read(buf),
InnerNetworkStream::None => { InnerNetworkStream::None => {
debug_assert!(false, "InnerNetworkStream::None must never be built"); debug_assert!(false, "InnerNetworkStream::None must never be built");
Ok(0) Ok(0)
@@ -285,6 +327,8 @@ impl Write for NetworkStream {
InnerNetworkStream::NativeTls(ref mut s) => s.write(buf), InnerNetworkStream::NativeTls(ref mut s) => s.write(buf),
#[cfg(feature = "rustls-tls")] #[cfg(feature = "rustls-tls")]
InnerNetworkStream::RustlsTls(ref mut s) => s.write(buf), InnerNetworkStream::RustlsTls(ref mut s) => s.write(buf),
#[cfg(feature = "boring-tls")]
InnerNetworkStream::BoringTls(ref mut s) => s.write(buf),
InnerNetworkStream::None => { InnerNetworkStream::None => {
debug_assert!(false, "InnerNetworkStream::None must never be built"); debug_assert!(false, "InnerNetworkStream::None must never be built");
Ok(0) Ok(0)
@@ -299,6 +343,8 @@ impl Write for NetworkStream {
InnerNetworkStream::NativeTls(ref mut s) => s.flush(), InnerNetworkStream::NativeTls(ref mut s) => s.flush(),
#[cfg(feature = "rustls-tls")] #[cfg(feature = "rustls-tls")]
InnerNetworkStream::RustlsTls(ref mut s) => s.flush(), InnerNetworkStream::RustlsTls(ref mut s) => s.flush(),
#[cfg(feature = "boring-tls")]
InnerNetworkStream::BoringTls(ref mut s) => s.flush(),
InnerNetworkStream::None => { InnerNetworkStream::None => {
debug_assert!(false, "InnerNetworkStream::None must never be built"); debug_assert!(false, "InnerNetworkStream::None must never be built");
Ok(()) Ok(())

View File

@@ -2,22 +2,57 @@ use std::fmt::{self, Debug};
#[cfg(feature = "rustls-tls")] #[cfg(feature = "rustls-tls")]
use std::{sync::Arc, time::SystemTime}; use std::{sync::Arc, time::SystemTime};
#[cfg(feature = "boring-tls")]
use boring::{
ssl::{SslConnector, SslVersion},
x509::store::X509StoreBuilder,
};
#[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::{ use rustls::{
client::{ServerCertVerified, ServerCertVerifier, WebPkiVerifier}, client::{ServerCertVerified, ServerCertVerifier, WebPkiVerifier},
ClientConfig, Error as TlsError, OwnedTrustAnchor, RootCertStore, ServerName, ClientConfig, Error as TlsError, RootCertStore, ServerName,
}; };
#[cfg(any(feature = "native-tls", feature = "rustls-tls"))] #[cfg(any(feature = "native-tls", feature = "rustls-tls", feature = "boring-tls"))]
use crate::transport::smtp::{error, Error}; use crate::transport::smtp::{error, Error};
/// Accepted protocols by default. /// TLS protocol versions.
/// This removes TLS 1.0 and 1.1 compared to tls-native defaults. #[derive(Debug, Copy, Clone)]
// This is also rustls' default behavior #[non_exhaustive]
#[cfg(feature = "native-tls")] #[cfg(any(feature = "native-tls", feature = "rustls-tls", feature = "boring-tls"))]
const DEFAULT_TLS_MIN_PROTOCOL: Protocol = Protocol::Tlsv12; pub enum TlsVersion {
/// TLS 1.0
///
/// Should only be used when trying to support legacy
/// SMTP servers that haven't updated to
/// at least TLS 1.2 yet.
///
/// Supported by `native-tls` and `boring-tls`.
Tlsv10,
/// TLS 1.1
///
/// Should only be used when trying to support legacy
/// SMTP servers that haven't updated to
/// at least TLS 1.2 yet.
///
/// Supported by `native-tls` and `boring-tls`.
Tlsv11,
/// TLS 1.2
///
/// A good option for most SMTP servers.
///
/// Supported by all TLS backends.
Tlsv12,
/// TLS 1.3
///
/// The most secure option, altough not supported by all SMTP servers.
///
/// Altough it is technically supported by all TLS backends,
/// trying to set it for `native-tls` will give a runtime error.
Tlsv13,
}
/// How to apply TLS to a client connection /// How to apply TLS to a client connection
#[derive(Clone)] #[derive(Clone)]
@@ -26,16 +61,25 @@ pub enum Tls {
/// Insecure connection only (for testing purposes) /// Insecure connection only (for testing purposes)
None, None,
/// Start with insecure connection and use `STARTTLS` when available /// Start with insecure connection and use `STARTTLS` when available
#[cfg(any(feature = "native-tls", feature = "rustls-tls"))] #[cfg(any(feature = "native-tls", feature = "rustls-tls", feature = "boring-tls"))]
#[cfg_attr(docsrs, doc(cfg(any(feature = "native-tls", feature = "rustls-tls"))))] #[cfg_attr(
docsrs,
doc(cfg(any(feature = "native-tls", feature = "rustls-tls", feature = "boring-tls")))
)]
Opportunistic(TlsParameters), Opportunistic(TlsParameters),
/// Start with insecure connection and require `STARTTLS` /// Start with insecure connection and require `STARTTLS`
#[cfg(any(feature = "native-tls", feature = "rustls-tls"))] #[cfg(any(feature = "native-tls", feature = "rustls-tls", feature = "boring-tls"))]
#[cfg_attr(docsrs, doc(cfg(any(feature = "native-tls", feature = "rustls-tls"))))] #[cfg_attr(
docsrs,
doc(cfg(any(feature = "native-tls", feature = "rustls-tls", feature = "boring-tls")))
)]
Required(TlsParameters), Required(TlsParameters),
/// Use TLS wrapped connection /// Use TLS wrapped connection
#[cfg(any(feature = "native-tls", feature = "rustls-tls"))] #[cfg(any(feature = "native-tls", feature = "rustls-tls", feature = "boring-tls"))]
#[cfg_attr(docsrs, doc(cfg(any(feature = "native-tls", feature = "rustls-tls"))))] #[cfg_attr(
docsrs,
doc(cfg(any(feature = "native-tls", feature = "rustls-tls", feature = "boring-tls")))
)]
Wrapper(TlsParameters), Wrapper(TlsParameters),
} }
@@ -43,31 +87,65 @@ impl Debug for Tls {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match &self { match &self {
Self::None => f.pad("None"), Self::None => f.pad("None"),
#[cfg(any(feature = "native-tls", feature = "rustls-tls"))] #[cfg(any(feature = "native-tls", feature = "rustls", feature = "boring-tls"))]
Self::Opportunistic(_) => f.pad("Opportunistic"), Self::Opportunistic(_) => f.pad("Opportunistic"),
#[cfg(any(feature = "native-tls", feature = "rustls-tls"))] #[cfg(any(feature = "native-tls", feature = "rustls", feature = "boring-tls"))]
Self::Required(_) => f.pad("Required"), Self::Required(_) => f.pad("Required"),
#[cfg(any(feature = "native-tls", feature = "rustls-tls"))] #[cfg(any(feature = "native-tls", feature = "rustls", feature = "boring-tls"))]
Self::Wrapper(_) => f.pad("Wrapper"), Self::Wrapper(_) => f.pad("Wrapper"),
} }
} }
} }
/// Source for the base set of root certificates to trust.
#[allow(missing_copy_implementations)]
#[derive(Clone, Debug)]
pub enum CertificateStore {
/// Use the default for the TLS backend.
///
/// For native-tls, this will use the system certificate store on Windows, the keychain on
/// macOS, and OpenSSL directories on Linux (usually `/etc/ssl`).
///
/// For rustls, this will also use the the system store if the `rustls-native-certs` feature is
/// enabled, or will fall back to `webpki-roots`.
///
/// The boring-tls backend uses the same logic as OpenSSL on all platforms.
Default,
/// Use a hardcoded set of Mozilla roots via the `webpki-roots` crate.
///
/// This option is only available in the rustls backend.
#[cfg(feature = "webpki-roots")]
WebpkiRoots,
/// Don't use any system certificates.
None,
}
impl Default for CertificateStore {
fn default() -> Self {
CertificateStore::Default
}
}
/// Parameters to use for secure clients /// Parameters to use for secure clients
#[derive(Clone)] #[derive(Clone)]
pub struct TlsParameters { pub struct TlsParameters {
pub(crate) connector: InnerTlsParameters, pub(crate) connector: InnerTlsParameters,
/// The domain name which is expected in the TLS certificate from the server /// The domain name which is expected in the TLS certificate from the server
pub(super) domain: String, pub(super) domain: String,
#[cfg(feature = "boring-tls")]
pub(super) accept_invalid_hostnames: bool,
} }
/// Builder for `TlsParameters` /// Builder for `TlsParameters`
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct TlsParametersBuilder { pub struct TlsParametersBuilder {
domain: String, domain: String,
cert_store: CertificateStore,
root_certs: Vec<Certificate>, root_certs: Vec<Certificate>,
accept_invalid_hostnames: bool, accept_invalid_hostnames: bool,
accept_invalid_certs: bool, accept_invalid_certs: bool,
#[cfg(any(feature = "native-tls", feature = "rustls-tls", feature = "boring-tls"))]
min_tls_version: TlsVersion,
} }
impl TlsParametersBuilder { impl TlsParametersBuilder {
@@ -75,12 +153,21 @@ impl TlsParametersBuilder {
pub fn new(domain: String) -> Self { pub fn new(domain: String) -> Self {
Self { Self {
domain, domain,
cert_store: CertificateStore::Default,
root_certs: Vec::new(), root_certs: Vec::new(),
accept_invalid_hostnames: false, accept_invalid_hostnames: false,
accept_invalid_certs: false, accept_invalid_certs: false,
#[cfg(any(feature = "native-tls", feature = "rustls-tls", feature = "boring-tls"))]
min_tls_version: TlsVersion::Tlsv12,
} }
} }
/// Set the source for the base set of root certificates to trust.
pub fn certificate_store(mut self, cert_store: CertificateStore) -> Self {
self.cert_store = cert_store;
self
}
/// Add a custom root certificate /// Add a custom root certificate
/// ///
/// Can be used to safely connect to a server using a self signed certificate, for example. /// Can be used to safely connect to a server using a self signed certificate, for example.
@@ -102,13 +189,22 @@ impl TlsParametersBuilder {
/// This method introduces significant vulnerabilities to man-in-the-middle attacks. /// This method introduces significant vulnerabilities to man-in-the-middle attacks.
/// ///
/// Hostname verification can only be disabled with the `native-tls` TLS backend. /// Hostname verification can only be disabled with the `native-tls` TLS backend.
#[cfg(feature = "native-tls")] #[cfg(any(feature = "native-tls", feature = "boring-tls"))]
#[cfg_attr(docsrs, doc(cfg(feature = "native-tls")))] #[cfg_attr(docsrs, doc(cfg(any(feature = "native-tls", feature = "boring-tls"))))]
pub fn dangerous_accept_invalid_hostnames(mut self, accept_invalid_hostnames: bool) -> Self { pub fn dangerous_accept_invalid_hostnames(mut self, accept_invalid_hostnames: bool) -> Self {
self.accept_invalid_hostnames = accept_invalid_hostnames; self.accept_invalid_hostnames = accept_invalid_hostnames;
self self
} }
/// Controls which minimum TLS version is allowed
///
/// Defaults to [`Tlsv12`][TlsVersion::Tlsv12].
#[cfg(any(feature = "native-tls", feature = "rustls-tls", feature = "boring-tls"))]
pub fn set_min_tls_version(mut self, min_tls_version: TlsVersion) -> Self {
self.min_tls_version = min_tls_version;
self
}
/// Controls whether invalid certificates are accepted /// Controls whether invalid certificates are accepted
/// ///
/// Defaults to `false`. /// Defaults to `false`.
@@ -130,16 +226,20 @@ impl TlsParametersBuilder {
self self
} }
/// Creates a new `TlsParameters` using native-tls or rustls /// Creates a new `TlsParameters` using native-tls, boring-tls or rustls
/// depending on which one is available /// depending on which one is available
#[cfg(any(feature = "native-tls", feature = "rustls-tls"))] #[cfg(any(feature = "native-tls", feature = "rustls-tls", feature = "boring-tls"))]
#[cfg_attr(docsrs, doc(cfg(any(feature = "native-tls", feature = "rustls-tls"))))] #[cfg_attr(
docsrs,
doc(cfg(any(feature = "native-tls", feature = "rustls-tls", feature = "boring-tls")))
)]
pub fn build(self) -> Result<TlsParameters, Error> { pub fn build(self) -> Result<TlsParameters, Error> {
#[cfg(feature = "rustls-tls")] #[cfg(feature = "rustls-tls")]
return self.build_rustls(); return self.build_rustls();
#[cfg(all(not(feature = "rustls-tls"), feature = "native-tls"))]
#[cfg(not(feature = "rustls-tls"))]
return self.build_native(); return self.build_native();
#[cfg(all(not(feature = "rustls-tls"), feature = "boring-tls"))]
return self.build_boring();
} }
/// Creates a new `TlsParameters` using native-tls with the provided configuration /// Creates a new `TlsParameters` using native-tls with the provided configuration
@@ -148,17 +248,93 @@ impl TlsParametersBuilder {
pub fn build_native(self) -> Result<TlsParameters, Error> { pub fn build_native(self) -> Result<TlsParameters, Error> {
let mut tls_builder = TlsConnector::builder(); let mut tls_builder = TlsConnector::builder();
match self.cert_store {
CertificateStore::Default => {}
CertificateStore::None => {
tls_builder.disable_built_in_roots(true);
}
#[allow(unreachable_patterns)]
other => {
return Err(error::tls(format!(
"{other:?} is not supported in native tls"
)))
}
}
for cert in self.root_certs { for cert in self.root_certs {
tls_builder.add_root_certificate(cert.native_tls); tls_builder.add_root_certificate(cert.native_tls);
} }
tls_builder.danger_accept_invalid_hostnames(self.accept_invalid_hostnames); tls_builder.danger_accept_invalid_hostnames(self.accept_invalid_hostnames);
tls_builder.danger_accept_invalid_certs(self.accept_invalid_certs); tls_builder.danger_accept_invalid_certs(self.accept_invalid_certs);
tls_builder.min_protocol_version(Some(DEFAULT_TLS_MIN_PROTOCOL)); let min_tls_version = match self.min_tls_version {
TlsVersion::Tlsv10 => Protocol::Tlsv10,
TlsVersion::Tlsv11 => Protocol::Tlsv11,
TlsVersion::Tlsv12 => Protocol::Tlsv12,
TlsVersion::Tlsv13 => {
return Err(error::tls(
"min tls version Tlsv13 not supported in native tls",
))
}
};
tls_builder.min_protocol_version(Some(min_tls_version));
let connector = tls_builder.build().map_err(error::tls)?; let connector = tls_builder.build().map_err(error::tls)?;
Ok(TlsParameters { Ok(TlsParameters {
connector: InnerTlsParameters::NativeTls(connector), connector: InnerTlsParameters::NativeTls(connector),
domain: self.domain, domain: self.domain,
#[cfg(feature = "boring-tls")]
accept_invalid_hostnames: self.accept_invalid_hostnames,
})
}
/// Creates a new `TlsParameters` using boring-tls with the provided configuration
#[cfg(feature = "boring-tls")]
#[cfg_attr(docsrs, doc(cfg(feature = "boring-tls")))]
pub fn build_boring(self) -> Result<TlsParameters, Error> {
use boring::ssl::{SslMethod, SslVerifyMode};
let mut tls_builder = SslConnector::builder(SslMethod::tls_client()).map_err(error::tls)?;
if self.accept_invalid_certs {
tls_builder.set_verify(SslVerifyMode::NONE);
} else {
match self.cert_store {
CertificateStore::Default => {}
CertificateStore::None => {
// Replace the default store with an empty store.
tls_builder
.set_cert_store(X509StoreBuilder::new().map_err(error::tls)?.build());
}
#[allow(unreachable_patterns)]
other => {
return Err(error::tls(format!(
"{other:?} is not supported in boring tls"
)))
}
}
let cert_store = tls_builder.cert_store_mut();
for cert in self.root_certs {
cert_store.add_cert(cert.boring_tls).map_err(error::tls)?;
}
}
let min_tls_version = match self.min_tls_version {
TlsVersion::Tlsv10 => SslVersion::TLS1,
TlsVersion::Tlsv11 => SslVersion::TLS1_1,
TlsVersion::Tlsv12 => SslVersion::TLS1_2,
TlsVersion::Tlsv13 => SslVersion::TLS1_3,
};
tls_builder
.set_min_proto_version(Some(min_tls_version))
.map_err(error::tls)?;
let connector = tls_builder.build();
Ok(TlsParameters {
connector: InnerTlsParameters::BoringTls(connector),
domain: self.domain,
accept_invalid_hostnames: self.accept_invalid_hostnames,
}) })
} }
@@ -167,26 +343,81 @@ impl TlsParametersBuilder {
#[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> {
let tls = ClientConfig::builder(); let tls = ClientConfig::builder();
let tls = tls.with_safe_defaults();
let just_version3 = &[&rustls::version::TLS13];
let supported_versions = match self.min_tls_version {
TlsVersion::Tlsv10 => {
return Err(error::tls("min tls version Tlsv10 not supported in rustls"))
}
TlsVersion::Tlsv11 => {
return Err(error::tls("min tls version Tlsv11 not supported in rustls"))
}
TlsVersion::Tlsv12 => rustls::ALL_VERSIONS,
TlsVersion::Tlsv13 => just_version3,
};
let tls = tls
.with_safe_default_cipher_suites()
.with_safe_default_kx_groups()
.with_protocol_versions(supported_versions)
.map_err(error::tls)?;
let tls = if self.accept_invalid_certs { let tls = if self.accept_invalid_certs {
tls.with_custom_certificate_verifier(Arc::new(InvalidCertsVerifier {})) tls.with_custom_certificate_verifier(Arc::new(InvalidCertsVerifier {}))
} else { } else {
let mut root_cert_store = RootCertStore::empty(); let mut root_cert_store = RootCertStore::empty();
#[cfg(feature = "rustls-native-certs")]
fn load_native_roots(store: &mut RootCertStore) -> Result<(), Error> {
let native_certs = rustls_native_certs::load_native_certs().map_err(error::tls)?;
let mut valid_count = 0;
let mut invalid_count = 0;
for cert in native_certs {
match store.add(&rustls::Certificate(cert.0)) {
Ok(_) => valid_count += 1,
Err(err) => {
#[cfg(feature = "tracing")]
tracing::debug!("certificate parsing failed: {:?}", err);
invalid_count += 1;
}
}
}
#[cfg(feature = "tracing")]
tracing::debug!(
"loaded platform certs with {valid_count} valid and {invalid_count} invalid certs"
);
Ok(())
}
#[cfg(feature = "webpki-roots")]
fn load_webpki_roots(store: &mut RootCertStore) {
store.add_server_trust_anchors(webpki_roots::TLS_SERVER_ROOTS.0.iter().map(|ta| {
rustls::OwnedTrustAnchor::from_subject_spki_name_constraints(
ta.subject,
ta.spki,
ta.name_constraints,
)
}));
}
match self.cert_store {
CertificateStore::Default => {
#[cfg(feature = "rustls-native-certs")]
load_native_roots(&mut root_cert_store)?;
#[cfg(all(not(feature = "rustls-native-certs"), feature = "webpki-roots"))]
load_webpki_roots(&mut root_cert_store);
}
#[cfg(feature = "webpki-roots")]
CertificateStore::WebpkiRoots => {
load_webpki_roots(&mut root_cert_store);
}
CertificateStore::None => {}
}
for cert in self.root_certs { for cert in self.root_certs {
for rustls_cert in cert.rustls { for rustls_cert in cert.rustls {
root_cert_store.add(&rustls_cert).map_err(error::tls)?; 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(
|ta| {
OwnedTrustAnchor::from_subject_spki_name_constraints(
ta.subject,
ta.spki,
ta.name_constraints,
)
},
));
tls.with_custom_certificate_verifier(Arc::new(WebPkiVerifier::new( tls.with_custom_certificate_verifier(Arc::new(WebPkiVerifier::new(
root_cert_store, root_cert_store,
@@ -198,27 +429,36 @@ impl TlsParametersBuilder {
Ok(TlsParameters { Ok(TlsParameters {
connector: InnerTlsParameters::RustlsTls(Arc::new(tls)), connector: InnerTlsParameters::RustlsTls(Arc::new(tls)),
domain: self.domain, domain: self.domain,
#[cfg(feature = "boring-tls")]
accept_invalid_hostnames: self.accept_invalid_hostnames,
}) })
} }
} }
#[derive(Clone)] #[derive(Clone)]
#[allow(clippy::enum_variant_names)]
pub enum InnerTlsParameters { pub enum InnerTlsParameters {
#[cfg(feature = "native-tls")] #[cfg(feature = "native-tls")]
NativeTls(TlsConnector), NativeTls(TlsConnector),
#[cfg(feature = "rustls-tls")] #[cfg(feature = "rustls-tls")]
RustlsTls(Arc<ClientConfig>), RustlsTls(Arc<ClientConfig>),
#[cfg(feature = "boring-tls")]
BoringTls(SslConnector),
} }
impl TlsParameters { impl TlsParameters {
/// Creates a new `TlsParameters` using native-tls or rustls /// Creates a new `TlsParameters` using native-tls or rustls
/// depending on which one is available /// depending on which one is available
#[cfg(any(feature = "native-tls", feature = "rustls-tls"))] #[cfg(any(feature = "native-tls", feature = "rustls-tls", feature = "boring-tls"))]
#[cfg_attr(docsrs, doc(cfg(any(feature = "native-tls", feature = "rustls-tls"))))] #[cfg_attr(
docsrs,
doc(cfg(any(feature = "native-tls", feature = "rustls-tls", feature = "boring-tls")))
)]
pub fn new(domain: String) -> Result<Self, Error> { pub fn new(domain: String) -> Result<Self, Error> {
TlsParametersBuilder::new(domain).build() TlsParametersBuilder::new(domain).build()
} }
/// Creates a new `TlsParameters` builder
pub fn builder(domain: String) -> TlsParametersBuilder { pub fn builder(domain: String) -> TlsParametersBuilder {
TlsParametersBuilder::new(domain) TlsParametersBuilder::new(domain)
} }
@@ -237,6 +477,13 @@ impl TlsParameters {
TlsParametersBuilder::new(domain).build_rustls() TlsParametersBuilder::new(domain).build_rustls()
} }
/// Creates a new `TlsParameters` using boring
#[cfg(feature = "boring-tls")]
#[cfg_attr(docsrs, doc(cfg(feature = "boring-tls")))]
pub fn new_boring(domain: String) -> Result<Self, Error> {
TlsParametersBuilder::new(domain).build_boring()
}
pub fn domain(&self) -> &str { pub fn domain(&self) -> &str {
&self.domain &self.domain
} }
@@ -250,20 +497,27 @@ pub struct Certificate {
native_tls: native_tls::Certificate, native_tls: native_tls::Certificate,
#[cfg(feature = "rustls-tls")] #[cfg(feature = "rustls-tls")]
rustls: Vec<rustls::Certificate>, rustls: Vec<rustls::Certificate>,
#[cfg(feature = "boring-tls")]
boring_tls: boring::x509::X509,
} }
#[cfg(any(feature = "native-tls", feature = "rustls-tls"))] #[cfg(any(feature = "native-tls", feature = "rustls-tls", feature = "boring-tls"))]
impl Certificate { impl Certificate {
/// Create a `Certificate` from a DER encoded certificate /// Create a `Certificate` from a DER encoded certificate
pub fn from_der(der: Vec<u8>) -> Result<Self, Error> { pub fn from_der(der: Vec<u8>) -> Result<Self, Error> {
#[cfg(feature = "native-tls")] #[cfg(feature = "native-tls")]
let native_tls_cert = native_tls::Certificate::from_der(&der).map_err(error::tls)?; let native_tls_cert = native_tls::Certificate::from_der(&der).map_err(error::tls)?;
#[cfg(feature = "boring-tls")]
let boring_tls_cert = boring::x509::X509::from_der(&der).map_err(error::tls)?;
Ok(Self { Ok(Self {
#[cfg(feature = "native-tls")] #[cfg(feature = "native-tls")]
native_tls: native_tls_cert, native_tls: native_tls_cert,
#[cfg(feature = "rustls-tls")] #[cfg(feature = "rustls-tls")]
rustls: vec![rustls::Certificate(der)], rustls: vec![rustls::Certificate(der)],
#[cfg(feature = "boring-tls")]
boring_tls: boring_tls_cert,
}) })
} }
@@ -272,6 +526,9 @@ impl Certificate {
#[cfg(feature = "native-tls")] #[cfg(feature = "native-tls")]
let native_tls_cert = native_tls::Certificate::from_pem(pem).map_err(error::tls)?; let native_tls_cert = native_tls::Certificate::from_pem(pem).map_err(error::tls)?;
#[cfg(feature = "boring-tls")]
let boring_tls_cert = boring::x509::X509::from_pem(pem).map_err(error::tls)?;
#[cfg(feature = "rustls-tls")] #[cfg(feature = "rustls-tls")]
let rustls_cert = { let rustls_cert = {
use std::io::Cursor; use std::io::Cursor;
@@ -289,6 +546,8 @@ impl Certificate {
native_tls: native_tls_cert, native_tls: native_tls_cert,
#[cfg(feature = "rustls-tls")] #[cfg(feature = "rustls-tls")]
rustls: rustls_cert, rustls: rustls_cert,
#[cfg(feature = "boring-tls")]
boring_tls: boring_tls_cert,
}) })
} }
} }

View File

@@ -13,7 +13,7 @@ use crate::{
}; };
/// EHLO command /// EHLO command
#[derive(PartialEq, Clone, Debug)] #[derive(PartialEq, Eq, Clone, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Ehlo { pub struct Ehlo {
client_id: ClientId, client_id: ClientId,
@@ -33,7 +33,7 @@ impl Ehlo {
} }
/// STARTTLS command /// STARTTLS command
#[derive(PartialEq, Clone, Debug, Copy)] #[derive(PartialEq, Eq, Clone, Debug, Copy)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Starttls; pub struct Starttls;
@@ -44,7 +44,7 @@ impl Display for Starttls {
} }
/// MAIL command /// MAIL command
#[derive(PartialEq, Clone, Debug)] #[derive(PartialEq, Eq, Clone, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Mail { pub struct Mail {
sender: Option<Address>, sender: Option<Address>,
@@ -59,7 +59,7 @@ impl Display for Mail {
self.sender.as_ref().map_or("", |s| s.as_ref()) self.sender.as_ref().map_or("", |s| s.as_ref())
)?; )?;
for parameter in &self.parameters { for parameter in &self.parameters {
write!(f, " {}", parameter)?; write!(f, " {parameter}")?;
} }
f.write_str("\r\n") f.write_str("\r\n")
} }
@@ -73,7 +73,7 @@ impl Mail {
} }
/// RCPT command /// RCPT command
#[derive(PartialEq, Clone, Debug)] #[derive(PartialEq, Eq, Clone, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Rcpt { pub struct Rcpt {
recipient: Address, recipient: Address,
@@ -84,7 +84,7 @@ impl Display for Rcpt {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "RCPT TO:<{}>", self.recipient)?; write!(f, "RCPT TO:<{}>", self.recipient)?;
for parameter in &self.parameters { for parameter in &self.parameters {
write!(f, " {}", parameter)?; write!(f, " {parameter}")?;
} }
f.write_str("\r\n") f.write_str("\r\n")
} }
@@ -101,7 +101,7 @@ impl Rcpt {
} }
/// DATA command /// DATA command
#[derive(PartialEq, Clone, Debug, Copy)] #[derive(PartialEq, Eq, Clone, Debug, Copy)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Data; pub struct Data;
@@ -112,7 +112,7 @@ impl Display for Data {
} }
/// QUIT command /// QUIT command
#[derive(PartialEq, Clone, Debug, Copy)] #[derive(PartialEq, Eq, Clone, Debug, Copy)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Quit; pub struct Quit;
@@ -123,7 +123,7 @@ impl Display for Quit {
} }
/// NOOP command /// NOOP command
#[derive(PartialEq, Clone, Debug, Copy)] #[derive(PartialEq, Eq, Clone, Debug, Copy)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Noop; pub struct Noop;
@@ -134,7 +134,7 @@ impl Display for Noop {
} }
/// HELP command /// HELP command
#[derive(PartialEq, Clone, Debug)] #[derive(PartialEq, Eq, Clone, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Help { pub struct Help {
argument: Option<String>, argument: Option<String>,
@@ -144,7 +144,7 @@ impl Display for Help {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.write_str("HELP")?; f.write_str("HELP")?;
if let Some(argument) = &self.argument { if let Some(argument) = &self.argument {
write!(f, " {}", argument)?; write!(f, " {argument}")?;
} }
f.write_str("\r\n") f.write_str("\r\n")
} }
@@ -158,7 +158,7 @@ impl Help {
} }
/// VRFY command /// VRFY command
#[derive(PartialEq, Clone, Debug)] #[derive(PartialEq, Eq, Clone, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Vrfy { pub struct Vrfy {
argument: String, argument: String,
@@ -178,7 +178,7 @@ impl Vrfy {
} }
/// EXPN command /// EXPN command
#[derive(PartialEq, Clone, Debug)] #[derive(PartialEq, Eq, Clone, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Expn { pub struct Expn {
argument: String, argument: String,
@@ -198,7 +198,7 @@ impl Expn {
} }
/// RSET command /// RSET command
#[derive(PartialEq, Clone, Debug, Copy)] #[derive(PartialEq, Eq, Clone, Debug, Copy)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Rset; pub struct Rset;
@@ -209,7 +209,7 @@ impl Display for Rset {
} }
/// AUTH command /// AUTH command
#[derive(PartialEq, Clone, Debug)] #[derive(PartialEq, Eq, Clone, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Auth { pub struct Auth {
mechanism: Mechanism, mechanism: Mechanism,
@@ -220,7 +220,7 @@ pub struct Auth {
impl Display for Auth { impl Display for Auth {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
let encoded_response = self.response.as_ref().map(base64::encode); let encoded_response = self.response.as_ref().map(crate::base64::encode);
if self.mechanism.supports_initial_response() { if self.mechanism.supports_initial_response() {
write!(f, "AUTH {} {}", self.mechanism, encoded_response.unwrap())?; write!(f, "AUTH {} {}", self.mechanism, encoded_response.unwrap())?;
@@ -271,7 +271,7 @@ impl Auth {
#[cfg(feature = "tracing")] #[cfg(feature = "tracing")]
tracing::debug!("auth encoded challenge: {}", encoded_challenge); tracing::debug!("auth encoded challenge: {}", encoded_challenge);
let decoded_base64 = base64::decode(&encoded_challenge).map_err(error::response)?; let decoded_base64 = crate::base64::decode(encoded_challenge).map_err(error::response)?;
let decoded_challenge = String::from_utf8(decoded_base64).map_err(error::response)?; let decoded_challenge = String::from_utf8(decoded_base64).map_err(error::response)?;
#[cfg(feature = "tracing")] #[cfg(feature = "tracing")]
tracing::debug!("auth decoded challenge: {}", decoded_challenge); tracing::debug!("auth decoded challenge: {}", decoded_challenge);
@@ -296,15 +296,15 @@ mod test {
#[test] #[test]
fn test_display() { fn test_display() {
let id = ClientId::Domain("localhost".to_string()); let id = ClientId::Domain("localhost".to_owned());
let email = Address::from_str("test@example.com").unwrap(); let email = Address::from_str("test@example.com").unwrap();
let mail_parameter = MailParameter::Other { let mail_parameter = MailParameter::Other {
keyword: "TEST".to_string(), keyword: "TEST".to_owned(),
value: Some("value".to_string()), value: Some("value".to_owned()),
}; };
let rcpt_parameter = RcptParameter::Other { let rcpt_parameter = RcptParameter::Other {
keyword: "TEST".to_string(), keyword: "TEST".to_owned(),
value: Some("value".to_string()), value: Some("value".to_owned()),
}; };
assert_eq!(format!("{}", Ehlo::new(id)), "EHLO localhost\r\n"); assert_eq!(format!("{}", Ehlo::new(id)), "EHLO localhost\r\n");
assert_eq!( assert_eq!(
@@ -341,24 +341,18 @@ mod test {
format!("{}", Rcpt::new(email, vec![rcpt_parameter])), format!("{}", Rcpt::new(email, vec![rcpt_parameter])),
"RCPT TO:<test@example.com> TEST=value\r\n" "RCPT TO:<test@example.com> TEST=value\r\n"
); );
assert_eq!(format!("{}", Quit), "QUIT\r\n"); assert_eq!(format!("{Quit}"), "QUIT\r\n");
assert_eq!(format!("{}", Data), "DATA\r\n"); assert_eq!(format!("{Data}"), "DATA\r\n");
assert_eq!(format!("{}", Noop), "NOOP\r\n"); assert_eq!(format!("{Noop}"), "NOOP\r\n");
assert_eq!(format!("{}", Help::new(None)), "HELP\r\n"); assert_eq!(format!("{}", Help::new(None)), "HELP\r\n");
assert_eq!( assert_eq!(
format!("{}", Help::new(Some("test".to_string()))), format!("{}", Help::new(Some("test".to_owned()))),
"HELP test\r\n" "HELP test\r\n"
); );
assert_eq!( assert_eq!(format!("{}", Vrfy::new("test".to_owned())), "VRFY test\r\n");
format!("{}", Vrfy::new("test".to_string())), assert_eq!(format!("{}", Expn::new("test".to_owned())), "EXPN test\r\n");
"VRFY test\r\n" assert_eq!(format!("{Rset}"), "RSET\r\n");
); let credentials = Credentials::new("user".to_owned(), "password".to_owned());
assert_eq!(
format!("{}", Expn::new("test".to_string())),
"EXPN test\r\n"
);
assert_eq!(format!("{}", Rset), "RSET\r\n");
let credentials = Credentials::new("user".to_string(), "password".to_string());
assert_eq!( assert_eq!(
format!( format!(
"{}", "{}",

View File

@@ -68,8 +68,11 @@ impl Error {
} }
/// Returns true if the error is from TLS /// Returns true if the error is from TLS
#[cfg(any(feature = "native-tls", feature = "rustls-tls"))] #[cfg(any(feature = "native-tls", feature = "rustls-tls", feature = "boring-tls"))]
#[cfg_attr(docsrs, doc(cfg(any(feature = "native-tls", feature = "rustls-tls"))))] #[cfg_attr(
docsrs,
doc(cfg(any(feature = "native-tls", feature = "rustls-tls", feature = "boring-tls")))
)]
pub fn is_tls(&self) -> bool { pub fn is_tls(&self) -> bool {
matches!(self.inner.kind, Kind::Tls) matches!(self.inner.kind, Kind::Tls)
} }
@@ -102,8 +105,11 @@ pub(crate) enum Kind {
/// Underlying network i/o error /// Underlying network i/o error
Network, Network,
/// TLS error /// TLS error
#[cfg_attr(docsrs, doc(cfg(any(feature = "native-tls", feature = "rustls-tls"))))] #[cfg_attr(
#[cfg(any(feature = "native-tls", feature = "rustls-tls"))] docsrs,
doc(cfg(any(feature = "native-tls", feature = "rustls-tls", feature = "boring-tls")))
)]
#[cfg(any(feature = "native-tls", feature = "rustls-tls", feature = "boring-tls"))]
Tls, Tls,
} }
@@ -128,18 +134,18 @@ impl fmt::Display for Error {
Kind::Client => f.write_str("internal client error")?, Kind::Client => f.write_str("internal client error")?,
Kind::Network => f.write_str("network error")?, Kind::Network => f.write_str("network error")?,
Kind::Connection => f.write_str("Connection error")?, Kind::Connection => f.write_str("Connection error")?,
#[cfg(any(feature = "native-tls", feature = "rustls-tls"))] #[cfg(any(feature = "native-tls", feature = "rustls-tls", feature = "boring-tls"))]
Kind::Tls => f.write_str("tls error")?, Kind::Tls => f.write_str("tls error")?,
Kind::Transient(ref code) => { Kind::Transient(ref code) => {
write!(f, "transient error ({})", code)?; write!(f, "transient error ({code})")?;
} }
Kind::Permanent(ref code) => { Kind::Permanent(ref code) => {
write!(f, "permanent error ({})", code)?; write!(f, "permanent error ({code})")?;
} }
}; };
if let Some(ref e) = self.inner.source { if let Some(ref e) = self.inner.source {
write!(f, ": {}", e)?; write!(f, ": {e}")?;
} }
Ok(()) Ok(())
@@ -179,7 +185,7 @@ pub(crate) fn connection<E: Into<BoxError>>(e: E) -> Error {
Error::new(Kind::Connection, Some(e)) Error::new(Kind::Connection, Some(e))
} }
#[cfg(any(feature = "native-tls", feature = "rustls-tls"))] #[cfg(any(feature = "native-tls", feature = "rustls-tls", feature = "boring-tls"))]
pub(crate) fn tls<E: Into<BoxError>>(e: E) -> Error { pub(crate) fn tls<E: Into<BoxError>>(e: E) -> Error {
Error::new(Kind::Tls, Some(e)) Error::new(Kind::Tls, Some(e))
} }

View File

@@ -55,8 +55,8 @@ impl Display for ClientId {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match *self { match *self {
Self::Domain(ref value) => f.write_str(value), Self::Domain(ref value) => f.write_str(value),
Self::Ipv4(ref value) => write!(f, "[{}]", value), Self::Ipv4(ref value) => write!(f, "[{value}]"),
Self::Ipv6(ref value) => write!(f, "[IPv6:{}]", value), Self::Ipv6(ref value) => write!(f, "[IPv6:{value}]"),
} }
} }
} }
@@ -97,7 +97,7 @@ impl Display for Extension {
Extension::EightBitMime => f.write_str("8BITMIME"), Extension::EightBitMime => f.write_str("8BITMIME"),
Extension::SmtpUtfEight => f.write_str("SMTPUTF8"), Extension::SmtpUtfEight => f.write_str("SMTPUTF8"),
Extension::StartTls => f.write_str("STARTTLS"), Extension::StartTls => f.write_str("STARTTLS"),
Extension::Authentication(ref mechanism) => write!(f, "AUTH {}", mechanism), Extension::Authentication(ref mechanism) => write!(f, "AUTH {mechanism}"),
} }
} }
} }
@@ -119,7 +119,7 @@ pub struct ServerInfo {
impl Display for ServerInfo { impl Display for ServerInfo {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
let features = if self.features.is_empty() { let features = if self.features.is_empty() {
"no supported features".to_string() "no supported features".to_owned()
} else { } else {
format!("{:?}", self.features) format!("{:?}", self.features)
}; };
@@ -174,7 +174,7 @@ impl ServerInfo {
} }
Ok(ServerInfo { Ok(ServerInfo {
name: name.to_string(), name: name.to_owned(),
features, features,
}) })
} }
@@ -228,8 +228,8 @@ pub enum MailParameter {
impl Display for MailParameter { impl Display for MailParameter {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match *self { match *self {
MailParameter::Body(ref value) => write!(f, "BODY={}", value), MailParameter::Body(ref value) => write!(f, "BODY={value}"),
MailParameter::Size(size) => write!(f, "SIZE={}", size), MailParameter::Size(size) => write!(f, "SIZE={size}"),
MailParameter::SmtpUtfEight => f.write_str("SMTPUTF8"), MailParameter::SmtpUtfEight => f.write_str("SMTPUTF8"),
MailParameter::Other { MailParameter::Other {
ref keyword, ref keyword,
@@ -304,21 +304,21 @@ mod test {
#[test] #[test]
fn test_clientid_fmt() { fn test_clientid_fmt() {
assert_eq!( assert_eq!(
format!("{}", ClientId::Domain("test".to_string())), format!("{}", ClientId::Domain("test".to_owned())),
"test".to_string() "test".to_owned()
); );
assert_eq!(format!("{}", LOCALHOST_CLIENT), "[127.0.0.1]".to_string()); assert_eq!(format!("{LOCALHOST_CLIENT}"), "[127.0.0.1]".to_owned());
} }
#[test] #[test]
fn test_extension_fmt() { fn test_extension_fmt() {
assert_eq!( assert_eq!(
format!("{}", Extension::EightBitMime), format!("{}", Extension::EightBitMime),
"8BITMIME".to_string() "8BITMIME".to_owned()
); );
assert_eq!( assert_eq!(
format!("{}", Extension::Authentication(Mechanism::Plain)), format!("{}", Extension::Authentication(Mechanism::Plain)),
"AUTH PLAIN".to_string() "AUTH PLAIN".to_owned()
); );
} }
@@ -331,11 +331,11 @@ mod test {
format!( format!(
"{}", "{}",
ServerInfo { ServerInfo {
name: "name".to_string(), name: "name".to_owned(),
features: eightbitmime, features: eightbitmime,
} }
), ),
"name with {EightBitMime}".to_string() "name with {EightBitMime}".to_owned()
); );
let empty = HashSet::new(); let empty = HashSet::new();
@@ -344,11 +344,11 @@ mod test {
format!( format!(
"{}", "{}",
ServerInfo { ServerInfo {
name: "name".to_string(), name: "name".to_owned(),
features: empty, features: empty,
} }
), ),
"name with no supported features".to_string() "name with no supported features".to_owned()
); );
let mut plain = HashSet::new(); let mut plain = HashSet::new();
@@ -358,11 +358,11 @@ mod test {
format!( format!(
"{}", "{}",
ServerInfo { ServerInfo {
name: "name".to_string(), name: "name".to_owned(),
features: plain, features: plain,
} }
), ),
"name with {Authentication(Plain)}".to_string() "name with {Authentication(Plain)}".to_owned()
); );
} }
@@ -374,18 +374,14 @@ mod test {
Category::Unspecified4, Category::Unspecified4,
Detail::One, Detail::One,
), ),
vec![ vec!["me".to_owned(), "8BITMIME".to_owned(), "SIZE 42".to_owned()],
"me".to_string(),
"8BITMIME".to_string(),
"SIZE 42".to_string(),
],
); );
let mut features = HashSet::new(); let mut features = HashSet::new();
assert!(features.insert(Extension::EightBitMime)); assert!(features.insert(Extension::EightBitMime));
let server_info = ServerInfo { let server_info = ServerInfo {
name: "me".to_string(), name: "me".to_owned(),
features, features,
}; };
@@ -401,10 +397,10 @@ mod test {
Detail::One, Detail::One,
), ),
vec![ vec![
"me".to_string(), "me".to_owned(),
"AUTH PLAIN CRAM-MD5 XOAUTH2 OTHER".to_string(), "AUTH PLAIN CRAM-MD5 XOAUTH2 OTHER".to_owned(),
"8BITMIME".to_string(), "8BITMIME".to_owned(),
"SIZE 42".to_string(), "SIZE 42".to_owned(),
], ],
); );
@@ -414,7 +410,7 @@ mod test {
assert!(features2.insert(Extension::Authentication(Mechanism::Xoauth2),)); assert!(features2.insert(Extension::Authentication(Mechanism::Xoauth2),));
let server_info2 = ServerInfo { let server_info2 = ServerInfo {
name: "me".to_string(), name: "me".to_owned(),
features: features2, features: features2,
}; };

View File

@@ -77,8 +77,8 @@
//! let sender = SmtpTransport::starttls_relay("smtp.example.com")? //! let sender = SmtpTransport::starttls_relay("smtp.example.com")?
//! // Add credentials for authentication //! // Add credentials for authentication
//! .credentials(Credentials::new( //! .credentials(Credentials::new(
//! "username".to_string(), //! "username".to_owned(),
//! "password".to_string(), //! "password".to_owned(),
//! )) //! ))
//! // Configure expected authentication mechanism //! // Configure expected authentication mechanism
//! .authentication(vec![Mechanism::Plain]) //! .authentication(vec![Mechanism::Plain])
@@ -111,7 +111,7 @@
//! .body(String::from("Be happy!"))?; //! .body(String::from("Be happy!"))?;
//! //!
//! // Custom TLS configuration //! // Custom TLS configuration
//! let tls = TlsParameters::builder("smtp.example.com".to_string()) //! let tls = TlsParameters::builder("smtp.example.com".to_owned())
//! .dangerous_accept_invalid_certs(true) //! .dangerous_accept_invalid_certs(true)
//! .build()?; //! .build()?;
//! //!
@@ -140,7 +140,7 @@ pub use self::{
error::Error, error::Error,
transport::{SmtpTransport, SmtpTransportBuilder}, transport::{SmtpTransport, SmtpTransportBuilder},
}; };
#[cfg(any(feature = "native-tls", feature = "rustls-tls"))] #[cfg(any(feature = "native-tls", feature = "rustls-tls", feature = "boring-tls"))]
use crate::transport::smtp::client::TlsParameters; use crate::transport::smtp::client::TlsParameters;
use crate::transport::smtp::{ use crate::transport::smtp::{
authentication::{Credentials, Mechanism, DEFAULT_MECHANISMS}, authentication::{Credentials, Mechanism, DEFAULT_MECHANISMS},
@@ -200,7 +200,7 @@ struct SmtpInfo {
impl Default for SmtpInfo { impl Default for SmtpInfo {
fn default() -> Self { fn default() -> Self {
Self { Self {
server: "localhost".to_string(), server: "localhost".to_owned(),
port: SMTP_PORT, port: SMTP_PORT,
hello_name: ClientId::default(), hello_name: ClientId::default(),
credentials: None, credentials: None,

View File

@@ -158,14 +158,14 @@ impl<E: Executor> Pool<E> {
#[cfg(feature = "tracing")] #[cfg(feature = "tracing")]
tracing::debug!("reusing a pooled connection"); tracing::debug!("reusing a pooled connection");
return Ok(PooledConnection::wrap(conn, self.clone())); return Ok(PooledConnection::wrap(conn, Arc::clone(self)));
} }
None => { None => {
#[cfg(feature = "tracing")] #[cfg(feature = "tracing")]
tracing::debug!("creating a new connection"); tracing::debug!("creating a new connection");
let conn = self.client.connection().await?; let conn = self.client.connection().await?;
return Ok(PooledConnection::wrap(conn, self.clone())); return Ok(PooledConnection::wrap(conn, Arc::clone(self)));
} }
} }
} }
@@ -203,7 +203,7 @@ impl<E: Executor> Debug for Pool<E> {
&match self.connections.try_lock() { &match self.connections.try_lock() {
Some(connections) => format!("{} connections", connections.len()), Some(connections) => format!("{} connections", connections.len()),
None => "LOCKED".to_string(), None => "LOCKED".to_owned(),
}, },
) )
.field("client", &self.client) .field("client", &self.client)

View File

@@ -141,14 +141,14 @@ impl Pool {
#[cfg(feature = "tracing")] #[cfg(feature = "tracing")]
tracing::debug!("reusing a pooled connection"); tracing::debug!("reusing a pooled connection");
return Ok(PooledConnection::wrap(conn, self.clone())); return Ok(PooledConnection::wrap(conn, Arc::clone(self)));
} }
None => { None => {
#[cfg(feature = "tracing")] #[cfg(feature = "tracing")]
tracing::debug!("creating a new connection"); tracing::debug!("creating a new connection");
let conn = self.client.connection()?; let conn = self.client.connection()?;
return Ok(PooledConnection::wrap(conn, self.clone())); return Ok(PooledConnection::wrap(conn, Arc::clone(self)));
} }
} }
} }
@@ -186,8 +186,8 @@ impl Debug for Pool {
&match self.connections.try_lock() { &match self.connections.try_lock() {
Ok(connections) => format!("{} connections", connections.len()), Ok(connections) => format!("{} connections", connections.len()),
Err(TryLockError::WouldBlock) => "LOCKED".to_string(), Err(TryLockError::WouldBlock) => "LOCKED".to_owned(),
Err(TryLockError::Poisoned(_)) => "POISONED".to_string(), Err(TryLockError::Poisoned(_)) => "POISONED".to_owned(),
}, },
) )
.field("client", &self.client) .field("client", &self.client)

View File

@@ -151,7 +151,7 @@ impl FromStr for Response {
fn from_str(s: &str) -> result::Result<Response, Error> { fn from_str(s: &str) -> result::Result<Response, Error> {
parse_response(s) parse_response(s)
.map(|(_, r)| r) .map(|(_, r)| r)
.map_err(|e| error::response(e.to_string())) .map_err(|e| error::response(e.to_owned()))
} }
} }
@@ -329,10 +329,10 @@ mod test {
detail: Detail::Zero, detail: Detail::Zero,
}, },
message: vec![ message: vec![
"me".to_string(), "me".to_owned(),
"8BITMIME".to_string(), "8BITMIME".to_owned(),
"SIZE 42".to_string(), "SIZE 42".to_owned(),
"AUTH PLAIN CRAM-MD5".to_string(), "AUTH PLAIN CRAM-MD5".to_owned(),
], ],
} }
); );
@@ -352,11 +352,7 @@ mod test {
category: Category::MailSystem, category: Category::MailSystem,
detail: Detail::Zero, detail: Detail::Zero,
}, },
vec![ vec!["me".to_owned(), "8BITMIME".to_owned(), "SIZE 42".to_owned(),],
"me".to_string(),
"8BITMIME".to_string(),
"SIZE 42".to_string(),
],
) )
.is_positive()); .is_positive());
assert!(!Response::new( assert!(!Response::new(
@@ -365,11 +361,7 @@ mod test {
category: Category::MailSystem, category: Category::MailSystem,
detail: Detail::Zero, detail: Detail::Zero,
}, },
vec![ vec!["me".to_owned(), "8BITMIME".to_owned(), "SIZE 42".to_owned(),],
"me".to_string(),
"8BITMIME".to_string(),
"SIZE 42".to_string(),
],
) )
.is_positive()); .is_positive());
} }
@@ -382,11 +374,7 @@ mod test {
category: Category::MailSystem, category: Category::MailSystem,
detail: Detail::One, detail: Detail::One,
}, },
vec![ vec!["me".to_owned(), "8BITMIME".to_owned(), "SIZE 42".to_owned(),],
"me".to_string(),
"8BITMIME".to_string(),
"SIZE 42".to_string(),
],
) )
.has_code(451)); .has_code(451));
assert!(!Response::new( assert!(!Response::new(
@@ -395,11 +383,7 @@ mod test {
category: Category::MailSystem, category: Category::MailSystem,
detail: Detail::One, detail: Detail::One,
}, },
vec![ vec!["me".to_owned(), "8BITMIME".to_owned(), "SIZE 42".to_owned(),],
"me".to_string(),
"8BITMIME".to_string(),
"SIZE 42".to_string(),
],
) )
.has_code(251)); .has_code(251));
} }
@@ -413,11 +397,7 @@ mod test {
category: Category::MailSystem, category: Category::MailSystem,
detail: Detail::One, detail: Detail::One,
}, },
vec![ vec!["me".to_owned(), "8BITMIME".to_owned(), "SIZE 42".to_owned(),],
"me".to_string(),
"8BITMIME".to_string(),
"SIZE 42".to_string(),
],
) )
.first_word(), .first_word(),
Some("me") Some("me")
@@ -430,9 +410,9 @@ mod test {
detail: Detail::One, detail: Detail::One,
}, },
vec![ vec![
"me mo".to_string(), "me mo".to_owned(),
"8BITMIME".to_string(), "8BITMIME".to_owned(),
"SIZE 42".to_string(), "SIZE 42".to_owned(),
], ],
) )
.first_word(), .first_word(),
@@ -457,7 +437,7 @@ mod test {
category: Category::MailSystem, category: Category::MailSystem,
detail: Detail::One, detail: Detail::One,
}, },
vec![" ".to_string()], vec![" ".to_owned()],
) )
.first_word(), .first_word(),
None None
@@ -469,7 +449,7 @@ mod test {
category: Category::MailSystem, category: Category::MailSystem,
detail: Detail::One, detail: Detail::One,
}, },
vec![" ".to_string()], vec![" ".to_owned()],
) )
.first_word(), .first_word(),
None None
@@ -481,7 +461,7 @@ mod test {
category: Category::MailSystem, category: Category::MailSystem,
detail: Detail::One, detail: Detail::One,
}, },
vec!["".to_string()], vec!["".to_owned()],
) )
.first_word(), .first_word(),
None None
@@ -494,7 +474,7 @@ mod test {
let res = parse_response(raw_response); let res = parse_response(raw_response);
match res { match res {
Err(nom::Err::Incomplete(_)) => {} Err(nom::Err::Incomplete(_)) => {}
_ => panic!("Expected incomplete response, got {:?}", res), _ => panic!("Expected incomplete response, got {res:?}"),
} }
} }
@@ -507,11 +487,7 @@ mod test {
category: Category::MailSystem, category: Category::MailSystem,
detail: Detail::One, detail: Detail::One,
}, },
vec![ vec!["me".to_owned(), "8BITMIME".to_owned(), "SIZE 42".to_owned(),],
"me".to_string(),
"8BITMIME".to_string(),
"SIZE 42".to_string(),
],
) )
.first_line(), .first_line(),
Some("me") Some("me")
@@ -524,9 +500,9 @@ mod test {
detail: Detail::One, detail: Detail::One,
}, },
vec![ vec![
"me mo".to_string(), "me mo".to_owned(),
"8BITMIME".to_string(), "8BITMIME".to_owned(),
"SIZE 42".to_string(), "SIZE 42".to_owned(),
], ],
) )
.first_line(), .first_line(),
@@ -551,7 +527,7 @@ mod test {
category: Category::MailSystem, category: Category::MailSystem,
detail: Detail::One, detail: Detail::One,
}, },
vec![" ".to_string()], vec![" ".to_owned()],
) )
.first_line(), .first_line(),
Some(" ") Some(" ")
@@ -563,7 +539,7 @@ mod test {
category: Category::MailSystem, category: Category::MailSystem,
detail: Detail::One, detail: Detail::One,
}, },
vec![" ".to_string()], vec![" ".to_owned()],
) )
.first_line(), .first_line(),
Some(" ") Some(" ")
@@ -575,7 +551,7 @@ mod test {
category: Category::MailSystem, category: Category::MailSystem,
detail: Detail::One, detail: Detail::One,
}, },
vec!["".to_string()], vec!["".to_owned()],
) )
.first_line(), .first_line(),
Some("") Some("")

View File

@@ -7,7 +7,7 @@ use super::pool::sync_impl::Pool;
#[cfg(feature = "pool")] #[cfg(feature = "pool")]
use super::PoolConfig; use super::PoolConfig;
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"))] #[cfg(any(feature = "native-tls", feature = "rustls-tls", feature = "boring-tls"))]
use super::{Tls, TlsParameters, SUBMISSIONS_PORT, SUBMISSION_PORT}; use super::{Tls, TlsParameters, SUBMISSIONS_PORT, SUBMISSION_PORT};
use crate::{address::Envelope, Transport}; use crate::{address::Envelope, Transport};
@@ -45,8 +45,11 @@ impl SmtpTransport {
/// ///
/// Creates an encrypted transport over submissions port, using the provided domain /// Creates an encrypted transport over submissions port, using the provided domain
/// to validate TLS certificates. /// to validate TLS certificates.
#[cfg(any(feature = "native-tls", feature = "rustls-tls"))] #[cfg(any(feature = "native-tls", feature = "rustls-tls", feature = "boring-tls"))]
#[cfg_attr(docsrs, doc(cfg(any(feature = "native-tls", feature = "rustls-tls"))))] #[cfg_attr(
docsrs,
doc(cfg(any(feature = "native-tls", feature = "rustls-tls", feature = "boring-tls")))
)]
pub fn relay(relay: &str) -> Result<SmtpTransportBuilder, Error> { pub fn relay(relay: &str) -> Result<SmtpTransportBuilder, Error> {
let tls_parameters = TlsParameters::new(relay.into())?; let tls_parameters = TlsParameters::new(relay.into())?;
@@ -66,8 +69,11 @@ impl SmtpTransport {
/// ///
/// An error is returned if the connection can't be upgraded. No credentials /// An error is returned if the connection can't be upgraded. No credentials
/// or emails will be sent to the server, protecting from downgrade attacks. /// or emails will be sent to the server, protecting from downgrade attacks.
#[cfg(any(feature = "native-tls", feature = "rustls-tls"))] #[cfg(any(feature = "native-tls", feature = "rustls-tls", feature = "boring-tls"))]
#[cfg_attr(docsrs, doc(cfg(any(feature = "native-tls", feature = "rustls-tls"))))] #[cfg_attr(
docsrs,
doc(cfg(any(feature = "native-tls", feature = "rustls-tls", feature = "boring-tls")))
)]
pub fn starttls_relay(relay: &str) -> Result<SmtpTransportBuilder, Error> { pub fn starttls_relay(relay: &str) -> Result<SmtpTransportBuilder, Error> {
let tls_parameters = TlsParameters::new(relay.into())?; let tls_parameters = TlsParameters::new(relay.into())?;
@@ -166,8 +172,11 @@ impl SmtpTransportBuilder {
} }
/// Set the TLS settings to use /// Set the TLS settings to use
#[cfg(any(feature = "native-tls", feature = "rustls-tls"))] #[cfg(any(feature = "native-tls", feature = "rustls-tls", feature = "boring-tls"))]
#[cfg_attr(docsrs, doc(cfg(any(feature = "native-tls", feature = "rustls-tls"))))] #[cfg_attr(
docsrs,
doc(cfg(any(feature = "native-tls", feature = "rustls-tls", feature = "boring-tls")))
)]
pub fn tls(mut self, tls: Tls) -> Self { pub fn tls(mut self, tls: Tls) -> Self {
self.info.tls = tls; self.info.tls = tls;
self self
@@ -210,7 +219,7 @@ impl SmtpClient {
pub fn connection(&self) -> Result<SmtpConnection, Error> { pub fn connection(&self) -> Result<SmtpConnection, Error> {
#[allow(clippy::match_single_binding)] #[allow(clippy::match_single_binding)]
let tls_parameters = match self.info.tls { let tls_parameters = match self.info.tls {
#[cfg(any(feature = "native-tls", feature = "rustls-tls"))] #[cfg(any(feature = "native-tls", feature = "rustls-tls", feature = "boring-tls"))]
Tls::Wrapper(ref tls_parameters) => Some(tls_parameters), Tls::Wrapper(ref tls_parameters) => Some(tls_parameters),
_ => None, _ => None,
}; };
@@ -224,7 +233,7 @@ impl SmtpClient {
None, None,
)?; )?;
#[cfg(any(feature = "native-tls", feature = "rustls-tls"))] #[cfg(any(feature = "native-tls", feature = "rustls-tls", feature = "boring-tls"))]
match self.info.tls { match self.info.tls {
Tls::Opportunistic(ref tls_parameters) => { Tls::Opportunistic(ref tls_parameters) => {
if conn.can_starttls() { if conn.can_starttls() {

View File

@@ -41,7 +41,7 @@ mod tests {
] ]
.iter() .iter()
{ {
assert_eq!(format!("{}", XText(input)), expect.to_string()); assert_eq!(format!("{}", XText(input)), (*expect).to_owned());
} }
} }
} }

View File

@@ -53,6 +53,8 @@ use futures_util::lock::Mutex as FuturesMutex;
use crate::AsyncTransport; use crate::AsyncTransport;
use crate::{address::Envelope, Transport}; use crate::{address::Envelope, Transport};
/// An error returned by the stub transport
#[non_exhaustive]
#[derive(Debug, Copy, Clone)] #[derive(Debug, Copy, Clone)]
pub struct Error; pub struct Error;

View File

@@ -33,7 +33,7 @@ mod sync {
let result = sender.send(&email); let result = sender.send(&email);
let id = result.unwrap(); let id = result.unwrap();
let eml_file = temp_dir().join(format!("{}.eml", id)); let eml_file = temp_dir().join(format!("{id}.eml"));
let eml = read_to_string(&eml_file).unwrap(); let eml = read_to_string(&eml_file).unwrap();
assert_eq!( assert_eq!(
@@ -68,10 +68,10 @@ mod sync {
let result = sender.send(&email); let result = sender.send(&email);
let id = result.unwrap(); let id = result.unwrap();
let eml_file = temp_dir().join(format!("{}.eml", id)); let eml_file = temp_dir().join(format!("{id}.eml"));
let eml = read_to_string(&eml_file).unwrap(); let eml = read_to_string(&eml_file).unwrap();
let json_file = temp_dir().join(format!("{}.json", id)); let json_file = temp_dir().join(format!("{id}.json"));
let json = read_to_string(&json_file).unwrap(); let json = read_to_string(&json_file).unwrap();
assert_eq!( assert_eq!(
@@ -131,7 +131,7 @@ mod tokio_1 {
let result = sender.send(email).await; let result = sender.send(email).await;
let id = result.unwrap(); let id = result.unwrap();
let eml_file = temp_dir().join(format!("{}.eml", id)); let eml_file = temp_dir().join(format!("{id}.eml"));
let eml = read_to_string(&eml_file).unwrap(); let eml = read_to_string(&eml_file).unwrap();
assert_eq!( assert_eq!(
@@ -182,7 +182,7 @@ mod asyncstd_1 {
let result = sender.send(email).await; let result = sender.send(email).await;
let id = result.unwrap(); let id = result.unwrap();
let eml_file = temp_dir().join(format!("{}.eml", id)); let eml_file = temp_dir().join(format!("{id}.eml"));
let eml = read_to_string(&eml_file).unwrap(); let eml = read_to_string(&eml_file).unwrap();
assert_eq!( assert_eq!(

View File

@@ -15,7 +15,7 @@ mod sync {
.unwrap(); .unwrap();
let result = sender.send(&email); let result = sender.send(&email);
println!("{:?}", result); println!("{result:?}");
assert!(result.is_ok()); assert!(result.is_ok());
} }
} }
@@ -42,7 +42,7 @@ mod tokio_1 {
.unwrap(); .unwrap();
let result = sender.send(email).await; let result = sender.send(email).await;
println!("{:?}", result); println!("{result:?}");
assert!(result.is_ok()); assert!(result.is_ok());
} }
} }
@@ -68,7 +68,7 @@ mod asyncstd_1 {
.unwrap(); .unwrap();
let result = sender.send(email).await; let result = sender.send(email).await;
println!("{:?}", result); println!("{result:?}");
assert!(result.is_ok()); assert!(result.is_ok());
} }
} }

View File

@@ -18,7 +18,7 @@ mod sync {
sender_ok.send(&email).unwrap(); sender_ok.send(&email).unwrap();
sender_ko.send(&email).unwrap_err(); sender_ko.send(&email).unwrap_err();
let expected_messages = vec![( let expected_messages = [(
email.envelope().clone(), email.envelope().clone(),
String::from_utf8(email.formatted()).unwrap(), String::from_utf8(email.formatted()).unwrap(),
)]; )];
@@ -47,7 +47,7 @@ mod tokio_1 {
sender_ok.send(email.clone()).await.unwrap(); sender_ok.send(email.clone()).await.unwrap();
sender_ko.send(email.clone()).await.unwrap_err(); sender_ko.send(email.clone()).await.unwrap_err();
let expected_messages = vec![( let expected_messages = [(
email.envelope().clone(), email.envelope().clone(),
String::from_utf8(email.formatted()).unwrap(), String::from_utf8(email.formatted()).unwrap(),
)]; )];
@@ -75,7 +75,7 @@ mod asyncstd_1 {
sender_ok.send(email.clone()).await.unwrap(); sender_ok.send(email.clone()).await.unwrap();
sender_ko.send(email.clone()).await.unwrap_err(); sender_ko.send(email.clone()).await.unwrap_err();
let expected_messages = vec![( let expected_messages = [(
email.envelope().clone(), email.envelope().clone(),
String::from_utf8(email.formatted()).unwrap(), String::from_utf8(email.formatted()).unwrap(),
)]; )];