-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from lapig-ufg/email
- Loading branch information
Showing
11 changed files
with
381 additions
and
46 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import os | ||
import email, smtplib, ssl | ||
from email.mime.text import MIMEText | ||
from email.mime.multipart import MIMEMultipart | ||
|
||
|
||
from workers.models.email import SendEmail | ||
from workers.render import template | ||
from workers.utils.emial import html_to_text | ||
|
||
|
||
def send_email(payload: SendEmail): | ||
try: | ||
sender_email = os.environ.get('EMAIL_SENDER') | ||
password = os.environ.get('EMAIL_PASSWORD') | ||
receiver_email = payload['receiver_email'] | ||
|
||
|
||
message = MIMEMultipart("alternative") | ||
message["Subject"] = payload['subject'] | ||
message["From"] = sender_email | ||
message["To"] = receiver_email | ||
|
||
to_render = payload['message'] | ||
html = template.get_template(to_render['template']).render(to_render['content']) | ||
|
||
message.attach(MIMEText(html_to_text(html), "plain")) | ||
message.attach(MIMEText(html, "html")) | ||
|
||
# Create secure connection with server and send email | ||
context = ssl.create_default_context() | ||
with smtplib.SMTP_SSL(os.environ.get('SERVER_EMAIL'), os.environ.get("EMAIL_PORT",465), context=context) as server: | ||
|
||
server.login(sender_email, password) | ||
server.sendmail( | ||
sender_email, receiver_email, message.as_string() | ||
) | ||
except Exception as e: | ||
raise Exception(f"Error sending email: {e} {sender_email} {password} {receiver_email}") | ||
return {"status": "Email sent successfully"} | ||
|
||
|
||
if __name__ == '__main__': | ||
payload_send_mail ={ | ||
"receiver_email": "devjairomr@gmail.com", | ||
"subject": "CONCLUÍDA - Requisição de Análise de Geometria (Área)", | ||
"message":{ | ||
"template":"base.html", | ||
"content":{ | ||
"title": "CONCLUÍDA - Requisição de Análise de Geometria (Área)", | ||
"hello": f"Olá, Jairo Matos da Rocja", | ||
"body": "Conforme solicitação, a análise de dados da área submetida está completa. Você consegue acessar os resultados no link abaixo:", | ||
"url":"https://atlasdaspastagens.ufg.br/map/123-456-789-000", | ||
"regards":f"Atenciosamente", | ||
"team":"Equipe do Atlas das Pastagens do Brasil" | ||
} | ||
}} | ||
send_email(payload_send_mail) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
This file was deleted.
Oops, something went wrong.
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,21 @@ | ||
from pathlib import Path | ||
from jinja2 import Environment, FileSystemLoader | ||
|
||
from glob import glob | ||
# Diretório do script atual | ||
current_directory = Path(__file__).resolve().parent | ||
|
||
# Diretório dos templates | ||
templates_directory = current_directory / 'templates' | ||
|
||
# Configurando o Jinja2 para carregar templates do diretório | ||
template = Environment(loader=FileSystemLoader(str(templates_directory))) | ||
template = Environment(loader=FileSystemLoader(str(templates_directory))) | ||
|
||
if __name__ == '__main__': | ||
# Testando o template | ||
template_vars = { | ||
'title': 'Hello, World!', | ||
'body': 'This is a simple Jinja2 template.', | ||
'team': 'Team!' | ||
} | ||
template_content = template.get_template('base.html').render(template_vars) | ||
print(template_content) |
Oops, something went wrong.