I'm trying to send same email to multiple users using lettre.rs, and do not want the app to panic if email could not be sent to any of the users, just need to be notified, and continue sending other emails, soI wrote this code:
use lettre::smtp::response::Response;
use lettre::smtp::error::Error;
let users = vec!("wrong@email", "correct@gmail.com");
let template = Email::builder()
.from("myEmail@gmail.com")
.subject("subject new")
.html("<h1>Hi man</h1>")
.text("message");
let creds = Credentials::new(
"myEmail@gmail.com".to_string(),
"myPassword".to_string(),
);
let mut mailer = SmtpClient::new_simple("smtp.gmail.com")
.unwrap()
.credentials(creds)
.transport();
for user in users {
let email = template.clone().to(user)
.build().unwrap();
let result: Result<Response, Error> = mailer.send(email.into());
match result {
Ok(_) => println!("Email sent to: {}", user),
Err(e) => println!("Could not send to: {}, email error: {:?}", user, e)
}
// OR
// if result.is_ok() {
// println!("Email sent to: {}", user);
// } else {
// println!("Could not send to: {}, email error: {:?}", user, result);
// }
}
But the app is panic and exit at the first wrong case it face, and I got this error:
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Envelope(InvalidEmailAddress)', src/libcore/result.rs:999:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace.
How can I avoid this panic?