add functionality to send attachmets, Fixes #113
This commit is contained in:
parent
7ff9978587
commit
23f5e3ca4a
@ -1,8 +1,12 @@
|
|||||||
use std::error::Error;
|
use std::{error::Error, fs};
|
||||||
|
|
||||||
use lettre::{
|
use lettre::{
|
||||||
message::header::ContentType, transport::smtp::authentication::Credentials, Message,
|
message::{
|
||||||
SmtpTransport, Transport,
|
header::{self, ContentType},
|
||||||
|
Attachment, MultiPart, SinglePart,
|
||||||
|
},
|
||||||
|
transport::smtp::authentication::Credentials,
|
||||||
|
Message, SmtpTransport, Transport,
|
||||||
};
|
};
|
||||||
use sqlx::SqlitePool;
|
use sqlx::SqlitePool;
|
||||||
|
|
||||||
@ -13,7 +17,7 @@ use super::{family::Family, log::Log, role::Role, user::User};
|
|||||||
pub struct Mail {}
|
pub struct Mail {}
|
||||||
|
|
||||||
impl Mail {
|
impl Mail {
|
||||||
pub async fn send(db: &SqlitePool, data: MailToSend, smtp_pw: String) -> bool {
|
pub async fn send(db: &SqlitePool, data: MailToSend<'_>, smtp_pw: String) -> bool {
|
||||||
let mut email = Message::builder()
|
let mut email = Message::builder()
|
||||||
.from(
|
.from(
|
||||||
"ASKÖ Ruderverein Donau Linz <no-reply@rudernlinz.at>"
|
"ASKÖ Ruderverein Donau Linz <no-reply@rudernlinz.at>"
|
||||||
@ -45,12 +49,30 @@ impl Mail {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: handle attachments
|
let mut multipart = MultiPart::mixed().singlepart(
|
||||||
|
SinglePart::builder()
|
||||||
|
.header(header::ContentType::TEXT_HTML)
|
||||||
|
.body(String::from(data.body)),
|
||||||
|
);
|
||||||
|
|
||||||
|
for temp_file in &data.files {
|
||||||
|
let content = fs::read(temp_file.path().unwrap()).unwrap();
|
||||||
|
let media_type = format!("{}", temp_file.content_type().unwrap().media_type());
|
||||||
|
let content_type = ContentType::parse(&media_type).unwrap();
|
||||||
|
let attachment = Attachment::new(format!(
|
||||||
|
"{}.{}",
|
||||||
|
temp_file.name().unwrap(),
|
||||||
|
temp_file.content_type().unwrap().extension().unwrap()
|
||||||
|
))
|
||||||
|
.body(content, content_type);
|
||||||
|
|
||||||
|
multipart = multipart.singlepart(attachment);
|
||||||
|
}
|
||||||
|
|
||||||
let email = email
|
let email = email
|
||||||
.subject(data.subject)
|
.subject(data.subject)
|
||||||
.header(ContentType::TEXT_PLAIN)
|
.header(ContentType::TEXT_PLAIN)
|
||||||
.body(String::from(data.body))
|
.multipart(multipart)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let creds = Credentials::new("no-reply@rudernlinz.at".to_owned(), smtp_pw);
|
let creds = Credentials::new("no-reply@rudernlinz.at".to_owned(), smtp_pw);
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
use rocket::form::Form;
|
use rocket::form::Form;
|
||||||
|
use rocket::fs::TempFile;
|
||||||
use rocket::response::{Flash, Redirect};
|
use rocket::response::{Flash, Redirect};
|
||||||
use rocket::{get, request::FlashMessage, routes, Route, State};
|
use rocket::{get, request::FlashMessage, routes, Route, State};
|
||||||
use rocket::{post, FromForm};
|
use rocket::{post, FromForm};
|
||||||
@ -39,18 +40,17 @@ async fn fee(db: &State<SqlitePool>, _admin: AdminUser, config: &State<Config>)
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(FromForm, Debug)]
|
#[derive(FromForm, Debug)]
|
||||||
pub struct MailToSend {
|
pub struct MailToSend<'a> {
|
||||||
//<'a> {
|
|
||||||
pub(crate) role_id: i32,
|
pub(crate) role_id: i32,
|
||||||
pub(crate) subject: String,
|
pub(crate) subject: String,
|
||||||
pub(crate) body: String,
|
pub(crate) body: String,
|
||||||
//pub(crate) files: Vec<TempFile<'a>>,
|
pub(crate) files: Vec<TempFile<'a>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[post("/mail", data = "<data>")]
|
#[post("/mail", data = "<data>")]
|
||||||
async fn update(
|
async fn update(
|
||||||
db: &State<SqlitePool>,
|
db: &State<SqlitePool>,
|
||||||
data: Form<MailToSend>,
|
data: Form<MailToSend<'_>>,
|
||||||
config: &State<Config>,
|
config: &State<Config>,
|
||||||
_admin: AdminUser,
|
_admin: AdminUser,
|
||||||
) -> Flash<Redirect> {
|
) -> Flash<Redirect> {
|
||||||
|
Loading…
Reference in New Issue
Block a user