Rust Send Email

8 阅读1分钟
async fn send_notifcation_email() -> doe::DynError<()> {

    use doe::*;

    use dotenv;

    use lettre::transport::smtp::authentication::Credentials;

    use lettre::{Message, SmtpTransport, Transport};

    use lettre_email::EmailBuilder;

  


    dotenv::dotenv().ok();

    let SMTP_HOST = dotenv::var("SMTP_HOST")?;

    let SMTP_PORT = dotenv::var("SMTP_PORT")?;

    let SMTP_TSL = dotenv::var("SMTP_TSL")?;

    let SMTP_USERNAME = dotenv::var("SMTP_USERNAME")?;

    let SMTP_PASSWORD = dotenv::var("SMTP_PASSWORD")?;

    let SMTP_FROM = dotenv::var("SMTP_FROM")?;

    let SMTP_USERNAME = dotenv::var("SMTP_USERNAME")?;

  


    // 创建邮件

    let mut message = Message::builder()

        .from(format!("Alice <{}>", SMTP_FROM).parse().unwrap())

        .reply_to("Bob <bob@demo.com>".parse().unwrap())

        .to("Carla <car@tto.com>".parse().unwrap())

        .subject("Hello")

        .body("Hi there, it's a test email, with utf-8 chars ë!\n\n\n".to_owned())

        .unwrap();

  


    // 配置SMTP服务器

    let creds = Credentials::new(

        SMTP_USERNAME.to_string(), // 你的QQ邮箱

        SMTP_PASSWORD.to_string(), // 你的QQ邮箱密码或授权码

    );

  


    // 创建SMTP传输

    let mailer = SmtpTransport::relay("smtp.qq.com")

        .unwrap()

        .credentials(creds)

        .build();

  


    mailer

        .send(&message)

        .map(|s| {

            s.message().into_iter().for_each(|d| {

                d.println();

            });

        })

        .map_err(|e| e.dprintln());

    Ok(())

}