|
| 1 | +use std::fmt; |
1 | 2 | use std::net::{TcpStream, ToSocketAddrs};
|
2 | 3 | use native_tls::{TlsConnector, TlsStream};
|
3 | 4 | use std::io::{self, Read, Write};
|
4 | 5 | use std::time::Duration;
|
5 | 6 | use bufstream::BufStream;
|
| 7 | +use chrono::prelude::*; |
6 | 8 |
|
7 | 9 | use super::mailbox::Mailbox;
|
8 | 10 | use super::authenticator::Authenticator;
|
@@ -448,9 +450,46 @@ impl<T: Read + Write> Client<T> {
|
448 | 450 | IdleHandle::new(self)
|
449 | 451 | }
|
450 | 452 |
|
451 |
| - /// The APPEND command adds a mail to a mailbox. |
452 |
| - pub fn append(&mut self, folder: &str, content: &[u8]) -> Result<Vec<String>> { |
453 |
| - try!(self.run_command(&format!("APPEND \"{}\" {{{}}}", folder, content.len()))); |
| 453 | + /// The APPEND command adds a mail to a mailbox. If the datetime or flags are unspecified they |
| 454 | + /// are not included in the request. |
| 455 | + pub fn append<Tz: TimeZone>( |
| 456 | + &mut self, |
| 457 | + folder: &str, |
| 458 | + flags: Option<Vec<String>>, |
| 459 | + datetime: Option<DateTime<Tz>>, |
| 460 | + content: &[u8], |
| 461 | + ) -> Result<Vec<String>> |
| 462 | + where |
| 463 | + Tz::Offset: fmt::Display, |
| 464 | + { |
| 465 | + // Set up optional flags. |
| 466 | + let mut optionals = String::new(); |
| 467 | + match flags { |
| 468 | + None => (), |
| 469 | + Some(flags) => { |
| 470 | + optionals.push_str(&format!( |
| 471 | + "({})", |
| 472 | + flags |
| 473 | + .iter() |
| 474 | + .fold(String::new(), |acc, ref flag| acc + &flag) |
| 475 | + .trim() |
| 476 | + )); |
| 477 | + } |
| 478 | + } |
| 479 | + optionals.push(' '); |
| 480 | + match datetime { |
| 481 | + None => (), |
| 482 | + Some(datetime) => { |
| 483 | + optionals.push_str(&format!("\"{}\"", datetime.format("%d-%b-%Y %H:%M:%S %z"))) |
| 484 | + } |
| 485 | + } |
| 486 | + |
| 487 | + try!(self.run_command(&format!( |
| 488 | + "APPEND \"{}\" {} {{{}}}", |
| 489 | + folder, |
| 490 | + optionals, |
| 491 | + content.len() |
| 492 | + ))); |
454 | 493 | let line = try!(self.readline());
|
455 | 494 | if !line.starts_with(b"+") {
|
456 | 495 | return Err(Error::Append);
|
|
0 commit comments