diff --git a/examples/client.rs b/examples/client.rs index ba2059e..d10a5d0 100644 --- a/examples/client.rs +++ b/examples/client.rs @@ -10,7 +10,6 @@ #![feature(default_type_params)] #![feature(phase)] #[phase(plugin, link)] extern crate log; - extern crate smtp; extern crate getopts; @@ -24,7 +23,7 @@ use smtp::client::Client; use smtp::error::SmtpResult; //use smtp::email::Email; -fn sendmail(source_address: &str, recipient_addresses: &[&str], message: &str, +fn sendmail(source_address: String, recipient_addresses: Vec, message: &str, server: &str, port: Port, my_hostname: &str) -> SmtpResult { let mut email_client = Client::new( @@ -90,19 +89,22 @@ fn main() { let mut recipients = Vec::new(); for recipient in recipients_str.split(' ') { - recipients.push(recipient); + recipients.push(recipient.to_string()); } let mut message = String::new(); - for line in stdin().lines() { + + let mut line = stdin().read_line(); + while line.is_ok() { message.push_str(line.unwrap().as_slice()); + line = stdin().read_line(); } match sendmail( // sender - matches.opt_str("r").unwrap().as_slice(), + matches.opt_str("r").unwrap(), // recipients - recipients.as_slice(), + recipients, // message content message.as_slice(), // server diff --git a/src/client/mod.rs b/src/client/mod.rs index 7fdb017..0d437f6 100644 --- a/src/client/mod.rs +++ b/src/client/mod.rs @@ -24,6 +24,7 @@ use error::{SmtpResult, ErrorKind}; use client::connecter::Connecter; use client::server_info::ServerInfo; use client::stream::ClientStream; +use email::Email; pub mod server_info; pub mod connecter; @@ -87,6 +88,16 @@ impl Client { } impl Client { + /// TODO + pub fn send_email(&mut self, email: &Email) -> SmtpResult { + //let tt: Vec<&str> = email.getTo().iter().map(|s| s.as_slice()).collect(); + self.send_mail( + email.get_from(), + email.get_to(), + email.to_string().as_slice(), + ) + } + /// Closes the SMTP transaction if possible, and then closes the TCP session fn close_on_error(&mut self) { if self.is_connected() { @@ -96,8 +107,8 @@ impl Client { } /// Sends an email - pub fn send_mail(&mut self, from_address: &str, - to_addresses: &[&str], message: &str) -> SmtpResult { + pub fn send_mail(&mut self, from_address: String, + to_addresses: Vec, message: &str) -> SmtpResult { // Connect to the server try!(self.connect()); @@ -117,7 +128,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.as_slice()) self); // Log the mail command info!("from=<{}>, size={}, nrcpt={}", from_address, message.len(), to_addresses.len()); @@ -126,7 +137,7 @@ 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.as_slice()) self); } // Data diff --git a/src/client/stream.rs b/src/client/stream.rs index df4cd1f..10b9e1b 100644 --- a/src/client/stream.rs +++ b/src/client/stream.rs @@ -13,9 +13,9 @@ use std::io::net::tcp::TcpStream; use std::io::IoResult; use std::str::from_utf8; use std::vec::Vec; -use error::SmtpResult; use std::error::FromError; +use error::SmtpResult; use response::Response; use tools::{escape_crlf, escape_dot}; diff --git a/src/email/address.rs b/src/email/address.rs index 5a5aedc..fc36e8e 100644 --- a/src/email/address.rs +++ b/src/email/address.rs @@ -17,7 +17,6 @@ use common::SP; pub trait ToAddress { /// TODO fn to_address(&self) -> Address; - } impl ToAddress for Address { diff --git a/src/email/mod.rs b/src/email/mod.rs index 5485430..ebc93c8 100644 --- a/src/email/mod.rs +++ b/src/email/mod.rs @@ -16,8 +16,6 @@ use time::{now, Tm}; use email::header::Header; use email::address::ToAddress; use common::CRLF; -use client::Client; -use error::SmtpResult; pub mod header; pub mod address; @@ -27,7 +25,7 @@ pub struct Email { /// Array of headers headers: Vec
, /// Message body - body: Option, + body: String, /// TODO cc bcc to to: Vec, /// TODO @@ -42,25 +40,47 @@ impl Show for Email { impl Email { /// TODO - 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(), - ) + pub fn new() -> Email { + Email{headers: vec!(), body: "".to_string(), to: vec!(), from: None} } /// TODO - pub fn new() -> Email { - Email{headers: vec!(), body: None, to: vec!(), from: None} + pub fn clear(&mut self) { + self.headers.clear(); + self.body = "".to_string(); + self.to.clear(); + self.from = None; + } + + /// TODO + pub fn get_to<'a>(&'a self) -> Vec { + if self.to.is_empty() { + panic!("The To field is empty") + } + //let mut recipients: Vec<&'a str> = Vec::new(); + //for recipient in self.to.iter() { + // recipients.push(recipient.as_slice()); + //} + //let to_clone = self.to.clone(); + //let rec: Vec<&'a str> = self.to.iter().map(|s| s.as_slice()).collect(); + //let plop: &'a[&'a str] = recipients.as_slice(); + //plop + self.to.clone() + } + + /// TODO + pub fn get_from(&self) -> String { + match self.from { + Some(ref from_address) => from_address.clone(), + None => panic!("The From field is empty"), + } } // TODO : standard headers method /// TODO pub fn body(&mut self, body: &str) { - self.body = Some(body.to_string()); + self.body = body.to_string(); } /// TODO diff --git a/src/lib.rs b/src/lib.rs index 1dacc2b..dbd4a81 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -36,8 +36,8 @@ //! Some("myhost"), // my hostname (default is localhost) //! ); //! let result = email_client.send_mail( -//! "user@example.com", // sender (reverse-path) -//! &["user@example.org"], // recipient list +//! "user@example.com".to_string(), // sender (reverse-path) +//! vec!("user@example.org".to_string()), // recipient list //! "Test email", // email content //! ); //! ``` @@ -70,7 +70,7 @@ #![doc(html_root_url = "http://amousset.github.io/rust-smtp/smtp/")] #![experimental] -#![feature(phase, macro_rules, if_let, default_type_params)] +#![feature(phase, macro_rules, default_type_params)] #![deny(missing_docs, warnings)] #![feature(phase)] #[phase(plugin, link)] extern crate log;