#![forbid(unsafe_code)]
#![deny(missing_docs)]
use std::fmt;
#[cfg(feature = "auth")]
pub mod auth;
#[cfg(feature = "jobs")]
mod later;
mod mailer;
mod message;
#[cfg(feature = "preview")]
mod preview;
mod sendgrid;
mod smtp;
mod template;
mod testing;
#[cfg(feature = "auth")]
pub use auth::AccountMails;
#[cfg(feature = "jobs")]
pub use later::{DeliverMail, deliver_later, deliver_later_in, register_mail_jobs};
pub use mailer::{
MailError, Mailer, MailerEnv, MailerService, Sent, deliver, mailer_env, provide_mailer,
};
pub use message::{Attachment, InlinePart, Mailbox, Message, MessageBuilder};
#[cfg(feature = "preview")]
pub use preview::{MailPreviews, PREVIEWS_PATH};
pub use sendgrid::{SendGridConfig, SendGridMailer};
pub use smtp::{SmtpConfig, SmtpMailer, SmtpSecurity};
pub use template::{RenderedMail, TemplateError, Templates};
pub use testing::TestMailbox;
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Address {
addr: String,
at: usize,
}
impl Address {
pub const MAX_LEN: usize = 254;
pub const MAX_LOCAL_LEN: usize = 64;
pub fn parse(addr: impl Into<String>) -> Result<Self, AddressError> {
let addr = addr.into();
if addr.chars().any(|c| c.is_whitespace() || c.is_control()) {
return Err(AddressError::Whitespace);
}
let (local, domain) = addr.split_once('@').ok_or(AddressError::MissingAt)?;
if local.is_empty() {
return Err(AddressError::EmptyLocal);
}
if local.len() > Self::MAX_LOCAL_LEN {
return Err(AddressError::LocalTooLong);
}
if domain.contains('@') || domain.split('.').any(str::is_empty) {
return Err(AddressError::InvalidDomain);
}
if addr.len() > Self::MAX_LEN {
return Err(AddressError::TooLong);
}
let at = local.len();
Ok(Address { addr, at })
}
pub fn local(&self) -> &str {
&self.addr[..self.at]
}
pub fn domain(&self) -> &str {
&self.addr[self.at + 1..]
}
pub fn as_str(&self) -> &str {
&self.addr
}
}
impl fmt::Display for Address {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.addr)
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum AddressError {
Whitespace,
MissingAt,
EmptyLocal,
LocalTooLong,
InvalidDomain,
TooLong,
}
impl fmt::Display for AddressError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
AddressError::Whitespace => "mail address contains whitespace or a control character",
AddressError::MissingAt => "mail address has no `@`",
AddressError::EmptyLocal => "mail address has nothing before the `@`",
AddressError::LocalTooLong => "mail address local part is longer than 64 bytes",
AddressError::InvalidDomain => "mail address domain is not a dot-separated host name",
AddressError::TooLong => "mail address is longer than 254 bytes",
})
}
}
impl std::error::Error for AddressError {}
#[cfg(all(doctest, feature = "preview", feature = "jobs"))]
#[doc = include_str!("../../id_effect/book/src/part6/ch31-03-mail.md")]
struct BookMailDoctests;