spring-mail

Crates.iospring-mail
lib.rsspring-mail
version0.3.0
sourcesrc
created_at2024-08-09 09:47:52.387533
updated_at2024-12-01 03:19:39.821971
descriptionIntegrate lettre into spring-rs to send emails
homepage
repositoryhttps://github.com/spring-rs/spring-rs
max_upload_size
id1330651
size12,348
holmofy (holmofy)

documentation

README

crates.io Documentation

Dependencies

spring-mail = { version = "<version>" }

Configuration items

[mail]
host = "smtp.gmail.com"    # SMTP mail server address,
port = 465                 # SMTP server port number
secure = true              # Enable TLS
auth = { user = "user@gmail.com", password = "passwd" } # Authentication information
test_connection = false    # Whether to test mail server connection on startup

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

Commit count: 523

cargo fmt