Skip to content

Commit

Permalink
Handle missing from/to headers + return parse error
Browse files Browse the repository at this point in the history
  • Loading branch information
marlonbaeten committed May 15, 2023
1 parent 0be4f85 commit 8677d4f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
6 changes: 4 additions & 2 deletions backend/src/mail_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,11 @@ 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);
}

mailin::response::OK
mailin::response::Response::custom(500, "Error parsing message".to_string())
} else {
mailin::response::OK
}
}

fn auth_plain(
Expand Down
25 changes: 23 additions & 2 deletions backend/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use chrono::{DateTime, Local};
use mail_parser::MimeHeaders;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use tracing::{event, Level};
use uuid::Uuid;

pub type MessageId = Uuid;
Expand Down Expand Up @@ -162,7 +163,17 @@ impl TryFrom<mail_parser::Message<'_>> for MailMessage {
fn try_from(message: mail_parser::Message) -> Result<Self, Self::Error> {
let from = match message.from() {
mail_parser::HeaderValue::Address(addr) => addr.into(),
_ => return Err("Could not parse From address header"),
_ => {
event!(
Level::WARN,
"Could not parse 'From' address header, setting placeholder 'from' address."
);

Address {
name: Some("No from header".to_string()),
email: Some("[email protected]".to_string()),
}
}
};

let to = match message.to() {
Expand All @@ -171,7 +182,17 @@ impl TryFrom<mail_parser::Message<'_>> for MailMessage {
.iter()
.map(|addr| addr.into())
.collect::<Vec<Address>>(),
_ => return Err("Could not parse To address header"),
_ => {
event!(
Level::WARN,
"Could not parse 'To' address header, setting placeholder 'to' address."
);

vec![Address {
name: Some("No to header".to_string()),
email: Some("[email protected]".to_string()),
}]
}
};

let subject = message.subject().unwrap_or_default().to_owned();
Expand Down

0 comments on commit 8677d4f

Please sign in to comment.