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;
11
3
4
+ use clap:: { Args , Parser , Subcommand } ;
5
+ use commands:: { send} ;
12
6
13
- // use mime::APPLICATION_OCTET_STREAM;
14
- use std:: path:: Path ;
15
7
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
+ }
16
15
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
+ }
23
21
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) ;
30
22
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 ( ) ;
38
25
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 ( ) ;
43
29
}
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:?}" ) ,
72
30
}
73
-
74
- }
31
+ }
0 commit comments