|
| 1 | +import os |
| 2 | +import json |
| 3 | +import base64 |
| 4 | + |
| 5 | +from firebase_functions import https_fn |
| 6 | +from firebase_admin import initialize_app, app_check |
| 7 | +from google.oauth2.credentials import Credentials |
| 8 | +from googleapiclient.discovery import build |
| 9 | +from email.mime.text import MIMEText |
| 10 | + |
| 11 | +initialize_app() |
| 12 | + |
| 13 | +REQUIRED_SECRETS = [ |
| 14 | + "GMAIL_REFRESH_TOKEN", |
| 15 | + "GMAIL_CLIENT_ID", |
| 16 | + "GMAIL_CLIENT_SECRET", |
| 17 | + "SENDER_EMAIL_ADDRESS", |
| 18 | +] |
| 19 | + |
| 20 | +ALLOWED_ORIGIN = "https://md-led-visual.web.app" |
| 21 | + |
| 22 | +cors_headers = { |
| 23 | + "Access-Control-Allow-Origin": ALLOWED_ORIGIN, |
| 24 | + "Access-Control-Allow-Methods": "POST, OPTIONS", |
| 25 | + "Access-Control-Allow-Headers": "Content-Type, X-Firebase-AppCheck", |
| 26 | + "Content-Type": "application/json", |
| 27 | +} |
| 28 | + |
| 29 | + |
| 30 | +def get_gmail_service(): |
| 31 | + """Authenticate with Gmail API""" |
| 32 | + creds = Credentials( |
| 33 | + token=None, |
| 34 | + refresh_token=os.environ.get("GMAIL_REFRESH_TOKEN"), |
| 35 | + client_id=os.environ.get("GMAIL_CLIENT_ID"), |
| 36 | + client_secret=os.environ.get("GMAIL_CLIENT_SECRET"), |
| 37 | + token_uri="https://oauth2.googleapis.com/token", |
| 38 | + ) |
| 39 | + return build("gmail", "v1", credentials=creds) |
| 40 | + |
| 41 | + |
| 42 | +def create_email_message(receiver_email: str, subject: str, message_text: str): |
| 43 | + """Create and encode an email message""" |
| 44 | + message = MIMEText(message_text) |
| 45 | + message["to"] = receiver_email |
| 46 | + message["from"] = os.environ.get("SENDER_EMAIL_ADDRESS") |
| 47 | + message["subject"] = subject |
| 48 | + return base64.urlsafe_b64encode(message.as_bytes()).decode("utf-8") |
| 49 | + |
| 50 | + |
| 51 | +@https_fn.on_request(secrets=REQUIRED_SECRETS, enforce_app_check=True) |
| 52 | +def send_email(req: https_fn.Request) -> https_fn.Response: |
| 53 | + """Handles email sending via Gmail API""" |
| 54 | + |
| 55 | + # Handle Preflight (OPTIONS) Requests |
| 56 | + if req.method == "OPTIONS": |
| 57 | + return https_fn.Response(status=204, headers=cors_headers) |
| 58 | + |
| 59 | + # Restrict API access based on Origin |
| 60 | + origin = req.headers.get("Origin") |
| 61 | + if origin != ALLOWED_ORIGIN: |
| 62 | + return https_fn.Response( |
| 63 | + json.dumps({"success": False, "error": "Forbidden - Invalid Origin"}), |
| 64 | + status=403, |
| 65 | + headers=cors_headers, |
| 66 | + ) |
| 67 | + |
| 68 | + # Get app check token |
| 69 | + app_check_token = req.headers.get("X-Firebase-AppCheck") |
| 70 | + if not app_check_token: |
| 71 | + return https_fn.Response( |
| 72 | + json.dumps( |
| 73 | + {"success": False, "error": "Unauthorized - No App Check Token"} |
| 74 | + ), |
| 75 | + status=401, |
| 76 | + headers=cors_headers, |
| 77 | + ) |
| 78 | + |
| 79 | + # Verify app check token |
| 80 | + try: |
| 81 | + app_check.verify_token(app_check_token) # Throws error if invalid |
| 82 | + except Exception: |
| 83 | + return https_fn.Response( |
| 84 | + json.dumps( |
| 85 | + {"success": False, "error": "Unauthorized - Invalid App Check Token"} |
| 86 | + ), |
| 87 | + status=403, |
| 88 | + headers=cors_headers, |
| 89 | + ) |
| 90 | + |
| 91 | + try: |
| 92 | + # Parse request data |
| 93 | + data = req.get_json() |
| 94 | + receiver_email = data.get("email") |
| 95 | + subject = data.get("subject") |
| 96 | + message_text = data.get("message") |
| 97 | + |
| 98 | + if not receiver_email or not subject or not message_text: |
| 99 | + return https_fn.Response( |
| 100 | + json.dumps({"success": False, "error": "Missing required fields"}), |
| 101 | + status=400, |
| 102 | + headers=cors_headers, |
| 103 | + ) |
| 104 | + |
| 105 | + # Create and send the email |
| 106 | + raw_message = create_email_message(receiver_email, subject, message_text) |
| 107 | + service = get_gmail_service() |
| 108 | + send_request = ( |
| 109 | + service.users() |
| 110 | + .messages() |
| 111 | + .send(userId="me", body={"raw": raw_message}) |
| 112 | + .execute() |
| 113 | + ) |
| 114 | + |
| 115 | + return https_fn.Response( |
| 116 | + json.dumps( |
| 117 | + {"success": True, "message": "Email sent!", "id": send_request["id"]} |
| 118 | + ), |
| 119 | + status=200, |
| 120 | + headers=cors_headers, |
| 121 | + ) |
| 122 | + |
| 123 | + except Exception as e: |
| 124 | + return https_fn.Response( |
| 125 | + json.dumps({"success": False, "error": str(e)}), |
| 126 | + status=500, |
| 127 | + headers=cors_headers, |
| 128 | + ) |
0 commit comments