How To Send An Email in Rust Using An SMTP (Step-By-Step)

No Comments
Modified: 25.09.2023

Do you want to send an Email to your users in Rust? Learn how to do so by first setting up an SMTP using Brevo SMTP and then sending the first email in this step-by-step guide! Brevo offers a free tier with up to 300 free emails per day, which is enough for most small projects!

Don’t want to read? Watch the video instead!

Set up Brevo SMTP

Before we can send an email, we have to set up an SMTP service. For this guide, I am using Brevo because I have a good experience with them, and the 300 free emails per day are enough for my projects. You can use whatever SMTP server you have access to, though.

  1. Create an account for Brevo here (affiliate link).
  2. Log in to your account
  3. Click on your Profile and go to “SMTP & API”
    set up brevo smtp for pocketbase: goto smtp and api
  4. Click on “Generate a new SMTP key”
    set up brevo ( sendinblue) smtp for pocketbase: generate smtp key
  5. Enter a name for the key, for example, “rust-mail”
  6. Copy the key. You will need it in the next step

Send an Email in Rust

Now that we have set up Brevo, you need the following details from the last steps to be able to send an email using the SMTP:

For this guide, I will create a new project, install the lettre crate and then send an email using it. Follow these steps:

  1. cargo new send-mail-rust
  2. cargo add lettre
  3. Open the file main.rs inside the src directory
  4. Enter the following code:
use lettre::transport::smtp::authentication::Credentials;
use lettre::{Message, SmtpTransport, Transport};

fn main() {
    let smtp_key: &str = "<your-smtp-key>";
    let from_email: &str = "<your-email>";
    let to_email: &str = "<receiver-email>";
    let host: &str = "smtp-relay.sendinblue.com";


    let email: Message = Message::builder()
        .from(from_email.parse().unwrap())
        .to(to_email.parse().unwrap())
        .subject("Your subject")
        .body("Your body".to_string())
        .unwrap();

    let creds: Credentials = Credentials::new(from_email.to_string(), smtp_key.to_string());

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

    // Send the email
    match mailer.send(&email) {
        Ok(_) => println!("Email sent successfully!"),
        Err(e) => panic!("Could not send email: {:?}", e),
    }
}
  1. Finally, run the code with cargo run

With this, you are able to send emails using Rust and Brevo SMTP. Try it out now, and let me know what you think!

Conclusion

In this guide, we learned how to set up Brevo SMTP and use it to send an email in Rust. I hope the guide was helpful to you, and if you have any questions, feel free to ask.

Need help or want to share feedback? Join my discord community!

Don’t miss out on any updates or future guides by subscribing to my monthly newsletter.

Discussion (0)

Add Comment

Your email address will not be published. Required fields are marked *