Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change addr to IpAddr #27

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,15 +125,15 @@
//!

pub mod smtp;
use std::net::IpAddr;
use std::{fmt::Display, hash::Hash, time::Duration};
use tokio::io::{AsyncRead, AsyncWrite};
use tokio_rustls::TlsConnector;

#[cfg(feature = "builder")]
pub use mail_builder;

#[cfg(feature = "dkim")]
pub use mail_auth;
#[cfg(feature = "builder")]
pub use mail_builder;
use tokio::io::{AsyncRead, AsyncWrite};
use tokio_rustls::TlsConnector;

#[derive(Debug)]
pub enum Error {
Expand Down Expand Up @@ -201,10 +201,11 @@ pub struct SmtpClientBuilder<T: AsRef<str> + PartialEq + Eq + Hash> {
pub tls_hostname: T,
pub tls_implicit: bool,
pub credentials: Option<Credentials<T>>,
pub addr: String,
pub addr: Option<IpAddr>,
pub is_lmtp: bool,
pub say_ehlo: bool,
pub local_host: String,
pub port: u16,
}

/// SMTP client builder
Expand Down
34 changes: 25 additions & 9 deletions src/smtp/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,32 @@
* except according to those terms.
*/

use crate::{Credentials, SmtpClient, SmtpClientBuilder};
use smtp_proto::{EhloResponse, EXT_START_TLS};
use std::hash::Hash;
use std::net::IpAddr;
use std::time::Duration;
use tokio::{
io::{AsyncRead, AsyncWrite},
net::TcpStream,
};
use tokio_rustls::client::TlsStream;

use crate::{Credentials, SmtpClient, SmtpClientBuilder};

use super::{tls::build_tls_connector, AssertReply};

impl<T: AsRef<str> + PartialEq + Eq + Hash> SmtpClientBuilder<T> {
pub fn new(hostname: T, port: u16) -> Self {
SmtpClientBuilder::_new(hostname, None, port)
}

pub fn new_bind_ip(hostname: T, addr: IpAddr, port: u16) -> Self {
SmtpClientBuilder::_new(hostname, Some(addr), port)
}

fn _new(hostname: T, addr: Option<IpAddr>, port: u16) -> Self {
SmtpClientBuilder {
addr: format!("{}:{}", hostname.as_ref(), port),
addr,
port,
timeout: Duration::from_secs(60 * 60),
tls_connector: build_tls_connector(false),
tls_hostname: hostname,
Expand Down Expand Up @@ -85,7 +94,7 @@ impl<T: AsRef<str> + PartialEq + Eq + Hash> SmtpClientBuilder<T> {
pub async fn connect(&self) -> crate::Result<SmtpClient<TlsStream<TcpStream>>> {
tokio::time::timeout(self.timeout, async {
let mut client = SmtpClient {
stream: TcpStream::connect(&self.addr).await?,
stream: self.tcp_connect().await?,
timeout: self.timeout,
};

Expand Down Expand Up @@ -130,14 +139,21 @@ impl<T: AsRef<str> + PartialEq + Eq + Hash> SmtpClientBuilder<T> {
.map_err(|_| crate::Error::Timeout)?
}

pub async fn tcp_connect(&self) -> std::io::Result<TcpStream> {
let port = self.port;
if let Some(addr) = self.addr {
TcpStream::connect((addr, port)).await
} else {
TcpStream::connect((self.tls_hostname.as_ref(), port)).await
}
}

/// Connect over clear text (should not be used)
pub async fn connect_plain(&self) -> crate::Result<SmtpClient<TcpStream>> {
let mut client = SmtpClient {
stream: tokio::time::timeout(self.timeout, async {
TcpStream::connect(&self.addr).await
})
.await
.map_err(|_| crate::Error::Timeout)??,
stream: tokio::time::timeout(self.timeout, async { self.tcp_connect().await })
.await
.map_err(|_| crate::Error::Timeout)??,
timeout: self.timeout,
};

Expand Down