From 89fa5cdb8020acd9f0142533d6e3db51e6c293d0 Mon Sep 17 00:00:00 2001 From: Paolo Barbolini Date: Wed, 23 Dec 2020 20:33:46 +0100 Subject: [PATCH] sendmail: upgrade to async_std::process from the 1.8.0 release (#520) --- .github/workflows/test.yml | 3 +++ Cargo.toml | 2 +- src/transport/sendmail/mod.rs | 36 +++++++++++++++++++++++++---------- tests/transport_sendmail.rs | 2 ++ 4 files changed, 32 insertions(+), 11 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 7dea5f4..7d327b4 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -6,6 +6,9 @@ on: branches: - master +env: + RUST_BACKTRACE: full + jobs: rustfmt: name: rustfmt / stable diff --git a/Cargo.toml b/Cargo.toml index 7b15f5a..cb5d703 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -52,7 +52,7 @@ futures-util = { version = "0.3.7", features = ["io"], optional = true } ## async-std async-attributes = { version = "1.1", optional = true } -async-std = { version = "1.5", optional = true, features = ["unstable"] } +async-std = { version = "1.8", optional = true, features = ["unstable"] } async-trait = { version = "0.1", optional = true } ## tokio diff --git a/src/transport/sendmail/mod.rs b/src/transport/sendmail/mod.rs index 6cb377c..b21952f 100644 --- a/src/transport/sendmail/mod.rs +++ b/src/transport/sendmail/mod.rs @@ -164,6 +164,25 @@ impl SendmailTransport { .stderr(Stdio::piped()); c } + + #[cfg(feature = "async-std1")] + fn async_std_command(&self, envelope: &Envelope) -> async_std::process::Command { + use async_std::process::Command; + + let mut c = Command::new(&self.command); + // TODO: figure out why enabling this kills it earlier + // c.kill_on_drop(true); + c.arg("-i"); + if let Some(from) = envelope.from() { + c.arg("-f").arg(from); + } + c.arg("--") + .args(envelope.to()) + .stdin(Stdio::piped()) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()); + c + } } impl Default for SendmailTransport { @@ -198,18 +217,15 @@ impl AsyncStd1Transport for SendmailTransport { type Error = Error; async fn send_raw(&self, envelope: &Envelope, email: &[u8]) -> Result { - let mut command = self.command(envelope); - let email = email.to_vec(); + use async_std::io::prelude::WriteExt; - // TODO: Convert to real async, once async-std has a process implementation. - let output = async_std::task::spawn_blocking(move || { - // Spawn the sendmail command - let mut process = command.spawn()?; + let mut command = self.async_std_command(envelope); - process.stdin.as_mut().unwrap().write_all(&email)?; - process.wait_with_output() - }) - .await?; + // Spawn the sendmail command + let mut process = command.spawn()?; + + process.stdin.as_mut().unwrap().write_all(&email).await?; + let output = process.output().await?; if output.status.success() { Ok(()) diff --git a/tests/transport_sendmail.rs b/tests/transport_sendmail.rs index 15535c6..fda2a64 100644 --- a/tests/transport_sendmail.rs +++ b/tests/transport_sendmail.rs @@ -39,6 +39,7 @@ mod test { .unwrap(); let result = sender.send(email).await; + println!("{:?}", result); assert!(result.is_ok()); } @@ -58,6 +59,7 @@ mod test { .unwrap(); let result = sender.send(email).await; + println!("{:?}", result); assert!(result.is_ok()); } }