Last active
December 5, 2022 15:24
-
-
Save gyng/5d60225d55928ab4cf55309c88b25ecf to your computer and use it in GitHub Desktop.
Sending email via GMail SMTP using lettre in Rust
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[dependencies] | |
lettre = "0.5" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extern crate lettre; | |
use lettre::email::EmailBuilder; | |
use lettre::transport::smtp::{SecurityLevel, SmtpTransportBuilder}; | |
fn main() { | |
// You might need to enable https://www.google.com/settings/security/lesssecureapps | |
// You also might want to enable IMAP and store a copy of all outgoing emails | |
// Reference: https://www.digitalocean.com/community/tutorials/how-to-use-google-s-smtp-server | |
let to_address = "hello@example.com"; | |
let smtp_server = "smtp.googlemail.com"; | |
let smtp_username = "exampleaccount@gmail"; | |
let smtp_password = "hunter2"; | |
let smtp_port = 587u16; | |
let email = EmailBuilder::new() | |
.to(to_address) | |
.from(smtp_username) | |
.subject("I am contacting you in respect of a family treasure of Gold deposited in my name") | |
.body("i am Becki Ofori a Ghanian from Ashanti region Kumasi, Ghana.") | |
.build().unwrap(); | |
let mut mailer = SmtpTransportBuilder::new((smtp_server, smtp_port)).unwrap() | |
.hello_name("localhost") | |
.credentials(smtp_username, smtp_password) | |
.security_level(SecurityLevel::AlwaysEncrypt) | |
.smtp_utf8(true) | |
.build(); | |
let result = mailer.send(email.clone()); | |
match result { | |
Ok(_) => println!("email sent"), | |
Err(err) => println!("failed to send email alert: {}", err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Just set a app password and everything should work fine