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
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.
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)"
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),
}
}