Worker for sending emails

We use a separate thread that receives SendableEmail values and send them using the SMTP protocol. The following code creates an instance of SmtpClient and uses the credentials method to set the credentials for a connection:

fn spawn_sender() -> Sender<SendableEmail> {
let (tx, rx) = channel();
let smtp = SmtpClient::new("localhost:2525", ClientSecurity::None)
.expect("can't start smtp client");
let credentials = ("[email protected]", "password").into_credentials();
let client = smtp.credentials(credentials);
thread::spawn(move || {
let mut mailer = SmtpTransport::new(client);
for email in rx.iter() {
let result = mailer.send(email);
if let Err(err) = result {
println!("Can't send mail: {}", err);
}
}
mailer.close();
});
tx
}

StmpClient has moved to the new thread's context. It's wrapped with SmtpTransport and is used to send every received SendableEmail instance.

The worker implements a non-transactional email sender. If you want a guarantee of email delivery, you need to implement more diverse interaction with a mail server, or you can even embed an email server, or use a third-party service. I recommend you use as many external services as possible; they will cost you, but you will save much more on maintenance. We implemented the mailer service for demonstration purposes only to show how to integrate multiple services together later.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.35.255