|
| 1 | +""" |
| 2 | +Gmail API Email Backend for Django |
| 3 | +
|
| 4 | +This backend sends emails using Google's Gmail API with a service account. |
| 5 | +It supports domain-wide delegation to send emails as any user in the domain. |
| 6 | +
|
| 7 | +Configuration in settings.py: |
| 8 | + EMAIL_BACKEND = 'django_forms_workflows.email_backends.GmailAPIBackend' |
| 9 | +
|
| 10 | + GMAIL_API = { |
| 11 | + 'service_account_json': '/path/to/service-account.json', |
| 12 | + # OR |
| 13 | + 'service_account_base64': 'base64-encoded-json-credentials', |
| 14 | +
|
| 15 | + 'delegated_user': 'noreply@yourdomain.com', # User to impersonate |
| 16 | + 'scopes': ['https://www.googleapis.com/auth/gmail.send'], |
| 17 | + } |
| 18 | +
|
| 19 | +Requirements: |
| 20 | + pip install google-auth google-api-python-client |
| 21 | +""" |
| 22 | + |
| 23 | +import base64 |
| 24 | +import json |
| 25 | +import logging |
| 26 | +from email.mime.base import MIMEBase |
| 27 | +from email.mime.multipart import MIMEMultipart |
| 28 | +from email.mime.text import MIMEText |
| 29 | + |
| 30 | +from django.conf import settings |
| 31 | +from django.core.mail.backends.base import BaseEmailBackend |
| 32 | + |
| 33 | +logger = logging.getLogger(__name__) |
| 34 | + |
| 35 | + |
| 36 | +def get_gmail_service(config): |
| 37 | + """ |
| 38 | + Create and return an authenticated Gmail API service. |
| 39 | +
|
| 40 | + Args: |
| 41 | + config: Dictionary with Gmail API configuration |
| 42 | +
|
| 43 | + Returns: |
| 44 | + Gmail API service object |
| 45 | + """ |
| 46 | + try: |
| 47 | + from google.oauth2 import service_account |
| 48 | + from googleapiclient.discovery import build |
| 49 | + except ImportError as err: |
| 50 | + raise ImportError( |
| 51 | + "Gmail API backend requires google-auth and google-api-python-client. " |
| 52 | + "Install with: pip install google-auth google-api-python-client" |
| 53 | + ) from err |
| 54 | + |
| 55 | + scopes = config.get("scopes", ["https://www.googleapis.com/auth/gmail.send"]) |
| 56 | + delegated_user = config.get("delegated_user") |
| 57 | + |
| 58 | + # Load credentials from JSON file or base64-encoded string |
| 59 | + if "service_account_json" in config: |
| 60 | + credentials = service_account.Credentials.from_service_account_file( |
| 61 | + config["service_account_json"], scopes=scopes |
| 62 | + ) |
| 63 | + elif "service_account_base64" in config: |
| 64 | + # Decode base64 credentials |
| 65 | + json_str = base64.b64decode(config["service_account_base64"]).decode("utf-8") |
| 66 | + service_info = json.loads(json_str) |
| 67 | + credentials = service_account.Credentials.from_service_account_info( |
| 68 | + service_info, scopes=scopes |
| 69 | + ) |
| 70 | + else: |
| 71 | + raise ValueError( |
| 72 | + "Gmail API config must include either 'service_account_json' " |
| 73 | + "or 'service_account_base64'" |
| 74 | + ) |
| 75 | + |
| 76 | + # Delegate to the specified user (required for sending as that user) |
| 77 | + if delegated_user: |
| 78 | + credentials = credentials.with_subject(delegated_user) |
| 79 | + |
| 80 | + # Build the Gmail service |
| 81 | + service = build("gmail", "v1", credentials=credentials, cache_discovery=False) |
| 82 | + return service |
| 83 | + |
| 84 | + |
| 85 | +class GmailAPIBackend(BaseEmailBackend): |
| 86 | + """ |
| 87 | + Django email backend that sends emails via Gmail API. |
| 88 | +
|
| 89 | + This is more reliable than SMTP for Google Workspace environments |
| 90 | + and supports domain-wide delegation with service accounts. |
| 91 | + """ |
| 92 | + |
| 93 | + def __init__(self, fail_silently=False, **kwargs): |
| 94 | + super().__init__(fail_silently=fail_silently, **kwargs) |
| 95 | + self.config = getattr(settings, "GMAIL_API", {}) |
| 96 | + self._service = None |
| 97 | + |
| 98 | + @property |
| 99 | + def service(self): |
| 100 | + """Lazy-load the Gmail service.""" |
| 101 | + if self._service is None: |
| 102 | + self._service = get_gmail_service(self.config) |
| 103 | + return self._service |
| 104 | + |
| 105 | + def send_messages(self, email_messages): |
| 106 | + """ |
| 107 | + Send one or more EmailMessage objects and return the number sent. |
| 108 | + """ |
| 109 | + if not email_messages: |
| 110 | + return 0 |
| 111 | + |
| 112 | + sent_count = 0 |
| 113 | + for message in email_messages: |
| 114 | + try: |
| 115 | + self._send(message) |
| 116 | + sent_count += 1 |
| 117 | + except Exception as e: |
| 118 | + logger.error(f"Failed to send email via Gmail API: {e}") |
| 119 | + if not self.fail_silently: |
| 120 | + raise |
| 121 | + |
| 122 | + return sent_count |
| 123 | + |
| 124 | + def _send(self, email_message): |
| 125 | + """Send a single EmailMessage via Gmail API.""" |
| 126 | + # Convert Django EmailMessage to MIME message |
| 127 | + mime_message = self._build_mime_message(email_message) |
| 128 | + |
| 129 | + # Encode the message |
| 130 | + raw_message = base64.urlsafe_b64encode(mime_message.as_bytes()).decode("utf-8") |
| 131 | + |
| 132 | + # Send via Gmail API |
| 133 | + self.service.users().messages().send( |
| 134 | + userId="me", body={"raw": raw_message} |
| 135 | + ).execute() |
| 136 | + |
| 137 | + logger.info( |
| 138 | + f"Email sent via Gmail API to {', '.join(email_message.to)}: " |
| 139 | + f"{email_message.subject}" |
| 140 | + ) |
| 141 | + |
| 142 | + def _build_mime_message(self, email_message): |
| 143 | + """ |
| 144 | + Convert a Django EmailMessage to a MIME message. |
| 145 | +
|
| 146 | + Handles plain text, HTML, and multipart messages with attachments. |
| 147 | + """ |
| 148 | + # Check if this is an HTML email or has alternatives |
| 149 | + has_html = hasattr(email_message, "alternatives") and email_message.alternatives |
| 150 | + has_attachments = email_message.attachments |
| 151 | + |
| 152 | + if has_html or has_attachments: |
| 153 | + # Multipart message |
| 154 | + if has_attachments: |
| 155 | + msg = MIMEMultipart("mixed") |
| 156 | + msg_body = MIMEMultipart("alternative") |
| 157 | + else: |
| 158 | + msg = MIMEMultipart("alternative") |
| 159 | + msg_body = msg |
| 160 | + |
| 161 | + # Add plain text body |
| 162 | + msg_body.attach(MIMEText(email_message.body, "plain", "utf-8")) |
| 163 | + |
| 164 | + # Add HTML alternatives |
| 165 | + if has_html: |
| 166 | + for content, mimetype in email_message.alternatives: |
| 167 | + if mimetype == "text/html": |
| 168 | + msg_body.attach(MIMEText(content, "html", "utf-8")) |
| 169 | + |
| 170 | + # If we have attachments, add the body part and attachments |
| 171 | + if has_attachments: |
| 172 | + msg.attach(msg_body) |
| 173 | + for attachment in email_message.attachments: |
| 174 | + self._add_attachment(msg, attachment) |
| 175 | + else: |
| 176 | + # Simple plain text message |
| 177 | + msg = MIMEText(email_message.body, "plain", "utf-8") |
| 178 | + |
| 179 | + # Set headers |
| 180 | + msg["Subject"] = email_message.subject |
| 181 | + msg["From"] = email_message.from_email |
| 182 | + msg["To"] = ", ".join(email_message.to) |
| 183 | + |
| 184 | + if email_message.cc: |
| 185 | + msg["Cc"] = ", ".join(email_message.cc) |
| 186 | + if email_message.bcc: |
| 187 | + msg["Bcc"] = ", ".join(email_message.bcc) |
| 188 | + if email_message.reply_to: |
| 189 | + msg["Reply-To"] = ", ".join(email_message.reply_to) |
| 190 | + |
| 191 | + return msg |
| 192 | + |
| 193 | + def _add_attachment(self, msg, attachment): |
| 194 | + """Add an attachment to the MIME message.""" |
| 195 | + if isinstance(attachment, tuple): |
| 196 | + filename, content, mimetype = attachment |
| 197 | + else: |
| 198 | + # MIMEBase attachment |
| 199 | + msg.attach(attachment) |
| 200 | + return |
| 201 | + |
| 202 | + if mimetype is None: |
| 203 | + mimetype = "application/octet-stream" |
| 204 | + |
| 205 | + maintype, subtype = mimetype.split("/", 1) |
| 206 | + |
| 207 | + if maintype == "text": |
| 208 | + part = MIMEText(content, _subtype=subtype) |
| 209 | + else: |
| 210 | + part = MIMEBase(maintype, subtype) |
| 211 | + if isinstance(content, str): |
| 212 | + content = content.encode("utf-8") |
| 213 | + part.set_payload(content) |
| 214 | + from email import encoders |
| 215 | + |
| 216 | + encoders.encode_base64(part) |
| 217 | + |
| 218 | + part.add_header("Content-Disposition", "attachment", filename=filename) |
| 219 | + msg.attach(part) |
0 commit comments