Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new file: backend/examples/crabfeeder.rs #82

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,16 @@ services:
networks: [default]
```

## Sample messages

The `samples` directory contains a couple of test messages.
These can be sent using by running:

```sh
cd backend/
cargo run --example crabfeeder ../samples/*l
```

## Development

Install [Rust](https://www.rust-lang.org/learn/get-started) and [Trunk](https://trunkrs.dev/)
Expand Down
65 changes: 65 additions & 0 deletions backend/examples/crabfeeder.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* crabfeeder
* a SMTP client
* for injecting (test) emails into mailcrab
*/
use core::str::FromStr;
use lettre::address::Envelope;
use lettre::{Address, SmtpTransport, Transport};
use std::env;
use std::net::IpAddr;

fn usage(name: &str) {
println!(
r#"{name} <filenames>

and those filenames will be injected as email into mailcrab.

You can configure the connection destination with setting
environment variables SMTP_SERVER and SMTP_PORT.
"#
)
}

fn main() {
let args: Vec<String> = env::args().collect();
if args.len() > 1 {
process(args[1..].to_vec());
} else {
usage(&args[0]);
}
}

/// get a configuration from the environment or return default value
fn parse_env_var<T: FromStr>(name: &'static str, default: T) -> T {
env::var(name)
.unwrap_or_default()
.parse::<T>()
.unwrap_or(default)
}

fn process(filenames: Vec<String>) {
let smtp_server: IpAddr = parse_env_var("SMTP_SERVER", [127, 0, 0, 1].into());
let smtp_port: u16 = parse_env_var("SMTP_PORT", 1025);
println!("I: Will connecting {:?}:{:?}", smtp_server, smtp_port);
let mailer = SmtpTransport::builder_dangerous(smtp_server.to_string())
.port(smtp_port)
.build();

for f in filenames.iter() {
let message = std::fs::read_to_string(f).unwrap();
let lines = message.lines();

let sender = "carbfeeder@carbfeeder".parse::<Address>().unwrap();
let recipients = "mailcrab@mailcrab"
.split(',')
.map(|r| r.trim().parse::<Address>().unwrap())
.collect::<Vec<Address>>();
let envelope = Envelope::new(Some(sender), recipients).unwrap();

let email = lines.collect::<Vec<&str>>().join("\n");

mailer.send_raw(&envelope, email.as_bytes()).unwrap();
println!("I: Having send {f}");
}
}
53 changes: 53 additions & 0 deletions backend/examples/multiple_rcpt_to.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* multiple_rcpt_to
* a SMTP client
* that injects a (test) email into mailcrab
* for multiple recipients
*/
use core::str::FromStr;
use lettre::address::Envelope;
use lettre::{Address, SmtpTransport, Transport};
use std::env;
use std::net::IpAddr;

/// get a configuration from the environment or return default value
fn parse_env_var<T: FromStr>(name: &'static str, default: T) -> T {
env::var(name)
.unwrap_or_default()
.parse::<T>()
.unwrap_or(default)
}

fn main() {
let smtp_server: IpAddr = parse_env_var("SMTP_SERVER", [127, 0, 0, 1].into());
let smtp_port: u16 = parse_env_var("SMTP_PORT", 1025);
println!("I: Will connecting {:?}:{:?}", smtp_server, smtp_port);
let mailer = SmtpTransport::builder_dangerous(smtp_server.to_string())
.port(smtp_port)
.build();

let sender = "multi_rcpt_to@examples".parse::<Address>().unwrap();
let recipients = "many@mailcrab,foo@bar,foo@baz,[email protected],[email protected]"
.split(',')
.map(|r| r.trim().parse::<Address>().unwrap())
.collect::<Vec<Address>>();
let envelope = Envelope::new(Some(sender), recipients).unwrap();

let email = r#"From: Many RCPT TO <recipient@metadata>
To: See Envelope <[email protected]>
Subject: Multiple RCPT TO in one SMTP session

Hi,

This message is for checking
how mailcrab handles multiple RCPT TO in one SMTP session.

Inspirated by the commit you can see with
git log --patch 0699315cb2509^1..0699315cb2509

Bye
"#;

mailer.send_raw(&envelope, email.as_bytes()).unwrap();
println!("I: Having send one email");
}