Client methods take &str

This commit is contained in:
Alexis Mousset
2014-11-07 20:42:22 +01:00
parent 67845bc13c
commit ec495288b5
3 changed files with 34 additions and 33 deletions

View File

@@ -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<String>, message: String,
server: String, port: Option<Port>, my_hostname: Option<String>) -> SmtpResult {
fn sendmail(source_address: &str, recipient_addresses: Vec<&str>, message: &str,
server: &str, port: Option<Port>, my_hostname: &str) -> SmtpResult {
let mut email_client: Client<TcpStream> =
Client::new(
server,
port,
my_hostname
Some(my_hostname)
);
email_client.send_mail::<TcpStream>(
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)
}

View File

@@ -63,12 +63,12 @@ macro_rules! try_smtp (
impl<S> Client<S> {
/// Creates a new SMTP client
pub fn new(host: String, port: Option<Port>, my_hostname: Option<String>) -> Client<S> {
pub fn new(host: &str, port: Option<Port>, my_hostname: Option<&str>) -> Client<S> {
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<S: Connecter + ClientStream + Clone> Client<S> {
}
/// Sends an email
pub fn send_mail<S>(&mut self, from_address: String,
to_addresses: Vec<String>, message: String) -> SmtpResult {
pub fn send_mail<S>(&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::<S>(my_hostname.clone().to_string()) {
match self.ehlo::<S>(my_hostname.as_slice()) {
Err(error) => match error.kind {
ErrorKind::PermanentError(Response{code: 550, message: _}) => {
try_smtp!(self.helo::<S>(my_hostname.clone()) self);
try_smtp!(self.helo::<S>(my_hostname.as_slice()) self);
//self.smtp_fail_if_err::<S>(smtp_result);
},
_ => {
@@ -112,7 +112,7 @@ impl<S: Connecter + ClientStream + Clone> Client<S> {
debug!("Server {}", self.server_info.clone().unwrap());
// Mail
try_smtp!(self.mail::<S>(from_address.clone()) self);
try_smtp!(self.mail::<S>(from_address) self);
// Log the mail command
info!("from=<{}>, size={}, nrcpt={}", from_address, message.len(), to_addresses.len());
@@ -121,17 +121,17 @@ impl<S: Connecter + ClientStream + Clone> Client<S> {
// TODO Return rejected addresses
// TODO Manage the number of recipients
for to_address in to_addresses.iter() {
try_smtp!(self.rcpt::<S>(to_address.clone()) self);
try_smtp!(self.rcpt::<S>(*to_address) self);
}
// Data
try_smtp!(self.data::<S>() self);
// Message content
let sent = try_smtp!(self.message::<S>(message.as_slice()) self);
let sent = try_smtp!(self.message::<S>(message) self);
info!("to=<{}>, status=sent ({})",
to_addresses.clone().connect(">, to=<"), sent.clone());
to_addresses.connect(">, to=<"), sent.clone());
// Quit
try_smtp!(self.quit::<S>() self);
@@ -221,8 +221,8 @@ impl<S: Connecter + ClientStream + Clone> Client<S> {
}
/// Send a HELO command
pub fn helo<S>(&mut self, my_hostname: String) -> SmtpResult {
let res = try!(self.send_command(command::Hello(my_hostname.clone())));
pub fn helo<S>(&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<S: Connecter + ClientStream + Clone> Client<S> {
}
/// Sends a EHLO command
pub fn ehlo<S>(&mut self, my_hostname: String) -> SmtpResult {
let res = try!(self.send_command(command::ExtendedHello(my_hostname.clone())));
pub fn ehlo<S>(&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<S: Connecter + ClientStream + Clone> Client<S> {
}
/// Sends a MAIL command
pub fn mail<S>(&mut self, from_address: String) -> SmtpResult {
pub fn mail<S>(&mut self, from_address: &str) -> SmtpResult {
let server_info = self.server_info.clone().expect("Bad command sequence");
@@ -271,14 +271,14 @@ impl<S: Connecter + ClientStream + Clone> Client<S> {
};
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<S>(&mut self, to_address: String) -> SmtpResult {
pub fn rcpt<S>(&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<S: Connecter + ClientStream + Clone> Client<S> {
}
/// Sends a VRFY command
pub fn vrfy<S, T>(&mut self, to_address: String) -> SmtpResult {
self.send_command(command::Verify(to_address))
pub fn vrfy<S, T>(&mut self, to_address: &str) -> SmtpResult {
self.send_command(command::Verify(to_address.to_string()))
}
/// Sends a EXPN command
pub fn expn<S, T>(&mut self, list: String) -> SmtpResult {
self.send_command(command::Expand(list))
pub fn expn<S, T>(&mut self, list: &str) -> SmtpResult {
self.send_command(command::Expand(list.to_string()))
}
}

View File

@@ -32,11 +32,11 @@
//! use smtp::client::Client;
//!
//! let mut email_client: Client<TcpStream> =
//! Client::new("localhost".to_string(), None, None);
//! Client::new("localhost", None, None);
//! let result = email_client.send_mail::<TcpStream>(
//! "user@example.com".to_string(),
//! vec!["user@example.org".to_string()],
//! "Test email".to_string()
//! "user@example.com",
//! vec!["user@example.org"],
//! "Test email"
//! );
//! ```