Adds mailer library

This commit is contained in:
2026-07-26 10:55:59 +02:00
parent 7f9702fed7
commit f338a9d964
4 changed files with 43 additions and 4 deletions
+1
View File
@@ -12,3 +12,4 @@ diesel = { version = "2.3.2", features = ["postgres", "uuid", "chrono", "r2d2"]
chrono = "0.4.42"
log = "0.4.28"
fern = "0.7.1"
lettre = "0.11"
@@ -8,3 +8,18 @@ CREATE TABLE IF NOT EXISTS actions
created_at TIMESTAMP NOT NULL DEFAULT now(),
updated_at TIMESTAMP NOT NULL DEFAULT now()
);
CREATE TABLE IF NOT EXISTS timed_emails
(
id UUID PRIMARY KEY NOT NULL UNIQUE DEFAULT gen_random_uuid(),
name TEXT NOT NULL,
recipient TEXT NOT NULL,
subject TEXT NOT NULL,
body TEXT NOT NULL,
interval INT NOT NULL,
heartbeat TIMESTAMP NOT NULL,
expires TIMESTAMP NOT NULL,
fired BOOLEAN NOT NULL DEFAULT FALSE,
created_at TIMESTAMP NOT NULL DEFAULT now(),
updated_at TIMESTAMP NOT NULL DEFAULT now()
)
+19 -2
View File
@@ -12,8 +12,10 @@ mod sh;
use crate::api::action;
use auth::CORS;
use chrono::Local;
use diesel::r2d2::{ConnectionManager, Pool};
use diesel::PgConnection;
use diesel::r2d2::{ConnectionManager, Pool};
use lettre::SmtpTransport;
use lettre::transport::smtp::authentication::Credentials;
use models::AppState;
use rocket::State;
use std::env;
@@ -63,7 +65,22 @@ fn rocket() -> _ {
.build(manager)
.expect("Failed to create pool");
let app_data = AppState::new(pool);
let smtp_server = env::var("SMTP_SERVER").expect("SMTP_SERVER must be set");
let smtp_port = env::var("SMTP_PORT")
.expect("SMTP_PORT must be set")
.parse::<u16>()
.expect("SMTP_PORT must be a number");
let smtp_username = env::var("SMTP_USERNAME").expect("SMTP_USERNAME must be set");
let smtp_password = env::var("SMTP_PASSWORD").expect("SMTP_PASSWORD must be set");
let creds = Credentials::new(smtp_username, smtp_password);
let mailer = SmtpTransport::relay(smtp_server.as_str())
.unwrap()
.port(smtp_port)
.credentials(creds)
.build();
let app_data = AppState::new(pool, mailer);
rocket::build()
.manage(app_data)
.attach(CORS {})
+8 -2
View File
@@ -1,5 +1,7 @@
use diesel::PgConnection;
use diesel::r2d2::{ConnectionManager, Pool};
use lettre::SmtpTransport;
use std::sync::{Arc, Mutex};
pub const DEFAULT_QUERY_LIMIT: i64 = 100;
@@ -7,10 +9,14 @@ pub mod action;
pub struct AppState {
pub db: Pool<ConnectionManager<PgConnection>>,
pub mailer: Arc<Mutex<SmtpTransport>>,
}
impl AppState {
pub fn new(pool: Pool<ConnectionManager<PgConnection>>) -> Self {
Self { db: pool }
pub fn new(pool: Pool<ConnectionManager<PgConnection>>, mailer: SmtpTransport) -> Self {
Self {
db: pool,
mailer: Arc::new(Mutex::new(mailer)),
}
}
}