Update exmaple client with headers

This commit is contained in:
Alexis Mousset
2014-12-07 13:53:50 +01:00
parent 56bceaa47e
commit 334613867d
4 changed files with 31 additions and 28 deletions

View File

@@ -21,19 +21,26 @@ use getopts::{optopt, optflag, getopts, OptGroup, usage};
use smtp::client::Client;
use smtp::error::SmtpResult;
//use smtp::email::Email;
use smtp::email::Email;
fn sendmail(source_address: String, recipient_addresses: Vec<String>, message: &str,
fn sendmail(source_address: &str, recipient_addresses: &[&str], message: &str, subject: &str,
server: &str, port: Port, my_hostname: &str) -> SmtpResult {
let mut email = Email::new();
for destination in recipient_addresses.iter() {
email.to(*destination);
}
email.date();
email.from(source_address);
email.body(message);
email.subject(subject);
let mut email_client =
Client::new(
(server, port),
Some(my_hostname),
);
email_client.send_mail(
source_address,
recipient_addresses,
message,
email_client.send_email(
email
)
}
@@ -57,9 +64,10 @@ fn main() {
program);
let opts = [
optopt("s", "subject", "set the email subject", "SUBJECT"),
optopt("r", "reverse-path", "set the sender address", "FROM_ADDRESS"),
optopt("p", "port", "set the port to use, default is 25", "PORT"),
optopt("s", "server", "set the server to use, default is localhost", "SERVER"),
optopt("a", "server", "set the server to use, default is localhost", "SERVER"),
optopt("m", "my-hostname", "set the hostname used by the client", "MY_HOSTNAME"),
optflag("h", "help", "print this help menu"),
];
@@ -89,7 +97,7 @@ fn main() {
let mut recipients = Vec::new();
for recipient in recipients_str.split(' ') {
recipients.push(recipient.to_string());
recipients.push(recipient);
}
let mut message = String::new();
@@ -102,13 +110,18 @@ fn main() {
match sendmail(
// sender
matches.opt_str("r").unwrap(),
matches.opt_str("r").unwrap().as_slice(),
// recipients
recipients,
recipients.as_slice(),
// message content
message.as_slice(),
// server
// subject
match matches.opt_str("s") {
Some(ref subject) => subject.as_slice(),
None => "(empty subject)"
},
// server
match matches.opt_str("a") {
Some(ref server) => server.as_slice(),
None => "localhost"
},

View File

@@ -89,8 +89,7 @@ impl<S = TcpStream> Client<S> {
impl<S: Connecter + ClientStream + Clone = TcpStream> Client<S> {
/// TODO
pub fn send_email(&mut self, email: &Email) -> SmtpResult {
//let tt: Vec<&str> = email.getTo().iter().map(|s| s.as_slice()).collect();
pub fn send_email(&mut self, email: Email) -> SmtpResult {
self.send_mail(
email.get_from(),
email.get_to(),

View File

@@ -35,12 +35,6 @@ impl Header {
}
}
// impl Str for Header {
// fn as_slice<'a>(&'a self) -> &'a str {
// self.clone().to_string().clone().as_slice()
// }
// }
#[cfg(test)]
mod test {
use super::Header;

View File

@@ -34,7 +34,12 @@ pub struct Email {
impl Show for Email {
fn fmt(&self, f: &mut Formatter) -> Result {
f.write(format!("{}{}{}", self.headers, CRLF, self.body).as_bytes())
let mut formatted_headers = String::new();
for header in self.headers.iter() {
formatted_headers.push_str(header.to_string().as_slice());
formatted_headers.push_str(CRLF);
}
f.write(format!("{}{}{}", formatted_headers, CRLF, self.body).as_bytes())
}
}
@@ -57,14 +62,6 @@ impl Email {
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()
}