10 releases

0.2.1 Oct 6, 2024
0.2.0 Sep 28, 2024
0.1.2 Sep 16, 2024
0.0.9 Sep 4, 2024
0.0.7 Aug 21, 2024

#86 in Email

Download history 85/week @ 2024-08-04 130/week @ 2024-08-11 164/week @ 2024-08-18 10/week @ 2024-08-25 398/week @ 2024-09-01 185/week @ 2024-09-08 256/week @ 2024-09-15 91/week @ 2024-09-22 121/week @ 2024-09-29 166/week @ 2024-10-06 3/week @ 2024-10-13

369 downloads per month

MIT license

8KB
100 lines

crates.io Documentation

Dependencies

spring-mail = { version = "0.1.1" }

Configuration items

[mail]
host = "smtp.gmail.com"    # SMTP mail server address,
port = 465                 # SMTP server port number
secure = true              # Response timeout, in milliseconds
auth = { user = "user@gmail.com", password = "passwd" } # Authentication information

Components

After configuring the above configuration items, the plugin will automatically register a MailerSTMP asynchronous client. This object is an alias of lettre::AsyncSmtpTransport<Tokio1Executor>.

pub type Mailer = lettre::AsyncSmtpTransport<Tokio1Executor>;

Extract the Component registered by the plugin

The MailPlugin plugin automatically registers an SMTP client for us. We can use Component to extract this connection pool from AppState. Component is an axum extractor.

async fn send_mail(Component(mailer): Component<Mailer>) -> Result<impl IntoResponse> {
    let email = Message::builder()
        .from("NoBody <nobody@domain.tld>".parse().unwrap())
        .reply_to("Yuin <yuin@domain.tld>".parse().unwrap())
        .to("hff1996723@163.com".parse().unwrap())
        .subject("Happy new year")
        .header(ContentType::TEXT_PLAIN)
        .body(String::from("Be happy!"))
        .unwrap();
    let resp = mailer.send(email).await.context("send mail failed")?;
    Ok(Json(resp))
}

For the complete code, please refer to mail-example

Dependencies

~15–46MB
~715K SLoC