|
|
|
@ -2,7 +2,7 @@ use chrono::{DateTime, Datelike, Utc};
|
|
|
|
|
use chrono_tz::US::Central;
|
|
|
|
|
use clap::Parser;
|
|
|
|
|
use lettre::{
|
|
|
|
|
message::{Message, header::ContentType, Attachment, MultiPart, SinglePart},
|
|
|
|
|
message::{header::ContentType, Attachment, Message, 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);
|
|
|
|
|
send_email_summary(&cfg, body_content, &args);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
@ -236,6 +236,9 @@ struct CliArgs {
|
|
|
|
|
#[arg(short, long)]
|
|
|
|
|
config_file: PathBuf,
|
|
|
|
|
|
|
|
|
|
#[arg(short, long)]
|
|
|
|
|
from_addr: String,
|
|
|
|
|
|
|
|
|
|
#[arg(short, long)]
|
|
|
|
|
debug: bool,
|
|
|
|
|
}
|
|
|
|
@ -299,14 +302,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>) {
|
|
|
|
|
fn send_email_summary(config: &Config, body_content: Vec<String>, cliargs: &CliArgs) {
|
|
|
|
|
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: {line}");
|
|
|
|
|
error!("There was an issue with this line. 3 splits not found: {line}");
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
let date: String = match split.pop() {
|
|
|
|
@ -355,36 +358,70 @@ fn send_email_summary(config: &Config, body_content: Vec<String>) {
|
|
|
|
|
.to_string()
|
|
|
|
|
.replace("### ", "");
|
|
|
|
|
|
|
|
|
|
wtr.serialize(SummaryRow { date: date.trim().to_string(), total_time: time, task_name }).unwrap();
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let data = String::from_utf8(wtr.into_inner().unwrap()).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;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
debug!("{:#?}", data);
|
|
|
|
|
|
|
|
|
|
let attachment = Attachment::new("TimeSummary.csv".to_string()).body(data, ContentType::parse("text/csv").unwrap());
|
|
|
|
|
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>
|
|
|
|
|
<!DOCTYPE html>
|
|
|
|
|
<html lang="en">
|
|
|
|
|
<head>
|
|
|
|
|
<meta charset="UTF-8">
|
|
|
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
|
|
|
<title>Hello from Lettre!</title>
|
|
|
|
|
<title>ChronoTrack Export</title>
|
|
|
|
|
</head>
|
|
|
|
|
<body>
|
|
|
|
|
<div style="display: flex; flex-direction: column; align-items: center;">
|
|
|
|
|
<h2 style="font-family: Arial, Helvetica, sans-serif;">Hello from Lettre!</h2>
|
|
|
|
|
<h4 style="font-family: Arial, Helvetica, sans-serif;">A mailer library for Rust</h4>
|
|
|
|
|
<div style="background-color: #33ccff; border-radius: 40px; height: 100px"></div>
|
|
|
|
|
<div style="display: flex; flex-direction: column; align-items: center; ">
|
|
|
|
|
<img src="https://cdn-icons-png.flaticon.com/512/3938/3938540.png"
|
|
|
|
|
style="height:100px; border-radius: 50%; background-color: whitesmoke; position: absolute; top: 40px;">
|
|
|
|
|
<h2 style="font-family: Arial, Helvetica, sans-serif; position: absolute; top: 160px;">Prepared with care, and sent through the horrors of the interwebs, ChronoTrack presents! (see attached)
|
|
|
|
|
</div>
|
|
|
|
|
</body>
|
|
|
|
|
</html>"#;
|
|
|
|
|
|
|
|
|
|
let email = Message::builder()
|
|
|
|
|
.from("NoReply@nickiel.net <noreply@nickiel.net>".parse().unwrap())
|
|
|
|
|
.to(config.email_addr.parse().unwrap())
|
|
|
|
|
.subject("Testing email")
|
|
|
|
|
.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")
|
|
|
|
|
.multipart(
|
|
|
|
|
MultiPart::mixed()
|
|
|
|
|
.multipart(
|
|
|
|
@ -392,23 +429,22 @@ fn send_email_summary(config: &Config, body_content: Vec<String>) {
|
|
|
|
|
.singlepart(
|
|
|
|
|
SinglePart::builder()
|
|
|
|
|
.header(ContentType::TEXT_PLAIN)
|
|
|
|
|
.body(String::from("Hello world"))
|
|
|
|
|
.body(String::from("This is an automated email from ChronoTrack")),
|
|
|
|
|
)
|
|
|
|
|
.singlepart(
|
|
|
|
|
SinglePart::builder()
|
|
|
|
|
.header(ContentType::TEXT_HTML)
|
|
|
|
|
.body(String::from(HTML))
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
.singlepart(attachment)
|
|
|
|
|
).unwrap();
|
|
|
|
|
|
|
|
|
|
.body(String::from(HTML)),
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
.singlepart(attachment),
|
|
|
|
|
)
|
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
|
|
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),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|