Skip to content

Commit

Permalink
Replace yagmail with Gmail API for sending emails
Browse files Browse the repository at this point in the history
  • Loading branch information
malloc-nbytes committed Jan 31, 2025
1 parent 676b2b6 commit 7310e4a
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 16 deletions.
14 changes: 8 additions & 6 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*.log
*.sql
*.sqlite

# OS generated files #
######################
.DS_Store?
Expand All @@ -15,18 +15,18 @@ Thumbs.db
# IDE
*.idea
*.iml

# Generated files
/web-app/WEB-INF
build/
*.[oa]
*.pyc

# Other source repository archive directories (protects when importing)
.hg
.svn
CVS

# automatic backup files
*~.nib
*.swp
Expand All @@ -42,7 +42,9 @@ BackEndFlask/logs/all.log
BackEndFlask/models/hidden.py
BackEndFlask/oauth2_creds.json
BackEndFlask/logs/all.log
BackEndFlask/.env
BackEndFlask/*.env*
FrontEndReact/*.env*
FrontEndReact/.env.production
.vscode

# Nginx configuration files
Expand All @@ -54,4 +56,4 @@ BackEndFlask/.env

# Misc
BackEndFlask/core/.cron_setup_done
BackEndFlask/token.json
BackEndFlask/token.json
60 changes: 50 additions & 10 deletions BackEndFlask/models/utility.py
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,
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 7310e4a

Please sign in to comment.