Invitation-only sign-up, invitation e-mails and waitlist
- SIGNUP_MODE=invite (default): a new account needs an open invitation for its address, or the invitation link (then any Google account will do) - admin page "Meghívók": invite many addresses at once, personal message, resend (max 3, 24 h apart), revoke, copy link, mail preview and test copy, waitlist - invitation e-mail in HTML and plain text; Message-ID on the sender's domain, List-Unsubscribe with one-click POST, opt-out list (hashes only) - e-mail texts moved to a shared crate (crates/email-templates) - login page shows the invitation; waitlist for uninvited visitors; unsubscribe page; privacy policy updated - docs/levelkuldes.md: Brevo, DNS (SPF, DKIM, DMARC) and the SMTP setup Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012vLHzh5qUhFtmJhzkcTbXL
This commit is contained in:
@@ -6,6 +6,7 @@
|
||||
|
||||
use std::time::Duration;
|
||||
|
||||
use lettre::message::header::{Header, HeaderName, HeaderValue};
|
||||
use lettre::message::{Mailbox, MultiPart};
|
||||
use lettre::{AsyncSmtpTransport, AsyncTransport, Message, Tokio1Executor};
|
||||
use serde_json::Value;
|
||||
@@ -24,7 +25,8 @@ pub enum Mailer {
|
||||
/// Collects messages instead of sending; fails for addresses listed in `fail`.
|
||||
#[cfg(test)]
|
||||
Memory {
|
||||
sent: std::sync::Mutex<Vec<(String, Rendered)>>,
|
||||
/// Recipient, rendered mail and the whole message as sent.
|
||||
sent: std::sync::Mutex<Vec<(String, Rendered, String)>>,
|
||||
fail: Vec<(String, bool)>,
|
||||
},
|
||||
}
|
||||
@@ -58,12 +60,19 @@ impl Mailer {
|
||||
Ok(Mailer::Smtp(transport))
|
||||
}
|
||||
|
||||
async fn send(&self, from: &Mailbox, to: &str, mail: &Rendered) -> Result<(), SendError> {
|
||||
async fn send(&self, id: Uuid, from: &Mailbox, to: &str, mail: &Rendered) -> Result<(), SendError> {
|
||||
let to_mailbox: Mailbox = to.parse().map_err(SendError::permanent)?;
|
||||
let message = Message::builder()
|
||||
// A Message-ID on our own domain (lettre would use the container's
|
||||
// host name, which spam filters dislike); the same on every retry.
|
||||
let mut builder = Message::builder()
|
||||
.message_id(Some(format!("<{id}@{}>", from.email.domain())))
|
||||
.from(from.clone())
|
||||
.to(to_mailbox)
|
||||
.subject(&mail.subject)
|
||||
.subject(&mail.subject);
|
||||
if let Some(url) = &mail.unsubscribe_post_url {
|
||||
builder = builder.header(ListUnsubscribe(url.clone())).header(ListUnsubscribePost);
|
||||
}
|
||||
let message = builder
|
||||
.multipart(MultiPart::alternative_plain_html(mail.text.clone(), mail.html.clone()))
|
||||
.map_err(SendError::permanent)?;
|
||||
match self {
|
||||
@@ -77,13 +86,46 @@ impl Mailer {
|
||||
if let Some((_, permanent)) = fail.iter().find(|(addr, _)| addr == to) {
|
||||
return Err(SendError { permanent: *permanent, message: "test failure".into() });
|
||||
}
|
||||
sent.lock().unwrap().push((to.to_owned(), mail.clone()));
|
||||
let raw = String::from_utf8_lossy(&message.formatted()).into_owned();
|
||||
sent.lock().unwrap().push((to.to_owned(), mail.clone(), raw));
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// `List-Unsubscribe: <url>`: mail clients show an unsubscribe button.
|
||||
#[derive(Clone)]
|
||||
struct ListUnsubscribe(String);
|
||||
|
||||
impl Header for ListUnsubscribe {
|
||||
fn name() -> HeaderName {
|
||||
HeaderName::new_from_ascii_str("List-Unsubscribe")
|
||||
}
|
||||
fn parse(s: &str) -> Result<Self, Box<dyn std::error::Error + Send + Sync>> {
|
||||
Ok(Self(s.trim().trim_start_matches('<').trim_end_matches('>').to_owned()))
|
||||
}
|
||||
fn display(&self) -> HeaderValue {
|
||||
HeaderValue::new(Self::name(), format!("<{}>", self.0))
|
||||
}
|
||||
}
|
||||
|
||||
/// `List-Unsubscribe-Post`: the button unsubscribes with one POST (RFC 8058).
|
||||
#[derive(Clone)]
|
||||
struct ListUnsubscribePost;
|
||||
|
||||
impl Header for ListUnsubscribePost {
|
||||
fn name() -> HeaderName {
|
||||
HeaderName::new_from_ascii_str("List-Unsubscribe-Post")
|
||||
}
|
||||
fn parse(_: &str) -> Result<Self, Box<dyn std::error::Error + Send + Sync>> {
|
||||
Ok(Self)
|
||||
}
|
||||
fn display(&self) -> HeaderValue {
|
||||
HeaderValue::new(Self::name(), "List-Unsubscribe=One-Click".into())
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Delivery {
|
||||
pub db: PgPool,
|
||||
pub mailer: Mailer,
|
||||
@@ -127,7 +169,7 @@ impl Delivery {
|
||||
for row in &rows {
|
||||
let attempts = row.attempts + 1;
|
||||
let result = match templates::render(&row.template, &row.locale, &row.payload) {
|
||||
Ok(mail) => self.mailer.send(&self.from, &row.to_email, &mail).await,
|
||||
Ok(mail) => self.mailer.send(row.id, &self.from, &row.to_email, &mail).await,
|
||||
Err(e) => Err(SendError::permanent(e)),
|
||||
};
|
||||
match result {
|
||||
|
||||
Reference in New Issue
Block a user