add pre-send hook (#178)

This commit is contained in:
Clément DOUIN
2022-03-05 00:42:11 +01:00
parent 212f5e6eb1
commit f79e0ae4fb
10 changed files with 94 additions and 34 deletions
+20 -5
View File
@@ -24,7 +24,7 @@ use crate::{
};
/// Representation of a message.
#[derive(Debug, Default)]
#[derive(Debug, Clone, Default)]
pub struct Msg {
/// The sequence number of the message.
///
@@ -359,15 +359,18 @@ impl Msg {
loop {
match choice::post_edit() {
Ok(PostEditChoice::Send) => {
let sent_msg = smtp.send_msg(account, &self)?;
printer.print_str("Sending message…")?;
let sent_msg = smtp.send(account, &self)?;
let sent_folder = account
.mailboxes
.get("sent")
.map(|s| s.as_str())
.unwrap_or(DEFAULT_SENT_FOLDER);
backend.add_msg(&sent_folder, &sent_msg.formatted(), "seen")?;
printer
.print_str(format!("Adding message to the {:?} folder…", sent_folder))?;
backend.add_msg(&sent_folder, &sent_msg, "seen")?;
msg_utils::remove_local_draft()?;
printer.print_struct("Message successfully sent")?;
printer.print_struct("Done!")?;
break;
}
Ok(PostEditChoice::Edit) => {
@@ -711,7 +714,19 @@ impl TryInto<lettre::address::Envelope> for Msg {
type Error = Error;
fn try_into(self) -> Result<lettre::address::Envelope> {
let from = match self.from.and_then(|addrs| addrs.extract_single_info()) {
(&self).try_into()
}
}
impl TryInto<lettre::address::Envelope> for &Msg {
type Error = Error;
fn try_into(self) -> Result<lettre::address::Envelope> {
let from = match self
.from
.as_ref()
.and_then(|addrs| addrs.clone().extract_single_info())
{
Some(addr) => addr.addr.parse().map(Some),
None => Ok(None),
}?;
+2 -4
View File
@@ -8,7 +8,6 @@ use log::{debug, info, trace};
use mailparse::addrparse;
use std::{
borrow::Cow,
convert::TryInto,
fs,
io::{self, BufRead},
};
@@ -356,9 +355,8 @@ pub fn send<'a, P: PrinterService, B: Backend<'a> + ?Sized, S: SmtpService>(
.join("\r\n")
};
trace!("raw message: {:?}", raw_msg);
let envelope: lettre::address::Envelope = Msg::from_tpl(&raw_msg)?.try_into()?;
trace!("envelope: {:?}", envelope);
smtp.send_raw_msg(&envelope, raw_msg.as_bytes())?;
let msg = Msg::from_tpl(&raw_msg)?;
smtp.send(&config, &msg)?;
backend.add_msg(&sent_folder, raw_msg.as_bytes(), "seen")?;
Ok(())
}
+2 -2
View File
@@ -103,7 +103,7 @@ pub fn send<'a, P: PrinterService, B: Backend<'a> + ?Sized, S: SmtpService>(
.join("\n")
};
let msg = Msg::from_tpl(&tpl)?.add_attachments(attachments_paths)?;
let sent_msg = smtp.send_msg(account, &msg)?;
backend.add_msg(mbox, &sent_msg.formatted(), "seen")?;
let sent_msg = smtp.send(account, &msg)?;
backend.add_msg(mbox, &sent_msg, "seen")?;
printer.print_struct("Template successfully sent")
}