async_mailer_core/lib.rs
1//! Core trait for `async-mailer`. Use [`async-mailer`](https://docs.rs/async-mailer/latest/async_mailer/) instead.
2use std::{fmt::Debug, sync::Arc};
3
4pub use async_trait::async_trait;
5
6pub use mail_send;
7use mail_send::smtp::message::Message;
8
9// == Mailer ==
10
11/// Statically typed [`Mailer`], to be used in `impl Mailer` or `<M: Mailer>` bounds.
12///
13/// The `async-mailer` crate exports Microsoft Outlook and SMTP mailers implementing the [`Mailer`] and [`DynMailer`] traits.
14#[async_trait]
15pub trait Mailer: Debug + Send + Sync {
16 type Error;
17
18 /// Send a [`Message`] using the [`Mailer`] implementation.
19 ///
20 /// # Errors
21 ///
22 /// Returns [`Self::Error`] in case sending the mail fails.
23 ///
24 /// Concrete errors vary by [`Mailer`] trait implementation.
25 /// ([Outlook](https://docs.rs/async-mailer/latest/async_mailer/struct.OutlookMailer.html#impl-Mailer-for-OutlookMailer),
26 /// [SMTP](https://docs.rs/async-mailer/latest/async_mailer/struct.SmtpMailer.html#impl-Mailer-for-SmtpMailer))
27 async fn send_mail(&self, message: Message<'_>) -> Result<(), Self::Error>;
28}
29
30// == DynMailer ==
31
32/// Type-erased mailer error, for use of [`DynMailer`] as trait object.
33pub type DynMailerError = Box<dyn std::error::Error + Send + Sync + 'static>;
34
35/// Object-safe [`DynMailer`] trait, usable as `&DynMailer`, [`ArcMailer`] (`Arc<dyn DynMailer>`) or [`BoxMailer`] (`Box<dyn DynMailer>`).
36///
37/// The `async-mailer` crate exports Microsoft Outlook and SMTP mailers implementing the [`DynMailer`] and [`Mailer`] traits.
38#[async_trait]
39pub trait DynMailer: Debug + Send + Sync {
40 /// Send a [`Message`] using the [`DynMailer`] implementation.
41 ///
42 /// # Errors
43 ///
44 /// Returns a boxed, type-erased [`DynMailerError`] in case sending the mail fails.
45 ///
46 /// Concrete errors vary by [`DynMailer`] trait implementation.
47 /// ([Outlook](https://docs.rs/async-mailer/latest/async_mailer/struct.OutlookMailer.html#impl-DynMailer-for-OutlookMailer),
48 /// [SMTP](https://docs.rs/async-mailer/latest/async_mailer/struct.SmtpMailer.html#impl-DynMailer-for-SmtpMailer))
49 async fn send_mail(&self, message: Message<'_>) -> Result<(), DynMailerError>;
50}
51
52/// Boxed dyn [`DynMailer`]
53pub type BoxMailer = Box<dyn DynMailer>;
54
55/// Arc-wrapped dyn [`DynMailer`]
56pub type ArcMailer = Arc<dyn DynMailer>;
57
58pub mod util {
59 use super::Message;
60
61 #[cfg(feature = "tracing")]
62 /// Extract recipient addresses for tracing log output.
63 pub fn format_recipient_addresses(message: &Message<'_>) -> String {
64 let recipient_addresses = message
65 .rcpt_to
66 .iter()
67 .map(|address| address.email.to_string())
68 .collect::<Vec<String>>()
69 .join(", ");
70
71 recipient_addresses
72 }
73}