summaryrefslogtreecommitdiff
path: root/src/main.rs
diff options
context:
space:
mode:
authorSanto Cariotti <santo@dcariotti.me>2020-12-04 20:07:09 +0100
committerSanto Cariotti <santo@dcariotti.me>2020-12-04 20:07:09 +0100
commite8a0cb618a5848ae930220f8152140bfdb4d6f6b (patch)
treed33854821f4d0c2fffd2e167477cb2b9376d22cc /src/main.rs
parent21817cfad161ecbb9c391ab05b4486835ea6785a (diff)
feat: send emails
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs45
1 files changed, 41 insertions, 4 deletions
diff --git a/src/main.rs b/src/main.rs
index f80069f..b1f36da 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,7 +1,12 @@
extern crate clap;
+extern crate lettre;
+
use clap::{App, Arg};
+use lettre::transport::smtp::authentication::Credentials;
+use lettre::{Message, SmtpTransport, Transport};
use rand::prelude::*;
use std::collections::HashMap;
+use std::env;
use std::fs;
fn my_gift_to(players: &mut Vec<String>) -> Option<&String> {
@@ -32,7 +37,24 @@ fn main() {
let contents = fs::read_to_string(config_file)
.expect("Error occurs reading this file")
.to_string();
- let mut members_data: Vec<&str> = contents.split("\n").collect();
+
+ let config_email;
+ let config_password;
+ let host;
+ match env::var("EMAIL") {
+ Ok(v) => config_email = v.to_string(),
+ Err(e) => panic!("Must provide EMAIL: {}", e),
+ }
+ match env::var("PASSWORD") {
+ Ok(v) => config_password = v.to_string(),
+ Err(e) => panic!("Must provide PASSWORD: {}", e),
+ }
+ match env::var("HOST") {
+ Ok(v) => host = v,
+ Err(e) => panic!("Must provide HOST: {}", e),
+ }
+
+ let mut members_data: Vec<&str> = contents.split(">\n").collect();
// ignore the last element, cause it's empty
members_data.pop();
@@ -42,7 +64,7 @@ fn main() {
let mut members = HashMap::new();
for member in members_data {
- let details: Vec<&str> = member.split(" ").collect();
+ let details: Vec<&str> = member.split("<").collect();
// <email> : <name>
members.insert(details[1], details[0]);
@@ -53,7 +75,13 @@ fn main() {
for (email, _) in &members {
emails.push(email.to_string());
}
- println!("{:?}", emails);
+
+ let creds = Credentials::new(config_email, config_password);
+
+ let mailer = SmtpTransport::relay(&host)
+ .unwrap()
+ .credentials(creds)
+ .build();
for (email, name) in members.into_iter() {
loop {
@@ -62,7 +90,16 @@ fn main() {
if gift_to.to_string() != email.to_string()
&& !done.iter().any(|v| v == gift_to)
{
- println!("{} {}", email, gift_to);
+ let mail = Message::builder()
+ .from("Santo Cariotti <santo@dcariotti.me>".parse().unwrap())
+ .to(format!("{} <{}>", name, email).parse().unwrap())
+ .subject("Secret Santa!")
+ .body(format!("You're the Secret Santa of:\n{}", name))
+ .unwrap();
+ match mailer.send(&mail) {
+ Ok(_) => println!("Email sent successfully to {}!", email),
+ Err(e) => panic!("Could not send email: {:?}", e),
+ }
done.push(gift_to.to_string());
break;
}