Compare commits

...

7 Commits

Author SHA1 Message Date
Paolo Barbolini
c3b5d760f8 wip 2024-10-22 22:49:25 +02:00
Paolo Barbolini
b6babbce00 Prepare v0.11.9 (#991) 2024-09-13 15:48:32 +02:00
Paolo Barbolini
c9895c52de readme: add fn main to getting started example (#990) 2024-09-13 15:41:46 +02:00
Paolo Barbolini
575492b9ed Bump rustls-native-certs to v0.8 (#992) 2024-09-13 15:41:18 +02:00
Paolo Barbolini
ad665cd01e chore: bump locked dependencies (#989)
And then downgrades:

cargo update -p clap --precise 4.3.24
cargo update -p clap_lex --precise 0.5.0
cargo update -p anstyle --precise 1.0.2
2024-09-13 02:37:04 +02:00
Arnaud de Bossoreille
e2ac5dadfb Fix parsing Mailbox with spaces (#986) 2024-09-12 17:26:43 +02:00
Max Breitenfeldt
1c6a348eb8 Enable accept_invalid_hostnames for rustls (#988)
With #977 dangerous_accept_invalid_hostnames is implemented for rustls. This add the config flag so that the feature can actually be used when rustls-tls is enabled.
2024-09-10 09:58:48 +02:00
13 changed files with 563 additions and 713 deletions

View File

@@ -112,12 +112,6 @@ jobs:
- name: Install dkimverify
run: sudo apt -y install python3-dkim
- name: Work around early dependencies MSRV bump
run: |
cargo update -p anstyle --precise 1.0.2
cargo update -p clap --precise 4.3.24
cargo update -p clap_lex --precise 0.5.0
- name: Test with no default features
run: cargo test --no-default-features

View File

@@ -1,3 +1,21 @@
<a name="v0.11.9"></a>
### v0.11.9 (2024-09-13)
#### Bug fixes
* Fix feature gate for `accept_invalid_hostnames` for rustls ([#988])
* Fix parsing `Mailbox` with trailing spaces ([#986])
#### Misc
* Bump `rustls-native-certs` to v0.8 ([#992])
* Make getting started example in readme complete ([#990])
[#988]: https://github.com/lettre/lettre/pull/988
[#986]: https://github.com/lettre/lettre/pull/986
[#990]: https://github.com/lettre/lettre/pull/990
[#992]: https://github.com/lettre/lettre/pull/992
<a name="v0.11.8"></a>
### v0.11.8 (2024-09-03)

1033
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@@ -1,7 +1,7 @@
[package]
name = "lettre"
# remember to update html_root_url and README.md (Cargo.toml example and deps.rs badge)
version = "0.11.8"
version = "0.11.9"
description = "Email client"
readme = "README.md"
homepage = "https://lettre.rs"
@@ -47,7 +47,7 @@ percent-encoding = { version = "2.3", optional = true }
native-tls = { version = "0.2.5", optional = true } # feature
rustls = { version = "0.23.5", default-features = false, features = ["ring", "logging", "std", "tls12"], optional = true }
rustls-pemfile = { version = "2", optional = true }
rustls-native-certs = { version = "0.7", optional = true }
rustls-native-certs = { version = "0.8", optional = true }
rustls-pki-types = { version = "1.7", optional = true }
webpki-roots = { version = "0.26", optional = true }
boring = { version = "4", optional = true }

View File

@@ -28,8 +28,8 @@
</div>
<div align="center">
<a href="https://deps.rs/crate/lettre/0.11.8">
<img src="https://deps.rs/crate/lettre/0.11.8/status.svg"
<a href="https://deps.rs/crate/lettre/0.11.9">
<img src="https://deps.rs/crate/lettre/0.11.9/status.svg"
alt="dependency status" />
</a>
</div>
@@ -71,27 +71,29 @@ use lettre::message::header::ContentType;
use lettre::transport::smtp::authentication::Credentials;
use lettre::{Message, SmtpTransport, Transport};
let email = Message::builder()
.from("NoBody <nobody@domain.tld>".parse().unwrap())
.reply_to("Yuin <yuin@domain.tld>".parse().unwrap())
.to("Hei <hei@domain.tld>".parse().unwrap())
.subject("Happy new year")
.header(ContentType::TEXT_PLAIN)
.body(String::from("Be happy!"))
.unwrap();
fn main() {
let email = Message::builder()
.from("NoBody <nobody@domain.tld>".parse().unwrap())
.reply_to("Yuin <yuin@domain.tld>".parse().unwrap())
.to("Hei <hei@domain.tld>".parse().unwrap())
.subject("Happy new year")
.header(ContentType::TEXT_PLAIN)
.body(String::from("Be happy!"))
.unwrap();
let creds = Credentials::new("smtp_username".to_owned(), "smtp_password".to_owned());
let creds = Credentials::new("smtp_username".to_owned(), "smtp_password".to_owned());
// Open a remote connection to gmail
let mailer = SmtpTransport::relay("smtp.gmail.com")
.unwrap()
.credentials(creds)
.build();
// Open a remote connection to gmail
let mailer = SmtpTransport::relay("smtp.gmail.com")
.unwrap()
.credentials(creds)
.build();
// Send the email
match mailer.send(&email) {
Ok(_) => println!("Email sent successfully!"),
Err(e) => panic!("Could not send email: {e:?}"),
// Send the email
match mailer.send(&email) {
Ok(_) => println!("Email sent successfully!"),
Err(e) => panic!("Could not send email: {e:?}"),
}
}
```

View File

@@ -109,7 +109,7 @@
//! [mime 0.3]: https://docs.rs/mime/0.3
//! [DKIM]: https://datatracker.ietf.org/doc/html/rfc6376
#![doc(html_root_url = "https://docs.rs/crate/lettre/0.11.8")]
#![doc(html_root_url = "https://docs.rs/crate/lettre/0.11.9")]
#![doc(html_favicon_url = "https://lettre.rs/favicon.ico")]
#![doc(html_logo_url = "https://avatars0.githubusercontent.com/u/15113230?v=4")]
#![forbid(unsafe_code)]

View File

@@ -1,11 +1,11 @@
use std::{mem, ops::Deref};
use std::{mem, ops::Deref, sync::Arc};
use crate::message::header::ContentTransferEncoding;
/// A [`Message`][super::Message] or [`SinglePart`][super::SinglePart] body that has already been encoded.
#[derive(Debug, Clone)]
pub struct Body {
buf: Vec<u8>,
buf: Arc<[u8]>,
encoding: ContentTransferEncoding,
}
@@ -39,7 +39,7 @@ impl Body {
let encoding = buf.encoding(false);
buf.encode_crlf();
Self::new_impl(buf.into(), encoding)
Self::new_impl(Vec::from(buf).into(), encoding)
}
/// Encode the supplied `buf`, using the provided `encoding`.
@@ -77,7 +77,7 @@ impl Body {
}
buf.encode_crlf();
Ok(Self::new_impl(buf.into(), encoding))
Ok(Self::new_impl(Vec::from(buf).into(), encoding))
}
/// Builds a new `Body` using a pre-encoded buffer.
@@ -87,11 +87,14 @@ impl Body {
/// `buf` shouldn't contain non-ascii characters, lines longer than 1000 characters or nul bytes.
#[inline]
pub fn dangerous_pre_encoded(buf: Vec<u8>, encoding: ContentTransferEncoding) -> Self {
Self { buf, encoding }
Self {
buf: buf.into(),
encoding,
}
}
/// Encodes the supplied `buf` using the provided `encoding`
fn new_impl(buf: Vec<u8>, encoding: ContentTransferEncoding) -> Self {
fn new_impl(buf: Arc<[u8]>, encoding: ContentTransferEncoding) -> Self {
match encoding {
ContentTransferEncoding::SevenBit
| ContentTransferEncoding::EightBit
@@ -133,7 +136,16 @@ impl Body {
/// Consumes `Body` and returns the inner `Vec<u8>`
#[inline]
#[deprecated(
note = "The inner memory is not stored into `Vec<u8>` anymore. Consider using `into_inner`"
)]
pub fn into_vec(self) -> Vec<u8> {
self.buf.to_vec()
}
/// Consumes `Body` and returns the inner `Arc<[u8]>`
#[inline]
pub fn into_inner(self) -> Arc<[u8]> {
self.buf
}
}

View File

@@ -18,6 +18,7 @@ pub use self::{
special::*,
textual::*,
};
use super::EmailFormat;
use crate::BoxError;
mod content;
@@ -154,6 +155,19 @@ impl Display for Headers {
}
}
impl EmailFormat for Headers {
fn format<'a>(&'a self, out: &mut impl Extend<Cow<'a, [u8]>>) {
for value in &self.headers {
out.extend([
Cow::Borrowed(value.name.as_bytes()),
Cow::Borrowed(b": "),
Cow::Borrowed(value.encoded_value.as_bytes()),
Cow::Borrowed(b"\r\n"),
]);
}
}
}
/// A possible error when converting a `HeaderName` from another type.
// comes from `http` crate
#[allow(missing_copy_implementations)]

View File

@@ -170,7 +170,9 @@ fn phrase() -> impl Parser<char, Vec<char>, Error = Cheap<char>> {
// mailbox = name-addr / addr-spec
pub(crate) fn mailbox() -> impl Parser<char, (Option<String>, (String, String)), Error = Cheap<char>>
{
choice((name_addr(), addr_spec().map(|addr| (None, addr)))).then_ignore(end())
choice((name_addr(), addr_spec().map(|addr| (None, addr))))
.padded()
.then_ignore(end())
}
// name-addr = [display-name] angle-addr

View File

@@ -556,6 +556,14 @@ mod test {
);
}
#[test]
fn parse_address_only_trim() {
assert_eq!(
" kayo@example.com ".parse(),
Ok(Mailbox::new(None, "kayo@example.com".parse().unwrap()))
);
}
#[test]
fn parse_address_with_name() {
assert_eq!(
@@ -567,6 +575,17 @@ mod test {
);
}
#[test]
fn parse_address_with_name_trim() {
assert_eq!(
" K. <kayo@example.com> ".parse(),
Ok(Mailbox::new(
Some("K.".into()),
"kayo@example.com".parse().unwrap()
))
);
}
#[test]
fn parse_address_with_empty_name() {
assert_eq!(
@@ -578,7 +597,7 @@ mod test {
#[test]
fn parse_address_with_empty_name_trim() {
assert_eq!(
" <kayo@example.com>".parse(),
" <kayo@example.com> ".parse(),
Ok(Mailbox::new(None, "kayo@example.com".parse().unwrap()))
);
}

View File

@@ -1,4 +1,4 @@
use std::{io::Write, iter::repeat_with};
use std::{borrow::Cow, iter::repeat_with, sync::Arc};
use mime::Mime;
@@ -28,7 +28,7 @@ impl Part {
}
impl EmailFormat for Part {
fn format(&self, out: &mut Vec<u8>) {
fn format<'a>(&'a self, out: &mut impl Extend<Cow<'a, [u8]>>) {
match self {
Part::Single(part) => part.format(out),
Part::Multi(part) => part.format(out),
@@ -71,7 +71,7 @@ impl SinglePartBuilder {
SinglePart {
headers: self.headers,
body: body.into_vec(),
body: body.into_inner(),
}
}
}
@@ -100,7 +100,7 @@ impl Default for SinglePartBuilder {
#[derive(Debug, Clone)]
pub struct SinglePart {
headers: Headers,
body: Vec<u8>,
body: Arc<[u8]>,
}
impl SinglePart {
@@ -138,24 +138,18 @@ impl SinglePart {
/// Get message content formatted for sending
pub fn formatted(&self) -> Vec<u8> {
let mut out = Vec::new();
self.format(&mut out);
out
}
/// Format only the signlepart body
fn format_body(&self, out: &mut Vec<u8>) {
out.extend_from_slice(&self.body);
out.extend_from_slice(b"\r\n");
self.format_to_vec()
}
}
impl EmailFormat for SinglePart {
fn format(&self, out: &mut Vec<u8>) {
write!(out, "{}", self.headers)
.expect("A Write implementation panicked while formatting headers");
out.extend_from_slice(b"\r\n");
self.format_body(out);
fn format<'a>(&'a self, out: &mut impl Extend<Cow<'a, [u8]>>) {
self.headers.format(out);
out.extend([
Cow::Borrowed("\r\n".as_bytes()),
Cow::Borrowed(&self.body),
Cow::Borrowed(b"\r\n"),
]);
}
}
@@ -384,33 +378,36 @@ impl MultiPart {
/// Get message content formatted for SMTP
pub fn formatted(&self) -> Vec<u8> {
let mut out = Vec::new();
self.format(&mut out);
out
self.format_to_vec()
}
/// Format only the multipart body
fn format_body(&self, out: &mut Vec<u8>) {
fn format_body<'a>(&'a self, out: &mut impl Extend<Cow<'a, [u8]>>) {
let boundary = self.boundary();
for part in &self.parts {
out.extend_from_slice(b"--");
out.extend_from_slice(boundary.as_bytes());
out.extend_from_slice(b"\r\n");
out.extend([
Cow::Borrowed("--".as_bytes()),
// FIXME: this clone shouldn't exist
Cow::Owned(boundary.clone().into()),
Cow::Borrowed("\r\n".as_bytes()),
]);
part.format(out);
}
out.extend_from_slice(b"--");
out.extend_from_slice(boundary.as_bytes());
out.extend_from_slice(b"--\r\n");
out.extend([
Cow::Borrowed("--".as_bytes()),
Cow::Owned(boundary.into()),
Cow::Borrowed("--\r\n".as_bytes()),
]);
}
}
impl EmailFormat for MultiPart {
fn format(&self, out: &mut Vec<u8>) {
write!(out, "{}", self.headers)
.expect("A Write implementation panicked while formatting headers");
out.extend_from_slice(b"\r\n");
fn format<'a>(&'a self, out: &mut impl Extend<Cow<'a, [u8]>>) {
self.headers.format(out);
out.extend([Cow::Borrowed("\r\n".as_bytes())]);
self.format_body(out);
}
}

View File

@@ -198,7 +198,7 @@
//! ```
//! </details>
use std::{io::Write, iter, time::SystemTime};
use std::{borrow::Cow, iter, sync::Arc, time::SystemTime};
pub use attachment::Attachment;
pub use body::{Body, IntoBody, MaybeString};
@@ -226,7 +226,23 @@ const DEFAULT_MESSAGE_ID_DOMAIN: &str = "localhost";
/// Something that can be formatted as an email message
trait EmailFormat {
// Use a writer?
fn format(&self, out: &mut Vec<u8>);
fn format<'a>(&'a self, out: &mut impl Extend<Cow<'a, [u8]>>);
fn format_to_vec(&self) -> Vec<u8> {
struct Formatter(Vec<u8>);
impl<'a> Extend<Cow<'a, [u8]>> for Formatter {
fn extend<T: IntoIterator<Item = Cow<'a, [u8]>>>(&mut self, iter: T) {
for chunk in iter {
self.0.extend_from_slice(&chunk);
}
}
}
let mut formatted = Formatter(Vec::new());
self.format(&mut formatted);
formatted.0
}
}
/// A builder for messages
@@ -454,7 +470,7 @@ impl MessageBuilder {
let body = body.into_body(maybe_encoding);
self.headers.set(body.encoding());
self.build(MessageBody::Raw(body.into_vec()))
self.build(MessageBody::Raw(body.into_inner()))
}
/// Create message using mime body ([`MultiPart`][self::MultiPart])
@@ -489,7 +505,7 @@ pub struct Message {
#[derive(Clone, Debug)]
enum MessageBody {
Mime(Part),
Raw(Vec<u8>),
Raw(Arc<[u8]>),
}
impl Message {
@@ -515,9 +531,7 @@ impl Message {
/// Get message content formatted for SMTP
pub fn formatted(&self) -> Vec<u8> {
let mut out = Vec::new();
self.format(&mut out);
out
self.format_to_vec()
}
#[cfg(feature = "dkim")]
@@ -593,15 +607,13 @@ impl Message {
}
impl EmailFormat for Message {
fn format(&self, out: &mut Vec<u8>) {
write!(out, "{}", self.headers)
.expect("A Write implementation panicked while formatting headers");
fn format<'a>(&'a self, out: &mut impl Extend<Cow<'a, [u8]>>) {
self.headers.format(out);
match &self.body {
MessageBody::Mime(p) => p.format(out),
MessageBody::Raw(r) => {
out.extend_from_slice(b"\r\n");
out.extend_from_slice(r)
out.extend([Cow::Borrowed("\r\n".as_bytes()), Cow::Borrowed(r)]);
}
}
}

View File

@@ -197,7 +197,7 @@ impl TlsParametersBuilder {
/// including those from other sites, are trusted.
///
/// This method introduces significant vulnerabilities to man-in-the-middle attacks.
#[cfg(any(feature = "native-tls", feature = "boring-tls"))]
#[cfg(any(feature = "native-tls", feature = "rustls-tls", feature = "boring-tls"))]
#[cfg_attr(
docsrs,
doc(cfg(any(feature = "native-tls", feature = "rustls-tls", feature = "boring-tls")))
@@ -390,11 +390,14 @@ impl TlsParametersBuilder {
#[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 (added, ignored) = store.add_parsable_certificates(native_certs);
let rustls_native_certs::CertificateResult { certs, errors, .. } =
rustls_native_certs::load_native_certs();
let errors_len = errors.len();
let (added, ignored) = store.add_parsable_certificates(certs);
#[cfg(feature = "tracing")]
tracing::debug!(
"loaded platform certs with {added} valid and {ignored} ignored (invalid) certs"
"loaded platform certs with {errors_len} failing to load, {added} valid and {ignored} ignored (invalid) certs"
);
Ok(())
}