-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace yagmail with Gmail API for sending emails
- Loading branch information
1 parent
676b2b6
commit 7310e4a
Showing
2 changed files
with
58 additions
and
16 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,26 @@ | ||
import traceback # Debugging, remove once done | ||
|
||
import os | ||
import sys | ||
import time | ||
import yagmail | ||
import string, secrets | ||
from models.logger import logger | ||
from controller.Routes.RouteExceptions import EmailFailureException | ||
|
||
OAUTH2_CREDS_FP = 'oauth2_creds.json' | ||
import base64 | ||
from email.message import EmailMessage | ||
|
||
# try: | ||
# from models.hidden import OAUTH2_CREDS_FP | ||
# except: | ||
# print("## need to add models/hidden.py and set PASSWORD before sending emails") | ||
import google.auth | ||
from googleapiclient.discovery import build | ||
from googleapiclient.errors import HttpError | ||
from google.auth.transport.requests import Request | ||
from google.oauth2.credentials import Credentials | ||
from google_auth_oauthlib.flow import InstalledAppFlow | ||
|
||
from models.logger import logger | ||
from controller.Routes.RouteExceptions import EmailFailureException | ||
|
||
OAUTH2_CREDS_FP = '/home/ubuntu/private/client_secret.json' | ||
OAUTH2_TOKEN_FP = '/home/ubuntu/private/token.json' | ||
|
||
def send_email_and_check_for_bounces(func, | ||
dest_addr, | ||
|
@@ -166,13 +175,44 @@ def email_students_feedback_is_ready_to_view(students: list, notification_messag | |
|
||
send_email(student.email, subject, message) | ||
|
||
def send_email(address: str, subject: str, content: str): | ||
def send_email(address: str, subject: str, content: str): | ||
try: | ||
yag = yagmail.SMTP("skillbuilder02", oauth2_file=OAUTH2_CREDS_FP) | ||
yag.send(to=address, subject=subject, contents=content) | ||
scopes = ["https://www.googleapis.com/auth/gmail.compose"] | ||
|
||
creds = None | ||
|
||
if os.path.exists(OAUTH2_TOKEN_FP): | ||
creds = Credentials.from_authorized_user_file(OAUTH2_TOKEN_FP, scopes) | ||
|
||
if creds and creds.expired and creds.refresh_token: | ||
creds.refresh(Request()) | ||
|
||
if not creds or not creds.valid: | ||
raise EmailFailureException("creds is invalid") | ||
|
||
service = build("gmail", "v1", credentials=creds) | ||
|
||
message = EmailMessage() | ||
message.set_content(content) | ||
message["To"] = address | ||
message["From"] = "[email protected]" | ||
message["Subject"] = subject | ||
|
||
encoded_message = base64.urlsafe_b64encode(message.as_bytes()).decode() | ||
create_message = { | ||
"raw": encoded_message, | ||
} | ||
|
||
send_message = service.users().messages().send(userId="me", body=create_message).execute() | ||
|
||
# except RecursionError: | ||
# s = traceback.format_exc() | ||
# raise EmailFailureException(f"STACK ERROR: {s}") | ||
|
||
except Exception as e: | ||
raise EmailFailureException(str(e)) | ||
|
||
|
||
def generate_random_password(length: int): | ||
letters = string.ascii_letters + string.digits | ||
|
||
|