Enviando E-mails SMTP com Python
This commit is contained in:
@@ -2,3 +2,6 @@ BD_USER="CHANGE-ME"
|
|||||||
BD_PASSWORD="CHANGE-ME"
|
BD_PASSWORD="CHANGE-ME"
|
||||||
BD_PORT=CHANGE-ME
|
BD_PORT=CHANGE-ME
|
||||||
BD_HOST="CHANGE-ME"
|
BD_HOST="CHANGE-ME"
|
||||||
|
|
||||||
|
FROM_EMAIL="CHANGE-ME"
|
||||||
|
EMAIL_PASSWORD="CHANGE-ME"
|
||||||
24
aula185.html
Normal file
24
aula185.html
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8" />
|
||||||
|
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||||
|
<title>Arquivo para o e-mail</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
Olá ${nome},
|
||||||
|
<br />
|
||||||
|
Estou testando
|
||||||
|
<span style="color: red; font-weight: bold;">este e-mail</span>
|
||||||
|
em HTML.
|
||||||
|
<br />
|
||||||
|
<br />
|
||||||
|
|
||||||
|
<em>
|
||||||
|
Atenciosamente,
|
||||||
|
<br />
|
||||||
|
Luiz Otávio.
|
||||||
|
</em>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
47
aula185.py
Normal file
47
aula185.py
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
# Enviando E-mails SMTP com Python
|
||||||
|
import os
|
||||||
|
import pathlib
|
||||||
|
import smtplib
|
||||||
|
from email.mime.multipart import MIMEMultipart
|
||||||
|
from email.mime.text import MIMEText
|
||||||
|
from string import Template
|
||||||
|
|
||||||
|
from dotenv import load_dotenv # type: ignore
|
||||||
|
|
||||||
|
load_dotenv()
|
||||||
|
|
||||||
|
# Caminho arquivo HTML
|
||||||
|
CAMINHO_HTML = pathlib.Path(__file__).parent / 'aula185.html'
|
||||||
|
|
||||||
|
# Dados do remetente e destinatário
|
||||||
|
remetente = os.getenv('FROM_EMAIL', '')
|
||||||
|
destinatario = remetente
|
||||||
|
|
||||||
|
# Configurações SMTP
|
||||||
|
smtp_server = 'smtp.gmail.com'
|
||||||
|
smtp_port = 587
|
||||||
|
smtp_username = os.getenv('FROM_EMAIL', '')
|
||||||
|
smtp_password = os.getenv('EMAIL_PASSWORD', '')
|
||||||
|
|
||||||
|
# Mensagem de texto
|
||||||
|
with open(CAMINHO_HTML, 'r') as arquivo:
|
||||||
|
texto_arquivo = arquivo.read()
|
||||||
|
template = Template(texto_arquivo)
|
||||||
|
texto_email = template.substitute(nome='Helena')
|
||||||
|
|
||||||
|
# Transformar nossa mensagem em MIMEMultipart
|
||||||
|
mime_multipart = MIMEMultipart()
|
||||||
|
mime_multipart['from'] = remetente
|
||||||
|
mime_multipart['to'] = destinatario
|
||||||
|
mime_multipart['subject'] = 'Este é o assunto do e-mail'
|
||||||
|
|
||||||
|
corpo_email = MIMEText(texto_email, 'html', 'utf-8')
|
||||||
|
mime_multipart.attach(corpo_email)
|
||||||
|
|
||||||
|
# Envia o e-mail
|
||||||
|
with smtplib.SMTP(smtp_server, smtp_port) as server:
|
||||||
|
server.ehlo()
|
||||||
|
server.starttls()
|
||||||
|
server.login(smtp_username, smtp_password)
|
||||||
|
server.send_message(mime_multipart)
|
||||||
|
print('E-mail enviado com sucesso!')
|
||||||
Reference in New Issue
Block a user