allow optional CC

This commit is contained in:
Ryan Heywood 2024-09-10 16:48:29 -04:00
parent c2d001b060
commit b2631babd8
Signed by: ryan
GPG Key ID: 8E401478A3FBEF72
1 changed files with 15 additions and 9 deletions

View File

@ -48,6 +48,7 @@ struct State {
username: String, username: String,
password: String, password: String,
target_address: String, target_address: String,
cc: Option<String>,
} }
#[tokio::main] #[tokio::main]
@ -55,11 +56,13 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let username = std::env::var("WEBMAIL_USERNAME")?; let username = std::env::var("WEBMAIL_USERNAME")?;
let password = std::env::var("WEBMAIL_PASSWORD")?; let password = std::env::var("WEBMAIL_PASSWORD")?;
let target_address = std::env::var("WEBMAIL_TARGET")?; let target_address = std::env::var("WEBMAIL_TARGET")?;
let cc = std::env::var("WEBMAIL_CC").ok();
let state = State { let state = State {
username, username,
password, password,
target_address, target_address,
cc,
}; };
let app = Router::new() let app = Router::new()
@ -125,14 +128,17 @@ async fn send_email(form_data: FormData, state: &State) -> Result<(), Box<dyn Er
]; ];
let body = content.map(|(a, b)| format!("{a}: {b}")).join("\n"); let body = content.map(|(a, b)| format!("{a}: {b}")).join("\n");
println!("Body: {body}"); let mut email_builder = Message::builder()
let email = Message::builder()
.from(username.parse()?) .from(username.parse()?)
.reply_to(email) .reply_to(email)
.to(state.target_address.parse()?) .to(state.target_address.parse()?)
.subject("New Website Form Inquiry") .subject("New Website Form Inquiry");
.singlepart(
if let Some(cc) = state.cc.as_ref() {
email_builder = email_builder.cc(cc.parse()?);
}
let email = email_builder.singlepart(
SinglePart::builder() SinglePart::builder()
.header(ContentType::TEXT_PLAIN) .header(ContentType::TEXT_PLAIN)
.body(body), .body(body),