Add APPEND call. (#30)

This commit is contained in:
Jos van den Oever 2017-05-03 04:08:24 +02:00 committed by Matt McCoy
parent 3b6816b732
commit 08c2b6847b
2 changed files with 16 additions and 1 deletions

View file

@ -382,6 +382,18 @@ impl<T: Read+Write> Client<T> {
IdleHandle::new(self) IdleHandle::new(self)
} }
/// The APPEND command adds a mail to a mailbox.
pub fn append(&mut self, folder: &str, content: &[u8]) -> Result<Vec<String>> {
try!(self.run_command(&format!("APPEND \"{}\" {{{}}}", folder, content.len())));
let line = try!(self.readline());
if !line.starts_with(b"+") {
return Err(Error::Append);
}
try!(self.stream.write_all(content));
try!(self.stream.write_all(b"\r\n"));
self.read_response()
}
/// Runs a command and checks if it returns OK. /// Runs a command and checks if it returns OK.
pub fn run_command_and_check_ok(&mut self, command: &str) -> Result<()> { pub fn run_command_and_check_ok(&mut self, command: &str) -> Result<()> {
let lines = try!(self.run_command_and_read_response(command)); let lines = try!(self.run_command_and_read_response(command));

View file

@ -20,7 +20,9 @@ pub enum Error {
/// A NO response from the IMAP server. /// A NO response from the IMAP server.
NoResponse(Vec<String>), NoResponse(Vec<String>),
// Error parsing a server response. // Error parsing a server response.
Parse(ParseError) Parse(ParseError),
// Error appending a mail
Append
} }
impl From<IoError> for Error { impl From<IoError> for Error {
@ -53,6 +55,7 @@ impl StdError for Error {
Error::Parse(ref e) => e.description(), Error::Parse(ref e) => e.description(),
Error::BadResponse(_) => "Bad Response", Error::BadResponse(_) => "Bad Response",
Error::NoResponse(_) => "No Response", Error::NoResponse(_) => "No Response",
Error::Append => "Could not append mail to mailbox"
} }
} }