Skip to content

Commit

Permalink
better formatted mail body
Browse files Browse the repository at this point in the history
ready for Release 0.1
  • Loading branch information
h4llow3En committed Oct 25, 2016
1 parent bb756f4 commit 656b12e
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 9 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ authors = ["h4llow3En <[email protected]>"]
[dependencies]
json = "0.10.2"
lettre = "0.6.0"
libc = "0.2"
33 changes: 27 additions & 6 deletions src/mailing.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,48 @@
extern crate libc;
use std::{slice, io, ptr};
use lettre::transport::{smtp, EmailTransport};
use lettre::email::{Email, EmailBuilder};

pub fn gen_mail(content: &String, mailto: String, mailfrom: String) -> Email {
let mail = EmailBuilder::new()
.to(&mailfrom[..])
.from((&mailto[..], "tdo notify"))
.body(content)
.to(mailfrom.as_str())
.from((mailto.as_str(), "tdo notify"))
.body(&gen_body(content))
.subject("Your undone taks")
.build()
.unwrap();
mail
}

pub fn send_mail(mail: Email, server: String, user: String, pass: String, port: u16) -> bool {
let mut sender = smtp::SmtpTransportBuilder::new((&server[..], port))
let mut sender = smtp::SmtpTransportBuilder::new((server.as_str(), port))
.unwrap()
.credentials(&user, &pass)
.security_level(smtp::SecurityLevel::AlwaysEncrypt)
.smtp_utf8(true)
.connection_reuse(true)
.build();

let result = sender.send(mail);
println!("{:?}", result.is_ok());
result.is_ok()
}

fn gen_body(content: &String) -> String {
let mut head: String = format!("Hello {},\nhere are your undone tasks.\n\n\n", get_user_name().unwrap());
head.push_str(content);
head
}

fn get_user_name() -> Result<String, io::Error> {
unsafe {
let uid = libc::geteuid();
let user = ptr::read(libc::getpwuid(uid));
let name = String::from_utf8_unchecked(slice::from_raw_parts(user.pw_gecos as *const u8,
libc::strlen(user.pw_gecos) as usize)
.to_vec());
if name == "" {
Err(io::Error::last_os_error())
} else {
Ok(name)
}
}
}
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#[macro_use]
extern crate json;
extern crate lettre;
extern crate libc;
use std::env;
mod mailing;
mod settings;
Expand Down
5 changes: 2 additions & 3 deletions src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub struct Settings {
pub server: String,
pub user: String,
pub pass: String,
pub port: u16,
pub port: u16
}

impl Into<JsonValue> for Settings {
Expand All @@ -22,7 +22,6 @@ impl Into<JsonValue> for Settings {
"user" => self.user,
"pass" => self.pass,
"port" => self.port

}
}
}
Expand All @@ -41,7 +40,7 @@ impl Settings {
server: server,
user: user,
pass: pass,
port: port,
port: port
}
}
pub fn load(path: &String) -> Result<Settings, Error> {
Expand Down

0 comments on commit 656b12e

Please sign in to comment.