Skip to content

Commit

Permalink
Return message id after a message is parsed
Browse files Browse the repository at this point in the history
  • Loading branch information
marlonbaeten committed Jun 8, 2023
1 parent 6fd03df commit 92b48d0
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 16 deletions.
22 changes: 13 additions & 9 deletions backend/src/mail_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ impl MailHandler {
}

impl MailHandler {
fn parse_mail(&mut self) -> Result<(), &'static str> {
fn parse_mail(&mut self) -> Result<MailMessage, &'static str> {
// parse the email and convert it to a internal data structure
let parsed = mail_parser::Message::parse(&self.buffer)
.ok_or("Could not parse email using mail_parser")?;
Expand All @@ -48,10 +48,10 @@ impl MailHandler {

// send the message to a internal queue
self.tx
.send(message)
.send(message.clone())
.map_err(|_| "Could not send email to own broadcast channel")?;

Ok(())
Ok(message)
}
}

Expand Down Expand Up @@ -80,7 +80,7 @@ impl mailin::Handler for MailHandler {
) -> mailin::Response {
event!(
Level::INFO,
"New email on {} from {} to {:?}",
"Incoming message on {} from {} to {:?}",
domain,
from,
to
Expand All @@ -94,12 +94,16 @@ impl mailin::Handler for MailHandler {
}

fn data_end(&mut self) -> mailin::Response {
if let Err(e) = self.parse_mail() {
event!(Level::WARN, "Error parsing email: {}", e);
match self.parse_mail() {
Err(e) => {
event!(Level::WARN, "Error parsing email: {}", e);

mailin::response::Response::custom(500, "Error parsing message".to_string())
} else {
mailin::response::OK
mailin::response::Response::custom(500, "Error parsing message".to_string())
}
Ok(message) => mailin::response::Response::custom(
250,
format!("2.0.0 Ok: queued as {}", message.id),
),
}
}

Expand Down
21 changes: 16 additions & 5 deletions backend/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ mod test {
use lettre::address::Envelope;
use lettre::message::header::ContentType;
use lettre::message::{Attachment, MultiPart, SinglePart};
use lettre::{Message, SmtpTransport, Transport, Address};
use lettre::{Address, Message, SmtpTransport, Transport};
use std::process::{Command, Stdio};
use tokio::time::{sleep, Duration};

Expand Down Expand Up @@ -309,10 +309,21 @@ mod test {

while let Some(Ok(entry)) = paths.next() {
let message = std::fs::read_to_string(entry.path()).unwrap();
let mut lines= message.lines();

let sender = lines.next().unwrap().trim_start_matches("Sender: ").parse::<Address>().unwrap();
let recipients = lines.next().unwrap().trim_start_matches("Recipients: ").split(',').map(|r| r.trim().parse::<Address>().unwrap()).collect::<Vec<Address>>();
let mut lines = message.lines();

let sender = lines
.next()
.unwrap()
.trim_start_matches("Sender: ")
.parse::<Address>()
.unwrap();
let recipients = lines
.next()
.unwrap()
.trim_start_matches("Recipients: ")
.split(',')
.map(|r| r.trim().parse::<Address>().unwrap())
.collect::<Vec<Address>>();
let envelope = Envelope::new(Some(sender), recipients).unwrap();

let email = lines.collect::<Vec<&str>>().join("\n");
Expand Down
4 changes: 2 additions & 2 deletions backend/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ impl TryFrom<mail_parser::Message<'_>> for MailMessage {
_ => {
event!(
Level::WARN,
"Could not parse 'From' address header, setting placeholder 'from' address."
"Could not parse 'From' address header, setting placeholder address."
);

Address {
Expand All @@ -199,7 +199,7 @@ impl TryFrom<mail_parser::Message<'_>> for MailMessage {
_ => {
event!(
Level::WARN,
"Could not parse 'To' address header, setting placeholder 'to' address."
"Could not parse 'To' address header, setting placeholder address."
);

vec![Address {
Expand Down

0 comments on commit 92b48d0

Please sign in to comment.