use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[cfg_attr(feature = "server", derive(utoipa::ToSchema))]
pub struct SendEmailRequest {
pub subject: String,
pub body: String,
pub to: Vec<String>,
#[serde(default)]
pub cc: Vec<String>,
#[serde(default)]
pub attachments: Vec<Attachment>,
#[serde(default)]
pub from: Option<String>,
#[serde(default)]
pub html: bool,
}
impl SendEmailRequest {
pub fn new<S, B, T, I>(subject: S, body: B, to: I) -> Self
where
S: Into<String>,
B: Into<String>,
T: Into<String>,
I: IntoIterator<Item = T>,
{
Self {
subject: subject.into(),
body: body.into(),
to: to.into_iter().map(Into::into).collect(),
..Default::default()
}
}
pub fn cc<T, I>(mut self, cc: I) -> Self
where
T: Into<String>,
I: IntoIterator<Item = T>,
{
self.cc = cc.into_iter().map(Into::into).collect();
self
}
pub fn from(mut self, from: impl Into<String>) -> Self {
self.from = Some(from.into());
self
}
pub fn html(mut self, html: bool) -> Self {
self.html = html;
self
}
pub fn attach(mut self, attachment: Attachment) -> Self {
self.attachments.push(attachment);
self
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "server", derive(utoipa::ToSchema))]
pub struct Attachment {
pub filename: String,
pub content_type: String,
pub content: String,
}
impl Attachment {
pub fn new(
filename: impl Into<String>,
content_type: impl Into<String>,
content: impl Into<String>,
) -> Self {
Self {
filename: filename.into(),
content_type: content_type.into(),
content: content.into(),
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "server", derive(utoipa::ToSchema))]
pub struct SendEmailResponse {
pub status: String,
pub message: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "server", derive(utoipa::ToSchema))]
pub struct ErrorResponse {
pub error: String,
}
#[derive(Debug)]
pub enum Error {
NotConfigured,
InvalidUrl(String),
#[cfg(feature = "client")]
Http(reqwest::Error),
Status { status: u16, message: String },
}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::NotConfigured => write!(
f,
"mailman is not configured; call mailman::setup() or set MAILMAN_URL and MAILMAN_API_TOKEN"
),
Self::InvalidUrl(s) => write!(f, "invalid mailman URL: {s}"),
#[cfg(feature = "client")]
Self::Http(e) => write!(f, "http error: {e}"),
Self::Status { status, message } => {
if message.is_empty() {
write!(f, "mailman server returned {status}")
} else {
write!(f, "mailman server returned {status}: {message}")
}
}
}
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
#[cfg(feature = "client")]
Self::Http(e) => Some(e),
_ => None,
}
}
}
#[cfg(feature = "client")]
mod client;
#[cfg(feature = "client")]
pub use client::{Client, configured_url, from_env, send, setup};