Hi all this is my first topic on this forum as beginner in programming and specially rust. After reading the book and following the rustlings examples, I started to make my first little program.
I will try to formulate my question as accurately as possible.
I want to send an email to multiple receivers, with a variable content as body. I made the following code with the lettre crate, and it works fine if i want to send it to 1 user:
extern crate chrono;
extern crate lettre;
extern crate json_macros;
use std::io;
use std::io::prelude::*;
use chrono::prelude::*;
use lettre::transport::smtp::authentication::Credentials;
use lettre::{Message, SmtpTransport, Transport};
fn main() {
pause_start();
// Must be send to following adresses, based on json file/format
let to_adresses = json!({
"To Reciever 1": "to.reciever.1@domain.com",
"To Reciever 2": "to.reciever.2@domain.com",
});
let dw_subject = format!("Message send on {}", Local::now().format("%A %e %B %Y"));
let dw_body = format!("{}", someinput);
let email = Message::builder()
.from("From Sender <from.sender0@domain.com>".parse().unwrap())
.to("To Reciever 1<to.reciever.1@domain.com>".parse().unwrap())
.subject(dw_subject)
.body(dw_body)
.unwrap();
let creds = Credentials::new(
"from.sender0@domain.com".to_string(),
"VeryComplicatedPassword".to_string(),
);
// Open a remote connection to gmail
let mailer = SmtpTransport::relay("smtp.domain.com")
.unwrap()
.credentials(creds)
.build();
// Send the email
match mailer.send(&email) {
Ok(_) => println!("mail send!"),
Err(e) => panic!("Mail not send because: {:?}", e),
};
pause_end();
}
I did try to use a vector and a for loop with no success.(I also think not optimal because the code has to generate message :: builder several times)
Need some advice how to implement this.