diff --git a/examples/client.rs b/examples/client.rs index c04300b..3f381a4 100644 --- a/examples/client.rs +++ b/examples/client.rs @@ -22,13 +22,13 @@ use getopts::{optopt, optflag, getopts, OptGroup, usage}; use smtp::client::Client; use smtp::error::SmtpResult; -fn sendmail(source_address: String, recipient_addresses: Vec, message: String, - server: String, port: Option, my_hostname: Option) -> SmtpResult { +fn sendmail(source_address: &str, recipient_addresses: Vec<&str>, message: &str, + server: &str, port: Option, my_hostname: &str) -> SmtpResult { let mut email_client: Client = Client::new( server, port, - my_hostname + Some(my_hostname) ); email_client.send_mail::( source_address, @@ -83,7 +83,7 @@ fn main() { let server = match matches.opt_str("s") { Some(server) => server, - None => String::from_str("localhost") + None => "localhost".to_string() }; let my_hostname = match matches.opt_str("m") { @@ -105,7 +105,7 @@ fn main() { }; let mut recipients = Vec::new(); for recipient in recipients_str.split(' ') { - recipients.push(String::from_str(recipient)) + recipients.push(recipient) } let mut message = String::new(); @@ -113,7 +113,8 @@ fn main() { message.push_str(line.unwrap().as_slice()); } - match sendmail(sender, recipients, message, server, port, my_hostname) { + match sendmail(sender.as_slice(), recipients, message.as_slice(), + server.as_slice(), port, my_hostname.unwrap_or("localhost".to_string()).as_slice()) { Ok(..) => info!("Email sent successfully"), Err(error) => error!("{}", error) } diff --git a/src/client/mod.rs b/src/client/mod.rs index ce510a0..0a93673 100644 --- a/src/client/mod.rs +++ b/src/client/mod.rs @@ -63,12 +63,12 @@ macro_rules! try_smtp ( impl Client { /// Creates a new SMTP client - pub fn new(host: String, port: Option, my_hostname: Option) -> Client { + pub fn new(host: &str, port: Option, my_hostname: Option<&str>) -> Client { Client{ stream: None, - host: host, + host: host.to_string(), port: port.unwrap_or(SMTP_PORT), - my_hostname: my_hostname.unwrap_or("localhost".to_string()), + my_hostname: my_hostname.unwrap_or("localhost").to_string(), server_info: None, state: transaction::Unconnected } @@ -87,18 +87,18 @@ impl Client { } /// Sends an email - pub fn send_mail(&mut self, from_address: String, - to_addresses: Vec, message: String) -> SmtpResult { + pub fn send_mail(&mut self, from_address: &str, + to_addresses: Vec<&str>, message: &str) -> SmtpResult { let my_hostname = self.my_hostname.clone(); // Connect to the server try!(self.connect()); // Extended Hello or Hello - match self.ehlo::(my_hostname.clone().to_string()) { + match self.ehlo::(my_hostname.as_slice()) { Err(error) => match error.kind { ErrorKind::PermanentError(Response{code: 550, message: _}) => { - try_smtp!(self.helo::(my_hostname.clone()) self); + try_smtp!(self.helo::(my_hostname.as_slice()) self); //self.smtp_fail_if_err::(smtp_result); }, _ => { @@ -112,7 +112,7 @@ impl Client { debug!("Server {}", self.server_info.clone().unwrap()); // Mail - try_smtp!(self.mail::(from_address.clone()) self); + try_smtp!(self.mail::(from_address) self); // Log the mail command info!("from=<{}>, size={}, nrcpt={}", from_address, message.len(), to_addresses.len()); @@ -121,17 +121,17 @@ 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.clone()) self); + try_smtp!(self.rcpt::(*to_address) self); } // Data try_smtp!(self.data::() self); // Message content - let sent = try_smtp!(self.message::(message.as_slice()) self); + let sent = try_smtp!(self.message::(message) self); info!("to=<{}>, status=sent ({})", - to_addresses.clone().connect(">, to=<"), sent.clone()); + to_addresses.connect(">, to=<"), sent.clone()); // Quit try_smtp!(self.quit::() self); @@ -221,8 +221,8 @@ impl Client { } /// Send a HELO command - pub fn helo(&mut self, my_hostname: String) -> SmtpResult { - let res = try!(self.send_command(command::Hello(my_hostname.clone()))); + pub fn helo(&mut self, my_hostname: &str) -> SmtpResult { + let res = try!(self.send_command(command::Hello(my_hostname.to_string()))); match res.with_code(vec![250]) { Ok(response) => { self.server_info = Some( @@ -239,8 +239,8 @@ impl Client { } /// Sends a EHLO command - pub fn ehlo(&mut self, my_hostname: String) -> SmtpResult { - let res = try!(self.send_command(command::ExtendedHello(my_hostname.clone()))); + pub fn ehlo(&mut self, my_hostname: &str) -> SmtpResult { + let res = try!(self.send_command(command::ExtendedHello(my_hostname.to_string()))); match res.with_code(vec![250]) { Ok(response) => { self.server_info = Some( @@ -259,7 +259,7 @@ impl Client { } /// Sends a MAIL command - pub fn mail(&mut self, from_address: String) -> SmtpResult { + pub fn mail(&mut self, from_address: &str) -> SmtpResult { let server_info = self.server_info.clone().expect("Bad command sequence"); @@ -271,14 +271,14 @@ impl Client { }; self.send_command( - command::Mail(unquote_email_address(from_address.as_slice()).to_string(), options) + command::Mail(unquote_email_address(from_address).to_string(), options) ) } /// Sends a RCPT command - pub fn rcpt(&mut self, to_address: String) -> SmtpResult { + pub fn rcpt(&mut self, to_address: &str) -> SmtpResult { self.send_command( - command::Recipient(unquote_email_address(to_address.as_slice()).to_string(), None) + command::Recipient(unquote_email_address(to_address).to_string(), None) ) } @@ -331,13 +331,13 @@ impl Client { } /// Sends a VRFY command - pub fn vrfy(&mut self, to_address: String) -> SmtpResult { - self.send_command(command::Verify(to_address)) + 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: String) -> SmtpResult { - self.send_command(command::Expand(list)) + pub fn expn(&mut self, list: &str) -> SmtpResult { + self.send_command(command::Expand(list.to_string())) } } diff --git a/src/lib.rs b/src/lib.rs index 24aa2f5..1bd11fd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -32,11 +32,11 @@ //! use smtp::client::Client; //! //! let mut email_client: Client = -//! Client::new("localhost".to_string(), None, None); +//! Client::new("localhost", None, None); //! let result = email_client.send_mail::( -//! "user@example.com".to_string(), -//! vec!["user@example.org".to_string()], -//! "Test email".to_string() +//! "user@example.com", +//! vec!["user@example.org"], +//! "Test email" //! ); //! ```