diff --git a/examples/client.rs b/examples/client.rs index 54dd9b0..ba2059e 100644 --- a/examples/client.rs +++ b/examples/client.rs @@ -19,7 +19,6 @@ use std::string::String; use std::io::net::ip::Port; use std::os; use getopts::{optopt, optflag, getopts, OptGroup, usage}; -use std::io::net::tcp::TcpStream; use smtp::client::Client; use smtp::error::SmtpResult; @@ -32,7 +31,7 @@ fn sendmail(source_address: &str, recipient_addresses: &[&str], message: &str, (server, port), Some(my_hostname), ); - email_client.send_mail::( + email_client.send_mail( source_address, recipient_addresses, message, diff --git a/src/client/mod.rs b/src/client/mod.rs index 2ea45aa..7fdb017 100644 --- a/src/client/mod.rs +++ b/src/client/mod.rs @@ -56,7 +56,7 @@ macro_rules! try_smtp ( macro_rules! fail_with_err ( ($err: expr $client: ident) => ({ - $client.close_on_error::(); + $client.close_on_error(); return Err(FromError::from_error($err)) }) ) @@ -88,24 +88,24 @@ impl Client { impl Client { /// Closes the SMTP transaction if possible, and then closes the TCP session - fn close_on_error(&mut self) { - if self.is_connected::() { - let _ = self.quit::(); + fn close_on_error(&mut self) { + if self.is_connected() { + let _ = self.quit(); } self.close(); } /// Sends an email - pub fn send_mail(&mut self, from_address: &str, + pub fn send_mail(&mut self, from_address: &str, to_addresses: &[&str], message: &str) -> SmtpResult { // Connect to the server try!(self.connect()); // Extended Hello or Hello - if let Err(error) = self.ehlo::() { + if let Err(error) = self.ehlo() { match error.kind { ErrorKind::PermanentError(Response{code: 550, message: _}) => { - try_smtp!(self.helo::() self); + try_smtp!(self.helo() self); }, _ => { try_smtp!(Err(error) self) @@ -117,7 +117,7 @@ impl Client { debug!("server {}", self.server_info.as_ref().unwrap()); // Mail - try_smtp!(self.mail::(from_address) self); + try_smtp!(self.mail(from_address) self); // Log the mail command info!("from=<{}>, size={}, nrcpt={}", from_address, message.len(), to_addresses.len()); @@ -126,21 +126,21 @@ impl Client { // TODO Return rejected addresses // TODO Manage the number of recipients for to_address in to_addresses.iter() { - try_smtp!(self.rcpt::(*to_address) self); + try_smtp!(self.rcpt(*to_address) self); } // Data - try_smtp!(self.data::() self); + try_smtp!(self.data() self); // Message content - let sent = try_smtp!(self.message::(message) self); + let sent = try_smtp!(self.message(message) self); // Log the rcpt command info!("to=<{}>, status=sent ({})", to_addresses.connect(">, to=<"), sent); // Quit - try_smtp!(self.quit::() self); + try_smtp!(self.quit() self); return Ok(sent); } @@ -206,8 +206,8 @@ impl Client { } /// Checks if the server is connected using the NOOP SMTP command - pub fn is_connected(&mut self) -> bool { - self.noop::().is_ok() + pub fn is_connected(&mut self) -> bool { + self.noop().is_ok() } /// Closes the TCP stream @@ -221,7 +221,7 @@ impl Client { } /// Send a HELO command and fills `server_info` - pub fn helo(&mut self) -> SmtpResult { + pub fn helo(&mut self) -> SmtpResult { let hostname = self.my_hostname.clone(); let result = try!(self.send_command(Command::Hello(hostname))); self.server_info = Some( @@ -234,7 +234,7 @@ impl Client { } /// Sends a EHLO command and fills `server_info` - pub fn ehlo(&mut self) -> SmtpResult { + pub fn ehlo(&mut self) -> SmtpResult { let hostname = self.my_hostname.clone(); let result = try!(self.send_command(Command::ExtendedHello(hostname))); self.server_info = Some( @@ -249,7 +249,7 @@ impl Client { } /// Sends a MAIL command - pub fn mail(&mut self, from_address: &str) -> SmtpResult { + pub fn mail(&mut self, from_address: &str) -> SmtpResult { let server_info = match self.server_info.clone() { Some(info) => info, @@ -272,19 +272,19 @@ impl Client { } /// Sends a RCPT command - pub fn rcpt(&mut self, to_address: &str) -> SmtpResult { + pub fn rcpt(&mut self, to_address: &str) -> SmtpResult { self.send_command( Command::Recipient(to_address.to_string(), None) ) } /// Sends a DATA command - pub fn data(&mut self) -> SmtpResult { + pub fn data(&mut self) -> SmtpResult { self.send_command(Command::Data) } /// Sends the message content - pub fn message(&mut self, message_content: &str) -> SmtpResult { + pub fn message(&mut self, message_content: &str) -> SmtpResult { let server_info = match self.server_info.clone() { Some(info) => info, @@ -315,27 +315,27 @@ impl Client { } /// Sends a QUIT command - pub fn quit(&mut self) -> SmtpResult { + pub fn quit(&mut self) -> SmtpResult { self.send_command(Command::Quit) } /// Sends a RSET command - pub fn rset(&mut self) -> SmtpResult { + pub fn rset(&mut self) -> SmtpResult { self.send_command(Command::Reset) } /// Sends a NOOP command - pub fn noop(&mut self) -> SmtpResult { + pub fn noop(&mut self) -> SmtpResult { self.send_command(Command::Noop) } /// Sends a VRFY command - pub fn vrfy(&mut self, to_address: &str) -> SmtpResult { + pub fn vrfy(&mut self, to_address: &str) -> SmtpResult { self.send_command(Command::Verify(to_address.to_string())) } /// Sends a EXPN command - pub fn expn(&mut self, list: &str) -> SmtpResult { + pub fn expn(&mut self, list: &str) -> SmtpResult { self.send_command(Command::Expand(list.to_string())) } diff --git a/src/email/mod.rs b/src/email/mod.rs index bbae639..5485430 100644 --- a/src/email/mod.rs +++ b/src/email/mod.rs @@ -16,8 +16,8 @@ use time::{now, Tm}; use email::header::Header; use email::address::ToAddress; use common::CRLF; -//use client::Client; -//use error::SmtpResult; +use client::Client; +use error::SmtpResult; pub mod header; pub mod address; @@ -42,15 +42,14 @@ impl Show for Email { impl Email { /// TODO - // pub fn send(&self, client: Client) -> SmtpResult { - // let test: Vec<&str> = self.to.iter().map(|s| s.as_slice()).collect(); - // //let to_vec: &[&str] = self.to.iter().map(|s| s.as_slice()).collect().as_slice(); - // client.send_mail( - // self.from.unwrap().as_slice(), - // test.as_slice(), - // self.to_string().as_slice(), - // ) - // } + pub fn send(&self, mut client: Client) -> SmtpResult { + let to_vec: Vec<&str> = self.to.iter().map(|s| s.as_slice()).collect(); + client.send_mail( + self.from.clone().unwrap().as_slice(), + to_vec.as_slice(), + self.to_string().as_slice(), + ) + } /// TODO pub fn new() -> Email { diff --git a/src/lib.rs b/src/lib.rs index 7186f2a..1dacc2b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -27,7 +27,6 @@ //! //! ```rust,no_run //! #![feature(default_type_params)] -//! use std::io::net::tcp::TcpStream; //! use smtp::client::Client; //! use smtp::common::SMTP_PORT; //! @@ -36,7 +35,7 @@ //! ("localhost", SMTP_PORT), // server socket //! Some("myhost"), // my hostname (default is localhost) //! ); -//! let result = email_client.send_mail::( +//! let result = email_client.send_mail( //! "user@example.com", // sender (reverse-path) //! &["user@example.org"], // recipient list //! "Test email", // email content @@ -49,7 +48,6 @@ //! //! ```rust,no_run //! #![feature(default_type_params)] -//! use std::io::net::tcp::TcpStream; //! use smtp::client::Client; //! use smtp::common::SMTP_PORT; //! @@ -59,12 +57,12 @@ //! Some("myhost"), // my hostname (default is localhost) //! ); //! let _ = email_client.connect(); -//! let _ = email_client.ehlo::(); -//! let _ = email_client.mail::("user@example.com"); -//! let _ = email_client.rcpt::("user@example.org"); -//! let _ = email_client.data::(); -//! let _ = email_client.message::("Test email"); -//! let _ = email_client.quit::(); +//! let _ = email_client.ehlo(); +//! let _ = email_client.mail("user@example.com"); +//! let _ = email_client.rcpt("user@example.org"); +//! let _ = email_client.data(); +//! let _ = email_client.message("Test email"); +//! let _ = email_client.quit(); //! ``` #![crate_type = "lib"]