| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
use goingson_core::EmailAccount; |
| 8 |
use lettre::{ |
| 9 |
message::{ |
| 10 |
header::ContentType, |
| 11 |
Attachment, MultiPart, SinglePart, |
| 12 |
}, |
| 13 |
transport::smtp::authentication::{Credentials, Mechanism}, |
| 14 |
AsyncSmtpTransport, AsyncTransport, Message, Tokio1Executor, |
| 15 |
}; |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
fn format_flowed(body: &str) -> String { |
| 24 |
const MAX_LINE: usize = 72; |
| 25 |
let mut out = String::with_capacity(body.len() + body.len() / 8); |
| 26 |
|
| 27 |
for line in body.split('\n') { |
| 28 |
|
| 29 |
if line == "-- " { |
| 30 |
out.push_str(line); |
| 31 |
out.push('\n'); |
| 32 |
continue; |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
if line.starts_with('>') { |
| 37 |
out.push(' '); |
| 38 |
out.push_str(line); |
| 39 |
out.push('\n'); |
| 40 |
continue; |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
let needs_stuffing = line.starts_with("From ") || line.starts_with(' '); |
| 45 |
|
| 46 |
|
| 47 |
let effective_len = line.len() + if needs_stuffing { 1 } else { 0 }; |
| 48 |
if effective_len <= MAX_LINE || line.trim().is_empty() { |
| 49 |
if needs_stuffing { |
| 50 |
out.push(' '); |
| 51 |
} |
| 52 |
out.push_str(line); |
| 53 |
out.push('\n'); |
| 54 |
continue; |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
let prefix = if needs_stuffing { " " } else { "" }; |
| 59 |
let mut remaining = line; |
| 60 |
|
| 61 |
while !remaining.is_empty() { |
| 62 |
let budget = MAX_LINE - prefix.len(); |
| 63 |
|
| 64 |
if remaining.len() <= budget { |
| 65 |
|
| 66 |
out.push_str(prefix); |
| 67 |
out.push_str(remaining); |
| 68 |
out.push('\n'); |
| 69 |
break; |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
let mut boundary = budget; |
| 76 |
while boundary > 0 && !remaining.is_char_boundary(boundary) { |
| 77 |
boundary -= 1; |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
if boundary == 0 { |
| 83 |
boundary = remaining |
| 84 |
.char_indices() |
| 85 |
.nth(1) |
| 86 |
.map_or(remaining.len(), |(i, _)| i); |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
let break_at = remaining[..boundary] |
| 93 |
.rfind(' ') |
| 94 |
.map(|i| i + 1) |
| 95 |
.unwrap_or(boundary); |
| 96 |
|
| 97 |
let (chunk, rest) = remaining.split_at(break_at); |
| 98 |
out.push_str(prefix); |
| 99 |
out.push_str(chunk); |
| 100 |
|
| 101 |
if !chunk.ends_with(' ') { |
| 102 |
out.push(' '); |
| 103 |
} |
| 104 |
out.push('\n'); |
| 105 |
remaining = rest; |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
if !body.ends_with('\n') && out.ends_with('\n') { |
| 111 |
out.pop(); |
| 112 |
} |
| 113 |
|
| 114 |
out |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
pub struct AttachmentFile { |
| 119 |
pub filename: String, |
| 120 |
pub mime_type: String, |
| 121 |
pub data: Vec<u8>, |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
pub struct SendParams<'a> { |
| 126 |
pub to: &'a str, |
| 127 |
pub cc: Option<&'a str>, |
| 128 |
pub bcc: Option<&'a str>, |
| 129 |
pub subject: &'a str, |
| 130 |
pub body: &'a str, |
| 131 |
pub in_reply_to: Option<&'a str>, |
| 132 |
pub references: Option<&'a str>, |
| 133 |
pub attachments: Vec<AttachmentFile>, |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
#[derive(Debug, Clone)] |
| 138 |
pub enum SmtpAuth { |
| 139 |
|
| 140 |
Password { username: String, password: String }, |
| 141 |
|
| 142 |
XOAuth2 { |
| 143 |
email: String, |
| 144 |
access_token: String, |
| 145 |
}, |
| 146 |
} |
| 147 |
|
| 148 |
pub struct SmtpClient { |
| 149 |
server: String, |
| 150 |
port: u16, |
| 151 |
auth: SmtpAuth, |
| 152 |
from_address: String, |
| 153 |
} |
| 154 |
|
| 155 |
impl SmtpClient { |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
pub fn with_password(account: &EmailAccount, password: &str) -> Self { |
| 160 |
Self { |
| 161 |
server: account.smtp_server.clone(), |
| 162 |
port: account.smtp_port as u16, |
| 163 |
auth: SmtpAuth::Password { |
| 164 |
username: account.username.clone(), |
| 165 |
password: password.to_string(), |
| 166 |
}, |
| 167 |
from_address: account.email_address.clone(), |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
pub fn with_oauth( |
| 173 |
server: &str, |
| 174 |
port: u16, |
| 175 |
email: &str, |
| 176 |
access_token: &str, |
| 177 |
) -> Self { |
| 178 |
Self { |
| 179 |
server: server.to_string(), |
| 180 |
port, |
| 181 |
auth: SmtpAuth::XOAuth2 { |
| 182 |
email: email.to_string(), |
| 183 |
access_token: access_token.to_string(), |
| 184 |
}, |
| 185 |
from_address: email.to_string(), |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
fn build_mailer(&self) -> Result<AsyncSmtpTransport<Tokio1Executor>, String> { |
| 197 |
let builder = AsyncSmtpTransport::<Tokio1Executor>::starttls_relay(&self.server) |
| 198 |
.map_err(|e| format!("SMTP relay error: {}", e))? |
| 199 |
.port(self.port); |
| 200 |
|
| 201 |
let mailer = match &self.auth { |
| 202 |
SmtpAuth::Password { username, password } => builder |
| 203 |
.credentials(Credentials::new(username.clone(), password.clone())) |
| 204 |
.build(), |
| 205 |
SmtpAuth::XOAuth2 { email, access_token } => builder |
| 206 |
|
| 207 |
|
| 208 |
.credentials(Credentials::new(email.clone(), access_token.clone())) |
| 209 |
.authentication(vec![Mechanism::Xoauth2]) |
| 210 |
.build(), |
| 211 |
}; |
| 212 |
Ok(mailer) |
| 213 |
} |
| 214 |
|
| 215 |
#[tracing::instrument(skip_all)] |
| 216 |
pub async fn send_message(&self, params: &SendParams<'_>) -> Result<String, String> { |
| 217 |
|
| 218 |
let message_id = format!( |
| 219 |
"<{}.{}@{}>", |
| 220 |
uuid::Uuid::new_v4(), |
| 221 |
chrono::Utc::now().timestamp(), |
| 222 |
self.server |
| 223 |
); |
| 224 |
|
| 225 |
let mut builder = Message::builder() |
| 226 |
.message_id(Some(message_id.clone())) |
| 227 |
.from( |
| 228 |
self.from_address |
| 229 |
.parse() |
| 230 |
.map_err(|e| format!("Invalid from address: {}", e))?, |
| 231 |
) |
| 232 |
.subject(params.subject); |
| 233 |
|
| 234 |
|
| 235 |
for addr in params.to.split(',').map(str::trim).filter(|a| !a.is_empty()) { |
| 236 |
builder = builder.to(addr.parse().map_err(|e| format!("Invalid to address '{}': {}", addr, e))?); |
| 237 |
} |
| 238 |
|
| 239 |
|
| 240 |
if let Some(cc) = params.cc { |
| 241 |
for addr in cc.split(',').map(str::trim).filter(|a| !a.is_empty()) { |
| 242 |
builder = builder.cc(addr.parse().map_err(|e| format!("Invalid CC address '{}': {}", addr, e))?); |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
if let Some(bcc) = params.bcc { |
| 248 |
for addr in bcc.split(',').map(str::trim).filter(|a| !a.is_empty()) { |
| 249 |
builder = builder.bcc(addr.parse().map_err(|e| format!("Invalid BCC address '{}': {}", addr, e))?); |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
if let Some(irt) = params.in_reply_to { |
| 254 |
builder = builder.in_reply_to(irt.to_string()); |
| 255 |
} |
| 256 |
if let Some(refs) = params.references { |
| 257 |
builder = builder.references(refs.to_string()); |
| 258 |
} |
| 259 |
|
| 260 |
let flowed_body = format_flowed(params.body); |
| 261 |
let flowed_ct = ContentType::parse("text/plain; charset=UTF-8; format=flowed") |
| 262 |
.unwrap_or(ContentType::TEXT_PLAIN); |
| 263 |
|
| 264 |
let email = if params.attachments.is_empty() { |
| 265 |
builder |
| 266 |
.header(flowed_ct) |
| 267 |
.body(flowed_body) |
| 268 |
.map_err(|e| format!("Failed to build email: {}", e))? |
| 269 |
} else { |
| 270 |
let body_part = SinglePart::builder() |
| 271 |
.content_type(flowed_ct) |
| 272 |
.body(flowed_body); |
| 273 |
let mut multipart = MultiPart::mixed().singlepart(body_part); |
| 274 |
|
| 275 |
for file in ¶ms.attachments { |
| 276 |
let content_type = file.mime_type.parse::<ContentType>() |
| 277 |
.or_else(|_| "application/octet-stream".parse::<ContentType>()) |
| 278 |
.map_err(|e| format!("Invalid attachment content type: {}", e))?; |
| 279 |
let attachment = Attachment::new(file.filename.clone()) |
| 280 |
.body(file.data.clone(), content_type); |
| 281 |
multipart = multipart.singlepart(attachment); |
| 282 |
} |
| 283 |
|
| 284 |
builder |
| 285 |
.multipart(multipart) |
| 286 |
.map_err(|e| format!("Failed to build email: {}", e))? |
| 287 |
}; |
| 288 |
|
| 289 |
let mailer = self.build_mailer()?; |
| 290 |
|
| 291 |
mailer |
| 292 |
.send(email) |
| 293 |
.await |
| 294 |
.map_err(|e| format!("Failed to send email: {}", e))?; |
| 295 |
|
| 296 |
Ok(message_id) |
| 297 |
} |
| 298 |
|
| 299 |
#[tracing::instrument(skip_all)] |
| 300 |
pub async fn test_connection(&self) -> Result<(), String> { |
| 301 |
let mailer = self.build_mailer()?; |
| 302 |
|
| 303 |
mailer |
| 304 |
.test_connection() |
| 305 |
.await |
| 306 |
.map_err(|e| format!("SMTP connection test failed: {}", e))?; |
| 307 |
|
| 308 |
Ok(()) |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
#[cfg(test)] |
| 313 |
mod tests { |
| 314 |
use super::format_flowed; |
| 315 |
|
| 316 |
#[test] |
| 317 |
fn short_ascii_line_unchanged() { |
| 318 |
assert_eq!(format_flowed("hello world"), "hello world"); |
| 319 |
} |
| 320 |
|
| 321 |
#[test] |
| 322 |
fn signature_separator_preserved() { |
| 323 |
assert_eq!(format_flowed("body\n-- \nsig"), "body\n-- \nsig"); |
| 324 |
} |
| 325 |
|
| 326 |
#[test] |
| 327 |
fn long_ascii_line_wraps_with_soft_break() { |
| 328 |
let line = "word ".repeat(20); |
| 329 |
let out = format_flowed(line.trim_end()); |
| 330 |
|
| 331 |
|
| 332 |
for seg in out.split('\n') { |
| 333 |
assert!(seg.len() <= 73, "segment too long: {:?}", seg); |
| 334 |
} |
| 335 |
} |
| 336 |
|
| 337 |
#[test] |
| 338 |
fn long_multibyte_line_does_not_panic() { |
| 339 |
|
| 340 |
|
| 341 |
let line = "の".repeat(60); |
| 342 |
let out = format_flowed(&line); |
| 343 |
|
| 344 |
|
| 345 |
let stripped: String = out.chars().filter(|c| *c != '\n' && *c != ' ').collect(); |
| 346 |
assert_eq!(stripped, line); |
| 347 |
} |
| 348 |
|
| 349 |
#[test] |
| 350 |
fn long_emoji_line_does_not_panic() { |
| 351 |
|
| 352 |
let line = "😀".repeat(40); |
| 353 |
let out = format_flowed(&line); |
| 354 |
let stripped: String = out.chars().filter(|c| *c != '\n' && *c != ' ').collect(); |
| 355 |
assert_eq!(stripped, line); |
| 356 |
} |
| 357 |
|
| 358 |
#[test] |
| 359 |
fn mixed_multibyte_with_spaces_word_breaks() { |
| 360 |
let line = format!("{} {} {}", "あ".repeat(30), "い".repeat(30), "う".repeat(30)); |
| 361 |
let out = format_flowed(&line); |
| 362 |
|
| 363 |
|
| 364 |
for seg in out.split('\n') { |
| 365 |
assert!(seg.len() <= 74, "segment too long: {} bytes", seg.len()); |
| 366 |
} |
| 367 |
|
| 368 |
let stripped: String = out.replace('\n', ""); |
| 369 |
assert!(stripped.contains(&"あ".repeat(24))); |
| 370 |
} |
| 371 |
} |
| 372 |
|