Initial commit
This commit is contained in:
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
/target
|
||||
.idea
|
1242
Cargo.lock
generated
Normal file
1242
Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
8
Cargo.toml
Normal file
8
Cargo.toml
Normal file
@ -0,0 +1,8 @@
|
||||
[package]
|
||||
name = "mail-sender"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
clap = { version = "4.5.40", features = ["derive"] }
|
||||
lettre = "0.11.17"
|
99
src/main.rs
Normal file
99
src/main.rs
Normal file
@ -0,0 +1,99 @@
|
||||
use clap::Parser;
|
||||
use lettre::message::header::ContentType;
|
||||
use lettre::message::Mailbox;
|
||||
use lettre::{Message, SmtpTransport, Transport};
|
||||
|
||||
/// Simple mail sender
|
||||
#[derive(Parser, Debug)]
|
||||
#[command(version, about, long_about = None)]
|
||||
struct Args {
|
||||
/// Src mail
|
||||
#[arg(short, long)]
|
||||
from_mail: String,
|
||||
|
||||
/// Src name
|
||||
#[arg(long)]
|
||||
from_name: Option<String>,
|
||||
|
||||
/// Dst mail
|
||||
#[arg(long)]
|
||||
to_mail: String,
|
||||
|
||||
/// Dst name
|
||||
#[arg(long)]
|
||||
to_name: Option<String>,
|
||||
|
||||
/// Reply to mail
|
||||
#[arg(long)]
|
||||
reply_to_mail: Option<String>,
|
||||
|
||||
/// Reply to name
|
||||
#[arg(long)]
|
||||
reply_to_name: Option<String>,
|
||||
|
||||
/// Subject
|
||||
#[arg(long)]
|
||||
subject: String,
|
||||
|
||||
/// Text body
|
||||
#[arg(long)]
|
||||
text_body: String,
|
||||
|
||||
/// SMTP domain
|
||||
#[arg(long)]
|
||||
smtp_domain: String,
|
||||
|
||||
/// SMTP port
|
||||
#[arg(long)]
|
||||
smtp_port: Option<u16>,
|
||||
|
||||
/// Disable TLS
|
||||
#[arg(long)]
|
||||
disable_tls: bool,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let args = Args::parse();
|
||||
|
||||
let mut builder = Message::builder();
|
||||
builder = builder.from(Mailbox::new(
|
||||
args.from_name,
|
||||
args.from_mail.parse().unwrap(),
|
||||
));
|
||||
if let Some(reply_to_mail) = args.reply_to_mail {
|
||||
builder = builder.reply_to(Mailbox::new(
|
||||
args.reply_to_name,
|
||||
reply_to_mail.parse().unwrap(),
|
||||
));
|
||||
}
|
||||
|
||||
builder = builder
|
||||
.to(Mailbox::new(
|
||||
args.to_name.clone(),
|
||||
args.to_mail.parse().unwrap(),
|
||||
))
|
||||
.subject(args.subject)
|
||||
.header(ContentType::TEXT_PLAIN);
|
||||
let email = builder.body(args.text_body).unwrap();
|
||||
|
||||
// Open a remote connection to gmail
|
||||
let mut mailer = match args.disable_tls {
|
||||
true => SmtpTransport::builder_dangerous(&args.smtp_domain),
|
||||
false => SmtpTransport::relay(&args.smtp_domain).unwrap(),
|
||||
};
|
||||
if let Some(port) = args.smtp_port {
|
||||
mailer = mailer.port(port);
|
||||
}
|
||||
|
||||
let mailer = mailer.build();
|
||||
|
||||
// Send the email
|
||||
match mailer.send(&email) {
|
||||
Ok(d) => println!(
|
||||
"Email sent successfully! code={} message={:?}",
|
||||
d.code(),
|
||||
d.message().collect::<Vec<_>>()
|
||||
),
|
||||
Err(e) => panic!("Could not send email: {e:?}"),
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user