Skip to content

Instantly share code, notes, and snippets.

@Kremilly
Created May 14, 2024 21:33
Show Gist options
  • Save Kremilly/1651c5fb8984474609f76e86242ad6d7 to your computer and use it in GitHub Desktop.
Save Kremilly/1651c5fb8984474609f76e86242ad6d7 to your computer and use it in GitHub Desktop.
use lettre::smtp::authentication::Credentials;
use lettre::{SmtpClient, Transport};
use lettre_email::EmailBuilder;
use std::fs::File;
use std::io::Read;
fn main() {
// Configuração do transporte SMTP (substitua pelos detalhes do seu servidor SMTP)
let smtp_username = "seu_email@gmail.com";
let smtp_password = "sua_senha";
let smtp_server = "smtp.gmail.com";
let smtp_port = 587; // ou sua_variavel_smtp_port
// Constrói o e-mail
let email = EmailBuilder::new()
.to("destinatario@example.com")
.from(smtp_username)
.subject("Assunto do email com anexo")
.text("Corpo do email com anexo")
.attachment_from_file("arquivo_anexo.txt", "arquivo_anexo.txt", "text/plain")
.build()
.unwrap();
// Lê o conteúdo do arquivo anexo
let mut file_content = Vec::new();
File::open("arquivo_anexo.txt")
.expect("Falha ao abrir o arquivo anexo")
.read_to_end(&mut file_content)
.expect("Falha ao ler o arquivo anexo");
// Configura o transporte SMTP e envia o e-mail
let creds = Credentials::new(smtp_username.to_string(), smtp_password.to_string());
let mailer = SmtpClient::new_simple(smtp_server)
.unwrap()
.credentials(creds)
.transport();
match mailer.connect((smtp_server, smtp_port)) {
Ok(mut mailer) => {
if let Err(e) = mailer.send(email.into()) {
println!("Falha ao enviar o e-mail: {:?}", e);
} else {
println!("E-mail enviado com sucesso!");
}
}
Err(e) => println!("Falha ao conectar ao servidor SMTP: {:?}", e),
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment