Skip to content

Instantly share code, notes, and snippets.

@gyng
Last active December 5, 2022 15:24
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save gyng/5d60225d55928ab4cf55309c88b25ecf to your computer and use it in GitHub Desktop.
Save gyng/5d60225d55928ab4cf55309c88b25ecf to your computer and use it in GitHub Desktop.
Sending email via GMail SMTP using lettre in Rust
[dependencies]
lettre = "0.5"
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)
}
}
@alexzanderr
Copy link

alexzanderr commented Jun 30, 2022

this is not going to work anymore; from 30 may 2022, google disabled less secure apps.

today on 30.06.2022, i tried to enable less secure apps from my account to try this gist and found this message: This setting is no longer available. [Learn more](https://support.google.com/accounts?p=less-secure-apps&hl=en&authuser=2)

google:

To help keep your account secure, from May 30, 2022, ​​Google no longer supports the use of third-party apps or devices which ask you to sign in to your Google Account using only your username and password.

my guess is to use Gmail API from google APIs and i also guess that you need to register your rust app in the google cloud console.

sorry boys, im disappointed too ...

@lamafab
Copy link

lamafab commented Jul 8, 2022

@alexzanderr For me it still works when using smtp-relay.gmail.com.

@c-git
Copy link

c-git commented Jul 17, 2022

Does this work if you use an app password?

@chikko80
Copy link

this is not going to work anymore; from 30 may 2022, google disabled less secure apps.

today on 30.06.2022, i tried to enable less secure apps from my account to try this gist and found this message: This setting is no longer available. [Learn more](https://support.google.com/accounts?p=less-secure-apps&hl=en&authuser=2)

google:

To help keep your account secure, from May 30, 2022, ​​Google no longer supports the use of third-party apps or devices which ask you to sign in to your Google Account using only your username and password.

my guess is to use Gmail API from google APIs and i also guess that you need to register your rust app in the google cloud console.

sorry boys, im disappointed too ...

Just set a app password and everything should work fine

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment