Version: v0.9.0

Mailer / SMTP

Curently Kalgan only supports email sending using SMTP. This task is done by service kalgan::service::mailer::send_email(), which basically it's a wrapper for crate lettre.

Configuring Mailer

Firstly, we must add the feature mailer in Cargo.toml:

...
[dependencies]
kalgan = { version = "0.9.0", features = ["mailer"] }

And then we must set in our settings file the parameters of our SMTP server.

Let's see an example for Gmail SMTP Server:

...
mailer:
  sender: John Doe <john.doe@gmail.com>
  server: smtp.gmail.com
  user: john.doe@gmail.com
  password: my_password
...

Find a description of these fields in the table below:

mailer.sender Sender email address.
mailer.server IP/Domain of the SMTP Server. Port number can be specified with a colon.
mailer.user User name of the SMTP Server.
mailer.password User password of the SMTP Server.

Be aware that while we're developing ( environment.is_prod: false ) any change made in these parameters is taken on the fly and there's no need to restart the app.

Using Mailer

As we have already said, Kalgan provides the service kalgan::service::mailer::send_email() to send emails.

This function reads the settings parameters defined above and takes three arguments:

  • The email of the addressee.
  • Subject of the email.
  • The body of the email.

Let's see an example:

use kalgan::service::mailer;

async fn send_email() {
    match mailer::send_email(
        "email_of_the_addressee@foo.bar",
        "Subject of the Email",
        "Body of the Email"
    ).await {
        Ok(()) => { ... },
        Err(e) => { ... }
    }
}
            

Notice that file attachment is not supported currently.