Update routes/emails.py
Browse files- routes/emails.py +9 -17
routes/emails.py
CHANGED
@@ -3,7 +3,7 @@ import aiohttp
|
|
3 |
from fastapi import APIRouter, HTTPException, Query
|
4 |
from google.auth.transport.requests import Request
|
5 |
from google.oauth2.credentials import Credentials
|
6 |
-
from email.
|
7 |
from email.utils import formataddr
|
8 |
|
9 |
router = APIRouter()
|
@@ -31,22 +31,14 @@ def get_access_token():
|
|
31 |
async def send_email_gmail(to_email: str, subject: str, body_html: str):
|
32 |
access_token = get_access_token()
|
33 |
|
34 |
-
#
|
35 |
-
|
|
|
|
|
|
|
36 |
|
37 |
-
#
|
38 |
-
|
39 |
-
|
40 |
-
message = f"""From: {from_header}
|
41 |
-
To: {to_email}
|
42 |
-
Subject: {encoded_subject}
|
43 |
-
MIME-Version: 1.0
|
44 |
-
Content-Type: text/html; charset="UTF-8"
|
45 |
-
|
46 |
-
{body_html}
|
47 |
-
"""
|
48 |
-
|
49 |
-
encoded_message = base64.urlsafe_b64encode(message.encode("utf-8")).decode("utf-8")
|
50 |
|
51 |
url = "https://gmail.googleapis.com/gmail/v1/users/me/messages/send"
|
52 |
headers = {
|
@@ -54,7 +46,7 @@ Content-Type: text/html; charset="UTF-8"
|
|
54 |
"Content-Type": "application/json"
|
55 |
}
|
56 |
payload = {
|
57 |
-
"raw":
|
58 |
}
|
59 |
|
60 |
async with aiohttp.ClientSession() as session:
|
|
|
3 |
from fastapi import APIRouter, HTTPException, Query
|
4 |
from google.auth.transport.requests import Request
|
5 |
from google.oauth2.credentials import Credentials
|
6 |
+
from email.mime.text import MIMEText
|
7 |
from email.utils import formataddr
|
8 |
|
9 |
router = APIRouter()
|
|
|
31 |
async def send_email_gmail(to_email: str, subject: str, body_html: str):
|
32 |
access_token = get_access_token()
|
33 |
|
34 |
+
# Cria o objeto MIMEText com charset utf-8 para o corpo HTML
|
35 |
+
msg = MIMEText(body_html, 'html', 'utf-8')
|
36 |
+
msg['To'] = to_email
|
37 |
+
msg['From'] = formataddr((SENDER_NAME, EMAIL_ADDRESS))
|
38 |
+
msg['Subject'] = subject
|
39 |
|
40 |
+
# Codifica a mensagem em base64 urlsafe para a API Gmail
|
41 |
+
raw_message = base64.urlsafe_b64encode(msg.as_bytes()).decode()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
|
43 |
url = "https://gmail.googleapis.com/gmail/v1/users/me/messages/send"
|
44 |
headers = {
|
|
|
46 |
"Content-Type": "application/json"
|
47 |
}
|
48 |
payload = {
|
49 |
+
"raw": raw_message
|
50 |
}
|
51 |
|
52 |
async with aiohttp.ClientSession() as session:
|