Parse multiple input based on jsonfile with lettre

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.

First, welcome to Rust and to this forum. You will find many people on this forum who are willing to help.

Second, please edit your Rust code in the above post so that if follows the Forum Code Formatting and Syntax Highlighting guidelines, which are pinned to the top of this forum so that all new posters will see them. You can edit any of your own posts by clicking on the pencil-shaped "edit this post" button under your posts to apply correct formatting.

Most readers of this forum view with distaste code snippets that do not follow those posting guidelines. Not following the guidelines disrespects all forum readers, including those whom you are asking for advice. Thanks. :clap:

2 Likes

Can't figure it out yet. :thinking:

lettre has an struct Mailboxes it seems it can handle multiple username and email adresses as a vector.

I tried multiple ways to implement this but I still cant figure it out. :flushed: feeling little bit stupid...

can someone give me a hint/directions?

I solved it with multiple .to argements

let email = Message::builder()
            .from("From Sender <from.sender0@domain.com>".parse().unwrap())
            .to("To Reciever 1<to.reciever.1@domain.com>".parse().unwrap())
            .to("To Reciever 2<to.reciever.2@domain.com>".parse().unwrap())
            .subject(dw_subject)
            .body(dw_body)
            .unwrap();

Not a clean solution but it works fine for know :flushed: