-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ready for Release 0.1
- Loading branch information
Showing
5 changed files
with
32 additions
and
9 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,4 @@ authors = ["h4llow3En <[email protected]>"] | |
[dependencies] | ||
json = "0.10.2" | ||
lettre = "0.6.0" | ||
libc = "0.2" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
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) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters