Crate 'Lettre' : doesn't support multiple recipients?

Could you please share an example how to sent email to multiple recipients(separated by comma) using crate 'Lettre'.

I 'm passing a recipients string having multiple recipients separated by comma but it gives error of 'Invalid address'

============================
let to_address = "test1@gmail.com, test2@gmail.com";

let mut email = EmailBuilder::new()
.to(to_address)
.from(username)
.subject(subject)
.html(mail_body)
.build()
.unwrap();

Please help.

(I'm just going on documentation.)

EmailBuilder looks like a pretty old version of letter. Can you upgrade?

If so, does this work?

let to_address = "test1@gmail.com, test2@gmail.com";
// https://docs.rs/lettre/latest/lettre/message/struct.Mailboxes.html
let mailboxes: Mailboxes = to_address.parse().unwrap();
// https://docs.rs/lettre/latest/lettre/message/header/struct.To.html
let to_header: header::To = mailboxes.into();

// https://docs.rs/lettre/latest/lettre/message/struct.MessageBuilder.html
let mut email = EmailBuilder::new()
    .mailbox(mailboxes)
    .from(username)
    .subject(subject)
    // https://docs.rs/lettre/latest/lettre/message/struct.SinglePart.html#method.html
    .singlepart(SinglePart::html(mail_body))
    .unwrap();

P.s. you can wrap code in ``` blocks.

Hi,

I'm rewriting my code as per your suggestion:

[dependencies]
lettre = "0.10.1"
lettre_email = "0.9.4"

use lettre::transport::smtp::authentication::Credentials;
use lettre::{Message, SmtpTransport, Transport};
use lettre::message::Mailboxes;
use lettre::message::header;
use lettre_email::EmailBuilder;
use lettre::message::SinglePart;
use lettre::message::MessageBuilder;

let to_address = "test1@gmail.com; test2@ocr-inc.com";
let mailboxes: Mailboxes = to_address.parse().unwrap();
let to_header: header::To = mailboxes.into();

let email = MessageBuilder::new()
.mailbox(mailboxes)
.from("testfrom@gmail.com".parse().unwrap())
.subject("Happy new year")
.singlepart(SinglePart::html(mail_body))
.unwrap();

question: where is the variable 'to_header' to be used in the above code?

Still, it is giving error:


the trait bound Mailboxes: lettre::message::header::Header is not satisfied
--> src\main.rs:27:18
|
27 | .mailbox(mailboxes)
| ------- ^^^^^^^^^ the trait lettre::message::header::Header is not implemented for Mailboxes
| |
| required by a bound introduced by this call
|
= help: the following other types implement trait lettre::message::header::Header:
ContentDisposition
ContentId
ContentLocation
ContentTransferEncoding
ContentType
MimeVersion
UserAgent
lettre::message::header::Bcc
and 12 others
note: required by a bound in MessageBuilder::mailbox
cargo\registry\src\github.com-1ecc6299db9ec823\lettre-0.10.1\src\message\mod.rs:371:23
|
371 | pub fn mailbox<H: Header + MailboxesHeader>(self, header: H) -> Self {
| ^^^^^^ required by this bound in MessageBuilder::mailbox

error[E0277]: the trait bound Mailboxes: MailboxesHeader is not satisfied
--> src\main.rs:27:18
|
27 | .mailbox(mailboxes)
| ------- ^^^^^^^^^ the trait MailboxesHeader is not implemented for Mailboxes
| |
| required by a bound introduced by this call
|
= help: the following other types implement trait MailboxesHeader:
lettre::message::header::Bcc
lettre::message::header::Cc
lettre::message::header::From
lettre::message::header::ReplyTo
lettre::message::header::To
note: required by a bound in MessageBuilder::mailbox

Please format code and error according to the link in the previous reply, it's currently unreadable.

1 Like

[quote="anjani.maurya, post:3, topic:80813"]
[dependencies]
lettre = "0.10.1"
lettre_email = "0.9.4"

use lettre::transport::smtp::authentication::Credentials;
use lettre::{Message, SmtpTransport, Transport};
use lettre::message::Mailboxes;
use lettre::message::header;
use lettre_email::EmailBuilder;
use lettre::message::SinglePart;
use lettre::message::MessageBuilder;


let to_address = "[test1@gmail.com](mailto:test1@gmail.com); [test2@ocr-inc.com](mailto:test2@ocr-inc.com)";
let mailboxes: Mailboxes = to_address.parse().unwrap();
let to_header: header::To = mailboxes.into();

let email = MessageBuilder::new()
.mailbox(mailboxes)
.from("[testfrom@gmail.com](mailto:testfrom@gmail.com)".parse().unwrap())
.subject("Happy new year")
.singlepart(SinglePart::html(mail_body))
.unwrap();

question: where is the variable 'to_header' to be used in the above code?

Still, it is giving error:

the trait bound Mailboxes: lettre::message::header::Header is not satisfied
--> src\main.rs:27:18
|
27 | .mailbox(mailboxes)
| ------- ^^^^^^^^^ the trait lettre::message::header::Header is not implemented for Mailboxes
| |
| required by a bound introduced by this call
|
= help: the following other types implement trait lettre::message::header::Header:
ContentDisposition
ContentId
ContentLocation
ContentTransferEncoding
ContentType
MimeVersion
UserAgent
lettre::message::header::Bcc
and 12 others
note: required by a bound in MessageBuilder::mailbox
cargo\registry\src\github.com-1ecc6299db9ec823\lettre-0.10.1\src\message\mod.rs:371:23
|
371 | pub fn mailbox<H: Header + MailboxesHeader>(self, header: H) -> Self {
| ^^^^^^ required by this bound in MessageBuilder::mailbox
error[E0277]: the trait bound Mailboxes: MailboxesHeader is not satisfied
--> src\main.rs:27:18
|
27 | .mailbox(mailboxes)
| ------- ^^^^^^^^^ the trait MailboxesHeader is not implemented for Mailboxes
| |
| required by a bound introduced by this call
|
= help: the following other types implement trait MailboxesHeader:
lettre::message::header::Bcc
lettre::message::header::Cc
lettre::message::header::From
lettre::message::header::ReplyTo
lettre::message::header::To
note: required by a bound in MessageBuilder::mailbox 
use lettre::message::*;

fn main() {
    let to_address =
        "[test1@gmail.com](mailto:test1@gmail.com); [test2@ocr-inc.com](mailto:test2@ocr-inc.com)";
    let mailboxes: Mailboxes = to_address.parse().unwrap();
    let to_header: header::To = mailboxes.into();

    let mail_body = MaybeString::String("mail_body".into());

    let _email = MessageBuilder::new()
        .mailbox(to_header)
        .from(
            "[testfrom@gmail.com](mailto:testfrom@gmail.com)"
                .parse()
                .unwrap(),
        )
        .subject("Happy new year")
        .singlepart(SinglePart::html(mail_body))
        .unwrap();
}

if there is a second recipients in the 'to_address', it doesn't work at all:

let to_address = "[test1@gmail.com](mailto:test1@gmail.com); [test2@ocr-inc.com](mailto:test2@ocr-inc.com)";

for the second user, it gives the below error
error:  thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: InvalidUser', src\main.rs:26:51

if we just keep only one recipient in the 'to_address' it works.

Looking at the implementation of Mailboxes::from_str(), it seems like it expects the mailboxes to be separated by commas, rather than semicolons.

It doesn't matter whether it is comma or semi colon.

I tried putting comma in the 'to_address' but no success:

let to_address = "[test1@gmail.com](mailto:test1@gmail.com), [test2@ocr-inc.com](mailto:test2@ocr-inc.com)"

My apologies, it looks like Discourse turned the addresses into Markdown links. Looking at Mailbox::from_str(), your original to_address should work:

let to_address = "test1@gmail.com, test2@ocr-inc.com";

Optionally, names can be added to the addresses:

let to_address = "Alice <test1@gmail.com>, Bob <test2@ocr-inc.com>";
Now, it worked. Thanks everyone for your help. Appreciate your efforts and help.

The complete code as an example is given below:

[dependencies]
lettre = "0.10.1"
lettre_email = "0.9.4"

use lettre::transport::smtp::authentication::Credentials;
use lettre::{Message, SmtpTransport, Transport};
use lettre::message::Mailboxes;
use lettre::message::header;
use lettre_email::EmailBuilder;
use lettre::message::SinglePart;
use lettre::message::MessageBuilder;

pub fn main(){
let mail_body = "<h2>This is a test email</h2>";
let to_address = "Alice <test1@gmail.com>, Bob <test2@ocr-inc.com>";
let mailboxes: Mailboxes = to_address.parse().unwrap();
let to_header: header::To = mailboxes.into();

let email = MessageBuilder::new()
        .mailbox(to_header)
        .from("username@gmail.com".parse().unwrap())
        .subject("Happy new year")
        .singlepart(SinglePart::html(mail_body))
        .unwrap();

let creds = Credentials::new("username@gmail.com".to_string(), "gmail_password".to_string());

// Open a remote connection to gmail
let mailer = SmtpTransport::relay("smtp.gmail.com")
       .unwrap()
       .credentials(creds)
       .build();

match mailer.send(&email) {
       Ok(_) => println!("Email sent successfully!"),
       Err(e) => panic!("Could not send email: {:?}", e),
}
}