Repository: lettre/lettre (language: rust)

Issue:
connection health check that sends NOOP command
**Is your feature request related to a problem? Please describe.**
Providing a way to check that a connection is alive and responding correctly. For example, as part of an app's startup self-test or ongoing health checks, as is common in kubernetes applications.

**Describe the solution you'd like**
Some way to send an SMTP `NOOP` command and wait for its `250 Ok` response.

**Describe alternatives you've considered**
There's no real alternatives to actively checking connection health yet.

**Additional context**
First mentioned in https://github.com/lettre/lettre/issues/547#issuecomment-856980942 as a potential aid to interop with an external async pool lib. I think active health checking is a more important use case.



Discussion / hints:

Fix patch:
diff --git a/src/transport/smtp/async_transport.rs b/src/transport/smtp/async_transport.rs
index b7f0334a1..8ed64724f 100644
--- a/src/transport/smtp/async_transport.rs
+++ b/src/transport/smtp/async_transport.rs
@@ -167,6 +167,21 @@ where
             pool_config: PoolConfig::default(),
         }
     }
+
+    /// Tests the SMTP connection
+    ///
+    /// `test_connection()` tests the connection by using the SMTP NOOP command.
+    /// The connection is closed afterwards if a connection pool is not used.
+    pub async fn test_connection(&self) -> Result<bool, Error> {
+        let mut conn = self.inner.connection().await?;
+
+        let is_connected = conn.test_connected().await;
+
+        #[cfg(not(feature = "pool"))]
+        conn.quit().await?;
+
+        Ok(is_connected)
+    }
 }
 
 impl<E: Executor> Debug for AsyncSmtpTransport<E> {
diff --git a/src/transport/smtp/transport.rs b/src/transport/smtp/transport.rs
index f72ec82c0..5e22b764c 100644
--- a/src/transport/smtp/transport.rs
+++ b/src/transport/smtp/transport.rs
@@ -107,6 +107,21 @@ impl SmtpTransport {
             pool_config: PoolConfig::default(),
         }
     }
+
+    /// Tests the SMTP connection
+    ///
+    /// `test_connection()` tests the connection by using the SMTP NOOP command.
+    /// The connection is closed afterwards if a connection pool is not used.
+    pub fn test_connection(&self) -> Result<bool, Error> {
+        let mut conn = self.inner.connection()?;
+
+        let is_connected = conn.test_connected();
+
+        #[cfg(not(feature = "pool"))]
+        conn.quit()?;
+
+        Ok(is_connected)
+    }
 }
 
 /// Contains client configuration.


Test patch: