Files
imphnen-backend-service/imphnen-email/src/service.rs
T
maulanasdqnandClaude Sonnet 4.6 331a4a4e88 feat: v0.3.0 — standardize codebase, centralize infra, merge QR into CMS
- Enforce axum best practices across all 13 workspace crates
  (max 200 LOC/file, no comments, no unwrap, clean architecture)
- Fix domain→infrastructure dependency inversions in imphnen-iam and imphnen-dimentorin
- Extract imphnen-storage (MinIO) and imphnen-email (Lettre) as standalone crates
- Centralize all config in ENV struct: CDN_URL, CORS_ALLOWED_ORIGINS
- Centralize SMTP through imphnen-email; remove dead HackathonConfig
- Centralize database: QR crate now shares main DB pool (single DATABASE_URL)
- Rename QR users table to qr_users to avoid collision with main users table
- Merge imphnen-qr into imphnen-cms/src/qr (13 crates, down from 14)
- Restructure imphnen-hackathon flat modules into clean architecture
- Remove all stale env vars from .env.example (SurrealDB, QR_JWT, Hackathon infra)
- Fix Dockerfile to include all current workspace crates
- Bump all crate versions 0.2.0 → 0.3.0

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-02 22:29:08 +07:00

49 lines
1.3 KiB
Rust

use imphnen_libs::{ENV, Env};
use lettre::message::Mailbox;
use lettre::transport::smtp::authentication::Credentials;
use lettre::{Message, SmtpTransport, Transport};
use std::error::Error;
use crate::error::EmailError;
pub fn send_email(
to: &str,
subject: &str,
body: &str,
) -> Result<(), Box<dyn Error>> {
let env = &ENV;
let message = build_message(to, subject, body, env)?;
let mailer = build_transport(env)?;
mailer.send(&message).map_err(|e| {
tracing::error!("Failed to send email to {}: {}", to, e);
Box::new(EmailError::Transport(e.to_string())) as Box<dyn Error>
})?;
tracing::info!("Email sent to: {}", to);
Ok(())
}
fn build_message(
to: &str,
subject: &str,
body: &str,
env: &Env,
) -> Result<Message, Box<dyn Error>> {
let sender_name = env.smtp_name.replace("-", " ");
Message::builder()
.from(Mailbox::new(Some(sender_name), env.smtp_email.parse()?))
.to(to.parse()?)
.subject(subject)
.body(body.to_string())
.map_err(|e| Box::new(EmailError::MessageBuild(e.to_string())) as Box<dyn Error>)
}
fn build_transport(env: &Env) -> Result<SmtpTransport, Box<dyn Error>> {
let credentials =
Credentials::new(env.smtp_email.clone(), env.smtp_password.replace("-", " "));
Ok(
SmtpTransport::relay(&env.smtp_host)?
.credentials(credentials)
.build(),
)
}