Skip to content

Commit fc9e54c

Browse files
committed
built CLI backbone
1 parent f150b58 commit fc9e54c

File tree

5 files changed

+208
-66
lines changed

5 files changed

+208
-66
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
/target
2-
.env
2+
.env
3+
*.png

Cargo.lock

+113
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ edition = "2021"
66
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
77

88
[dependencies]
9+
clap = { version = "4.5.1", features = ["derive"] }
910
dotenv = "0.15.0"
1011
lettre = "0.10.0-rc.3"
1112
lettre_email = "0.9.4"

src/commands.rs

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// commands.rs
2+
use lettre::{Message, SmtpTransport, Transport};
3+
use lettre::transport::smtp::authentication::Credentials;
4+
use lettre::message::Mailbox;
5+
use lettre::address::Address;
6+
use dotenv::dotenv;
7+
use std::fs;
8+
use lettre::message::{Attachment, header::ContentType, };
9+
use lettre::message::{MultiPart, SinglePart};
10+
use mime_guess::from_path;
11+
use std::path::Path;
12+
13+
14+
pub fn send() {
15+
dotenv().ok();
16+
let smtp_username = "[email protected]";
17+
let smtp_password = std::env::var("SMTP_TOKEN").expect("SMTP_TOKEN must be set.").to_string();
18+
// let smtp_port = 587;
19+
20+
let filename = String::from("image.png");
21+
let file_path = Path::new("image.png");
22+
let filebody = fs::read("image.png").unwrap();
23+
let mime_guess = from_path(file_path).first_or_octet_stream();
24+
let content_type = ContentType::parse(&mime_guess.to_string()).unwrap();
25+
let attachment = Attachment::new(filename).body(filebody, content_type);
26+
27+
28+
let from_address = match Address::new("shahrishi108", "gmail.com") {
29+
Ok(addr) => addr,
30+
Err(e) => {
31+
panic!("Failed to create address: {}", e);
32+
}
33+
};
34+
35+
let to_address = match Address::new("space.198tyler", "gmail.com") {
36+
Ok(addr) => addr,
37+
Err(e) => {
38+
panic!("Failed to create address: {}", e);
39+
}
40+
};
41+
42+
let from_user = Mailbox::new(None, from_address.clone());
43+
let to_user = Mailbox::new(None, to_address.clone());
44+
45+
let email = Message::builder()
46+
.from(from_user)
47+
.to(to_user)
48+
.subject("hello world!")
49+
.multipart(
50+
MultiPart::mixed()
51+
.singlepart(SinglePart::html(String::from("Hey, here's your file from Atmail!")))
52+
.singlepart(attachment)
53+
).unwrap();
54+
55+
56+
let creds = Credentials::new(smtp_username.to_string(), smtp_password.to_string());
57+
58+
// Open a remote connection to gmail
59+
let mailer = SmtpTransport::relay("smtp.gmail.com")
60+
.unwrap()
61+
.credentials(creds)
62+
.build();
63+
64+
// Send the email
65+
match mailer.send(&email) {
66+
Ok(_) => println!("Email sent successfully!"),
67+
Err(e) => panic!("Could not send email: {e:?}"),
68+
}
69+
70+
}

src/main.rs

+22-65
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,31 @@
1-
use lettre::{Message, SmtpTransport, Transport};
2-
use lettre::transport::smtp::authentication::Credentials;
3-
use lettre::message::Mailbox;
4-
use lettre::address::Address;
5-
use dotenv::dotenv;
6-
use std::fs;
7-
use lettre::message::{Attachment, header::ContentType};
8-
use lettre::message::MultiPart;
9-
use lettre::message::SinglePart;
10-
use mime_guess::from_path;
1+
// main.rs
2+
mod commands;
113

4+
use clap::{Args, Parser, Subcommand};
5+
use commands::{send};
126

13-
// use mime::APPLICATION_OCTET_STREAM;
14-
use std::path::Path;
157

8+
// Shared bin controller
9+
#[derive(Parser)]
10+
#[command(version = "1.0", author = "Rishi Shah <[email protected]>", about = "Send files through your CL")]
11+
struct Cli {
12+
#[command(subcommand)]
13+
command: Commands,
14+
}
1615

17-
fn main() {
18-
dotenv().ok();
19-
let smtp_username = "[email protected]";
20-
let smtp_password = std::env::var("SMTP_TOKEN").expect("SMTP_TOKEN must be set.").to_string();
21-
// let filename = String::from("test.txt");
22-
// let smtp_port = 587;
16+
#[derive(Subcommand)]
17+
enum Commands {
18+
/// Send your file
19+
Send,
20+
}
2321

24-
let filename = String::from("image.png");
25-
let file_path = Path::new("image.png");
26-
let filebody = fs::read("image.png").unwrap();
27-
let mime_guess = from_path(file_path).first_or_octet_stream();
28-
let content_type = ContentType::parse(&mime_guess.to_string()).unwrap();
29-
let attachment = Attachment::new(filename).body(filebody, content_type);
3022

31-
32-
let from_address = match Address::new("shahrishi108", "gmail.com") {
33-
Ok(addr) => addr,
34-
Err(e) => {
35-
panic!("Failed to create address: {}", e);
36-
}
37-
};
23+
fn main() {
24+
let cli: Cli = Cli::parse();
3825

39-
let to_address = match Address::new("shahrishi108", "gmail.com") {
40-
Ok(addr) => addr,
41-
Err(e) => {
42-
panic!("Failed to create address: {}", e);
26+
match &cli.command {
27+
Commands::Send => {
28+
send();
4329
}
44-
};
45-
46-
let from_user = Mailbox::new(None, from_address.clone());
47-
let to_user = Mailbox::new(None, to_address.clone());
48-
49-
let email = Message::builder()
50-
.from(from_user)
51-
.to(to_user)
52-
.subject("hello world!")
53-
.multipart(
54-
MultiPart::mixed()
55-
.singlepart(SinglePart::html(String::from("Hey, here's your file from Atmail!")))
56-
.singlepart(attachment)
57-
).unwrap();
58-
59-
60-
let creds = Credentials::new(smtp_username.to_string(), smtp_password.to_string());
61-
62-
// Open a remote connection to gmail
63-
let mailer = SmtpTransport::relay("smtp.gmail.com")
64-
.unwrap()
65-
.credentials(creds)
66-
.build();
67-
68-
// Send the email
69-
match mailer.send(&email) {
70-
Ok(_) => println!("Email sent successfully!"),
71-
Err(e) => panic!("Could not send email: {e:?}"),
7230
}
73-
74-
}
31+
}

0 commit comments

Comments
 (0)