-
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.
- Loading branch information
Showing
5 changed files
with
102 additions
and
298 deletions.
There are no files selected for viewing
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,35 +1,48 @@ | ||
use std::io::prelude::*; | ||
use std::fs::OpenOptions; | ||
use std::path::Path; | ||
use std::fs::File; | ||
|
||
pub fn write_to_file(text: String, file_path: &str) -> Result<(), String> { | ||
use async_std::fs::OpenOptions; | ||
use async_std::path::Path; | ||
use async_std::fs::File; | ||
use async_std::prelude::*; | ||
|
||
pub async fn write_to_file(text: String, file_path: &str) -> Result<(), String> { | ||
let mut file = OpenOptions::new() | ||
.append(true) | ||
.create(true) | ||
.open(file_path) | ||
.await | ||
.map_err(|e| e.to_string())?; | ||
|
||
write!(file, "{} ", text).map_err(|e| e.to_string()) | ||
file.write_all(text.as_bytes()).await.map_err(|e| e.to_string()) | ||
} | ||
|
||
pub fn update_credentials(username: String, password: String) { | ||
pub async fn update_credentials(username: String, password: String) -> Result<(), String> { | ||
let file_path = "config/smtp_account.txt"; | ||
let path = Path::new(file_path); | ||
|
||
let mut content = String::new(); | ||
{ | ||
let mut file = File::open(path).expect("An error occurs when tried to open a file"); | ||
file.read_to_string(&mut content).expect("An error occurs when tried to read the file"); | ||
let mut file = File::open(path) | ||
.await | ||
.map_err(|e| e.to_string())?; | ||
file.read_to_string(&mut content) | ||
.await | ||
.map_err(|e| e.to_string())?; | ||
} | ||
|
||
let updated_content = content | ||
.replace("username:[email protected]", &format!("username:{}", username)) | ||
.replace("password:password123123", &format!("password:{}", password)); | ||
|
||
{ | ||
let mut file = OpenOptions::new().write(true).truncate(true).open(&path).expect("An error occurs when tried to open a file with OpenOptions"); | ||
file.write_all(updated_content.as_bytes()).expect("An error occurs when tried to update content"); | ||
let mut file = OpenOptions::new() | ||
.write(true) | ||
.truncate(true) | ||
.open(&path) | ||
.await | ||
.map_err(|e| e.to_string())?; | ||
file.write_all(updated_content.as_bytes()) | ||
.await | ||
.map_err(|e| e.to_string())?; | ||
} | ||
|
||
Ok(()) | ||
} |
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 |
---|---|---|
|
@@ -5,7 +5,7 @@ use lettre::{ | |
use crate::file_reader::smtp_account_reader; | ||
use crate::file_writer::write_to_file; | ||
|
||
pub fn mail_sender(title: &str, text: String) { | ||
pub async fn mail_sender(title: &str, text: String) { | ||
let email = Message::builder() | ||
.from("ServerAPI <[email protected]>".parse().expect("Error parsing sender in Message Builder")) | ||
// .reply_to("Yuin <[email protected]>".parse().unwrap()) | ||
|
@@ -28,14 +28,14 @@ pub fn mail_sender(title: &str, text: String) { | |
// Send the email | ||
match mailer.send(&email) { | ||
Ok(_) => { | ||
match write_to_file(format!("Email has been sent successfully!"), "config/mail_logs.txt") { | ||
match write_to_file(format!("Email has been sent successfully!"), "config/mail_logs.txt").await { | ||
Ok(()) => {}, | ||
n => println!("An error occured when tried to write logs (email success): {n:?}") | ||
} | ||
println!("Email has been sent successfully!"); | ||
}, | ||
Err(e) => { | ||
match write_to_file(format!("Could not send email: {e:?}"), "config/mail_logs.txt") { | ||
match write_to_file(format!("Could not send email: {e:?}"), "config/mail_logs.txt").await { | ||
Ok(()) => {}, | ||
n => println!("An error occured when tried to write logs (email failure): {n:?}") | ||
} | ||
|
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
Oops, something went wrong.