email content in progress
This commit is contained in:
+8
-6
@@ -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<String>, 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
|
||||
|
||||
+15
-4
@@ -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<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();
|
||||
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<S: Connecter + ClientStream + Clone = TcpStream> Client<S> {
|
||||
}
|
||||
|
||||
/// 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<String>, message: &str) -> SmtpResult {
|
||||
// Connect to the server
|
||||
try!(self.connect());
|
||||
|
||||
@@ -117,7 +128,7 @@ impl<S: Connecter + ClientStream + Clone = TcpStream> Client<S> {
|
||||
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<S: Connecter + ClientStream + Clone = TcpStream> Client<S> {
|
||||
// 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
|
||||
|
||||
@@ -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};
|
||||
|
||||
|
||||
@@ -17,7 +17,6 @@ use common::SP;
|
||||
pub trait ToAddress {
|
||||
/// TODO
|
||||
fn to_address(&self) -> Address;
|
||||
|
||||
}
|
||||
|
||||
impl ToAddress for Address {
|
||||
|
||||
+33
-13
@@ -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<Header>,
|
||||
/// Message body
|
||||
body: Option<String>,
|
||||
body: String,
|
||||
/// TODO cc bcc to
|
||||
to: Vec<String>,
|
||||
/// 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<String> {
|
||||
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
|
||||
|
||||
+3
-3
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user