|
|
|
@ -2,7 +2,7 @@ use chrono::{DateTime, Datelike, Utc};
|
|
|
|
|
use chrono_tz::US::Central;
|
|
|
|
|
use clap::Parser;
|
|
|
|
|
use lettre::{
|
|
|
|
|
message::{header::ContentType, Attachment, Message, MultiPart, SinglePart},
|
|
|
|
|
message::{Message, header::ContentType, Attachment, MultiPart, SinglePart},
|
|
|
|
|
SendmailTransport, Transport,
|
|
|
|
|
};
|
|
|
|
|
use log::{debug, error};
|
|
|
|
@ -196,7 +196,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|
|
|
|
if do_email_summary {
|
|
|
|
|
debug!("Send email selected");
|
|
|
|
|
reset_email_checkbox(&cfg);
|
|
|
|
|
send_email_summary(&cfg, body_content, &args);
|
|
|
|
|
send_email_summary(&cfg, body_content);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
@ -236,9 +236,6 @@ struct CliArgs {
|
|
|
|
|
#[arg(short, long)]
|
|
|
|
|
config_file: PathBuf,
|
|
|
|
|
|
|
|
|
|
#[arg(short, long)]
|
|
|
|
|
from_addr: String,
|
|
|
|
|
|
|
|
|
|
#[arg(short, long)]
|
|
|
|
|
debug: bool,
|
|
|
|
|
}
|
|
|
|
@ -302,14 +299,14 @@ struct SummaryRow {
|
|
|
|
|
// All calls after depend on the success of the one before, and cannot run
|
|
|
|
|
// without. So it would be too much work to make an error type to handle an error
|
|
|
|
|
// that would cause a crash anyways
|
|
|
|
|
fn send_email_summary(config: &Config, body_content: Vec<String>, cliargs: &CliArgs) {
|
|
|
|
|
fn send_email_summary(config: &Config, body_content: Vec<String>) {
|
|
|
|
|
let mut wtr = csv::Writer::from_writer(vec![]);
|
|
|
|
|
|
|
|
|
|
for line in body_content {
|
|
|
|
|
if line.starts_with("### ") {
|
|
|
|
|
let mut split: Vec<&str> = line.split('|').collect();
|
|
|
|
|
if split.len() != 3 {
|
|
|
|
|
error!("There was an issue with this line. 3 splits not found: {line}");
|
|
|
|
|
error!("There was an issue with this line: {line}");
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
let date: String = match split.pop() {
|
|
|
|
@ -358,97 +355,33 @@ fn send_email_summary(config: &Config, body_content: Vec<String>, cliargs: &CliA
|
|
|
|
|
.to_string()
|
|
|
|
|
.replace("### ", "");
|
|
|
|
|
|
|
|
|
|
match wtr.serialize(SummaryRow {
|
|
|
|
|
date: date.trim().to_string(),
|
|
|
|
|
total_time: time,
|
|
|
|
|
task_name,
|
|
|
|
|
}) {
|
|
|
|
|
Ok(_) => continue,
|
|
|
|
|
Err(e) => {
|
|
|
|
|
error!("There was an error serializing the csv, aborting: {e}");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
wtr.serialize(SummaryRow { date: date.trim().to_string(), total_time: time, task_name }).unwrap();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let data = match String::from_utf8(wtr.into_inner().unwrap()) {
|
|
|
|
|
Ok(val) => val,
|
|
|
|
|
Err(e) => {
|
|
|
|
|
error!("There was an error converting the csv writer to a string, aborting: {e}");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
let data = String::from_utf8(wtr.into_inner().unwrap()).unwrap();
|
|
|
|
|
|
|
|
|
|
debug!("{:#?}", data);
|
|
|
|
|
|
|
|
|
|
let attachment = Attachment::new("TimeSummary.csv".to_string())
|
|
|
|
|
// The unwrap is on a constant value
|
|
|
|
|
.body(data, ContentType::parse("text/csv").unwrap());
|
|
|
|
|
|
|
|
|
|
const HTML: &str = r#"
|
|
|
|
|
<!DOCTYPE html>
|
|
|
|
|
<html lang="en">
|
|
|
|
|
<head>
|
|
|
|
|
<meta charset="UTF-8">
|
|
|
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
|
|
|
<title>ChronoTrack Export</title>
|
|
|
|
|
</head>
|
|
|
|
|
<body>
|
|
|
|
|
<div style="background-color: #33ccff; border-radius: 40px; height: 100px;">
|
|
|
|
|
<div style="margin-left: auto; margin-right: auto;width: fit-content">
|
|
|
|
|
<img src="https://cdn-icons-png.flaticon.com/512/3938/3938540.png"
|
|
|
|
|
style="height:100px; border-radius: 50%; background-color: whitesmoke;">
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
<div>
|
|
|
|
|
<h2>Prepared with care, and sent through the horrors of the interwebs, ChronoTrack presents! (see attached)</h2>
|
|
|
|
|
</div>
|
|
|
|
|
</body>
|
|
|
|
|
</html>
|
|
|
|
|
"#;
|
|
|
|
|
let attachment = Attachment::new("TimeSummary.csv".to_string()).body(data, ContentType::parse("text/csv").unwrap());
|
|
|
|
|
|
|
|
|
|
let email = Message::builder()
|
|
|
|
|
.from(match cliargs.from_addr.parse() {
|
|
|
|
|
Ok(val) => val,
|
|
|
|
|
Err(e) => {
|
|
|
|
|
error!("The provided from_email address was unparsable:\n{e}");
|
|
|
|
|
std::process::exit(1);
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.to(match config.email_addr.parse() {
|
|
|
|
|
Ok(val) => val,
|
|
|
|
|
Err(e) => {
|
|
|
|
|
error!(
|
|
|
|
|
"The provided destination email in the configuration file was unparsable:\n{e}"
|
|
|
|
|
);
|
|
|
|
|
std::process::exit(1);
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.subject("ChronoTrack Export")
|
|
|
|
|
.from("NoReplay <noreply@nickiel.net>".parse().unwrap())
|
|
|
|
|
.to("Nicholas Young <nicholasyoungsumner@gmail.com>".parse().unwrap())
|
|
|
|
|
.subject("Testing email")
|
|
|
|
|
.header(ContentType::TEXT_PLAIN)
|
|
|
|
|
.multipart(
|
|
|
|
|
MultiPart::mixed()
|
|
|
|
|
.multipart(
|
|
|
|
|
MultiPart::alternative()
|
|
|
|
|
.singlepart(
|
|
|
|
|
SinglePart::builder()
|
|
|
|
|
.header(ContentType::TEXT_PLAIN)
|
|
|
|
|
.body(String::from("This is an automated email from ChronoTrack")),
|
|
|
|
|
)
|
|
|
|
|
.singlepart(
|
|
|
|
|
SinglePart::builder()
|
|
|
|
|
.header(ContentType::TEXT_HTML)
|
|
|
|
|
.body(String::from(HTML)),
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
.singlepart(attachment),
|
|
|
|
|
)
|
|
|
|
|
.unwrap();
|
|
|
|
|
.singlepart(SinglePart::plain("Hello World".to_string()))
|
|
|
|
|
.singlepart(attachment)
|
|
|
|
|
).unwrap();
|
|
|
|
|
|
|
|
|
|
let mailer = SendmailTransport::new();
|
|
|
|
|
|
|
|
|
|
let mailer = SendmailTransport::new();
|
|
|
|
|
|
|
|
|
|
match mailer.send(&email) {
|
|
|
|
|
Ok(val) => debug!("email sent: {:?}", val),
|
|
|
|
|
Err(e) => error!("Couldn't send email {}", e),
|
|
|
|
|
Err(e) => error!("Couldn't send email {}", e)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|