diff --git a/melib/src/mailbox/email/compose.rs b/melib/src/mailbox/email/compose.rs index 91187d64b..6853a703f 100644 --- a/melib/src/mailbox/email/compose.rs +++ b/melib/src/mailbox/email/compose.rs @@ -203,12 +203,36 @@ impl Draft { Ok(ret) } - pub fn finalise(self) -> Result { + pub fn finalise(mut self) -> Result { let mut ret = String::new(); + if self.headers.contains_key("From") && !self.headers.contains_key("Message-ID") { + if let super::parser::IResult::Done(_, addr) = + super::parser::mailbox(self.headers["From"].as_bytes()) + { + if let Some(fqdn) = addr.get_fqdn() { + if self + .headers + .insert("Message-ID".into(), random::gen_message_id(&fqdn)) + .is_none() + { + let pos = self + .header_order + .iter() + .position(|h| h == "Subject") + .unwrap(); + self.header_order.insert(pos, "Message-ID".into()); + } + } + } + } for k in &self.header_order { let v = &self.headers[k]; - ret.extend(format!("{}: {}\n", k, v).chars()); + if v.is_ascii() { + ret.extend(format!("{}: {}\n", k, v).chars()); + } else { + ret.extend(format!("{}: {}\n", k, mime::encode_header(v)).chars()); + } } ret.push_str("MIME-Version: 1.0\n"); diff --git a/melib/src/mailbox/email/compose/mime.rs b/melib/src/mailbox/email/compose/mime.rs index 8252c34d6..b01eb7c61 100644 --- a/melib/src/mailbox/email/compose/mime.rs +++ b/melib/src/mailbox/email/compose/mime.rs @@ -1 +1,18 @@ -//use super::*; +use super::*; + +pub fn encode_header(value: &str) -> String { + eprintln!("encoding \"{}\"", value); + let mut ret = String::with_capacity(5 / 3 * value.len()); + for word in value.split_whitespace() { + if word.is_ascii() { + ret.push_str(word); + } else { + ret.push_str( + format!("=?UTF-8?B?{}?=", BASE64_MIME.encode(word.as_bytes()).trim()).as_str(), + ); + } + ret.push(' '); + } + ret.pop(); + ret +} diff --git a/ui/src/components/mail/compose.rs b/ui/src/components/mail/compose.rs index 73c87fe50..09b02af24 100644 --- a/ui/src/components/mail/compose.rs +++ b/ui/src/components/mail/compose.rs @@ -503,7 +503,7 @@ impl Component for Composer { if let Err(e) = account.save_draft(draft) { if cfg!(debug_assertions) { eprint!("{}:{}_{}: ", file!(), line!(), column!()); -eprintln!("{:?} could not save draft", e); + eprintln!("{:?} could not save draft", e); } context.replies.push_back(UIEvent::Notification( Some("Could not save draft.".into()), @@ -547,6 +547,7 @@ eprintln!("{:?} could not save draft", e); .expect("Failed to start mailer command"); { let mut stdin = msmtp.stdin.as_mut().expect("failed to open stdin"); + self.update_draft(); let draft = self.draft.clone().finalise().unwrap(); stdin .write_all(draft.as_bytes()) @@ -560,7 +561,7 @@ eprintln!("{:?} could not save draft", e); ) { if cfg!(debug_assertions) { eprint!("{}:{}_{}: ", file!(), line!(), column!()); -eprintln!("{:?} could not save sent msg", e); + eprintln!("{:?} could not save sent msg", e); } context.replies.push_back(UIEvent::Notification( Some("Could not save in 'Sent' folder.".into()),