File size: 11,951 Bytes
d712cef | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 | import json
import base64
import os
import re
import sqlite3
from datetime import datetime, timedelta
from email.message import EmailMessage
from google.oauth2.credentials import Credentials
from google.auth.transport.requests import Request
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
import random
import string
from dotenv import load_dotenv
load_dotenv(dotenv_path=os.path.join(os.environ.get('WORKSPACE_ROOT', '.'), 'backend/.env'))
# ==========================================
# APP CREDENTIALS (From your server.ts)
# ==========================================
CLIENT_ID = os.getenv('GOOGLE_CLIENT_ID')
CLIENT_SECRET = os.getenv('GOOGLE_CLIENT_SECRET')
def send_email_from_database(db_row_data, database_file='database.json', existing_unique_id=None):
"""
Constructs and sends an email using credentials dynamically loaded
from database.json, then saves the record to a follow-ups database.
"""
try:
# 1. Load User Credentials from database.json
if not os.path.exists(database_file):
print(f"Error: Could not find {database_file}")
return
with open(database_file, 'r') as f:
user_db_data = json.load(f)
access_token = user_db_data.get('access_token')
refresh_token = user_db_data.get('refresh_token')
sender_email = user_db_data.get('email')
# 2. Parse the target Email JSON from the database row
body_json_str = db_row_data.get('body_json', '{}')
data = json.loads(body_json_str)
# Extract necessary nested data
body_data = data.get('body', {})
generated_content = body_data.get('generated_content', '')
# Support both 'outreach_data' (new) and 'excel_data' (old)
outreach_data = body_data.get('outreach_data', body_data.get('excel_data', {}))
# Prioritize the flat database column (sanitized by UI) over the JSON blob
db_email = db_row_data.get('company_email')
recipient_email = db_email if db_email and db_email != 'not updated' else outreach_data.get('company_email')
if not recipient_email or not isinstance(recipient_email, str):
print(f"❌ Error: Invalid recipient email type: {type(recipient_email)} content: {recipient_email}")
return None
# Robust cleaning: remove anything that isn't a valid email character
recipient_email = "".join(c for c in recipient_email if c.isprintable()).strip().strip(',')
if '@' not in recipient_email:
print(f"❌ Error: Malformed email address: '{recipient_email}'")
return None
company_name = outreach_data.get('company_name', db_row_data.get('company_name', 'Unknown Company'))
# 3. Extract the Subject and HTML Body
subject = body_data.get('subject', db_row_data.get('generated_subject', "Partnership Inquiry"))
html_body = generated_content
# If the generated content has a Subject: header, parse it
match = re.match(r"(?i)Subject:\s*(.*?)(?:<br\s*/?>|\n)+(.*)", generated_content, re.DOTALL)
if match:
subject = match.group(1).strip()
html_body = match.group(2).strip()
# Final sanitization of headers to prevent "Invalid Header" errors
subject = subject.replace('\n', ' ').replace('\r', ' ').strip()
recipient_email = recipient_email.strip()
# 4. Authenticate
creds = Credentials(
token=access_token,
refresh_token=refresh_token,
token_uri="https://oauth2.googleapis.com/token",
client_id=CLIENT_ID, # <-- Added
client_secret=CLIENT_SECRET, # <-- Added
scopes=["https://www.googleapis.com/auth/gmail.send"]
)
# Force a token refresh if it has expired
if creds and creds.expired and creds.refresh_token:
print("Access token expired. Refreshing token automatically...")
creds.refresh(Request())
# Optional: You could write the newly refreshed access_token back to your database.json here
# so the next run is faster, but it's not strictly necessary since the library handles it in memory!
# 5. Construct the Email Message
message = EmailMessage()
message["To"] = recipient_email
message["From"] = sender_email.strip() if sender_email else ""
message["Subject"] = subject
# --- Robust HTML Formatting ---
if '<p>' not in html_body and '<br' not in html_body:
# Handle Markdown-style bolding **text** -> <b>text</b>
html_body = re.sub(r"\*\*(.*?)\*\*", r"<b>\1</b>", html_body)
# Handle Markdown-style bullet points - text -> • text
lines = html_body.split('\n')
for i, line in enumerate(lines):
s_line = line.strip()
if s_line.startswith('- '):
lines[i] = '• ' + s_line[2:]
html_body = '\n'.join(lines)
# Split by double newline for paragraphs
paragraphs = [p.strip() for p in html_body.split('\n\n') if p.strip()]
if len(paragraphs) > 1:
html_body = "".join("<p style='margin-bottom:1.2em;'>" + p.replace('\n', '<br />') + "</p>" for p in paragraphs)
else:
html_body = html_body.replace('\n', '<br />')
elif '\n' in html_body and not html_body.startswith('<'):
# Fallback for mixed content
html_body = html_body.replace('\n', '<br />')
# Final HTML wrapper for professional look
html_wrapper = f"""
<div style="font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; line-height: 1.6; color: #1f2937; max-width: 600px;">
{html_body}
</div>
"""
message.set_content(html_wrapper, subtype='html')
# 6. Optional: Attach documents (omitted for standard B2B outreach unless specified)
# If we had a brochure_path, we would attach it here.
# 7. Send the Email via Gmail API
service = build('gmail', 'v1', credentials=creds)
raw_message = base64.urlsafe_b64encode(message.as_bytes()).decode('utf-8')
sent_message = service.users().messages().send(
userId="me",
body={'raw': raw_message}
).execute()
print(f"Email sent successfully to {recipient_email}. Message ID: {sent_message['id']}")
# 8. Save to Follow-Ups Database
save_to_followups_db(recipient_email, company_name, subject, body_json_str, sent_message['id'], existing_unique_id)
return sent_message
except HttpError as error:
print(f"Gmail API Error: {error}")
except Exception as e:
print(f"An unexpected error occurred: {e}")
def save_to_followups_db(company_email, company_name, subject, body_json_str, message_id, existing_unique_id=None):
"""
Saves or updates the sent email details into the master outreach journey database.
If existing_unique_id is provided, it updates the record. Otherwise, it creates a new one.
"""
base_dir = os.path.join(os.environ.get('WORKSPACE_ROOT', '.'), 'Database/FollowUps')
os.makedirs(base_dir, exist_ok=True)
db_path = os.path.join(base_dir, 'sent_emails.db')
now = datetime.now()
followup_time = now + timedelta(days=7)
# Use existing ID or generate a new random 20-digit ID
unique_id = existing_unique_id if existing_unique_id else ''.join(random.choices(string.digits, k=20))
try:
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
cursor.execute('''
CREATE TABLE IF NOT EXISTS sent_applications (
id INTEGER PRIMARY KEY AUTOINCREMENT,
company_email TEXT,
company_name TEXT,
generated_subject TEXT,
date_sent DATETIME,
followup_date DATETIME,
status TEXT,
Unique_application_id TEXT,
message_id TEXT,
body_json TEXT
)
''')
# Check if record exists
cursor.execute("SELECT id FROM sent_applications WHERE Unique_application_id = ?", (unique_id,))
exists = cursor.fetchone()
if exists:
# Update existing record (Follow-up case)
cursor.execute('''
UPDATE sent_applications
SET date_sent = ?, followup_date = ?, status = ?, message_id = ?, body_json = ?, generated_subject = ?
WHERE Unique_application_id = ?
''', (
now.strftime("%Y-%m-%d %H:%M:%S"),
followup_time.strftime("%Y-%m-%d %H:%M:%S"),
"Follow-up Sent - Awaiting Reply",
message_id,
body_json_str,
subject,
unique_id
))
print(f"Follow-up logged. Journey updated for ID: {unique_id}")
else:
# Create new record (Cold email case)
cursor.execute('''
INSERT INTO sent_applications
(company_email, company_name, generated_subject, date_sent, followup_date, status, Unique_application_id, message_id, body_json)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
''', (
company_email,
company_name,
subject,
now.strftime("%Y-%m-%d %H:%M:%S"),
followup_time.strftime("%Y-%m-%d %H:%M:%S"),
"Sent - Awaiting Follow-up",
unique_id,
message_id,
body_json_str
))
print(f"New application logged. ID: {unique_id}")
conn.commit()
except sqlite3.Error as e:
print(f"Database error occurred: {e}")
finally:
if conn:
conn.close()
# ==========================================
# Example Usage
# ==========================================
if __name__ == "__main__":
sample_db_row = {
"body_json": """{
"body": {
"generated_content": "Subject: Excited About Stripe's Mission and Eager to Contribute<br><br>Hi Stripe Team,<br><br>I hope this email finds you well. I’ve been following Stripe’s journey and am genuinely impressed by how you’ve built the financial infrastructure for the internet. Your mission to simplify global payments and empower businesses of all sizes resonates deeply with me. I’d love to be part of a team that’s making such a significant impact on the internet economy.<br><br>As a final-year AI student at GIKI, I’ve developed a strong foundation in building scalable systems and solving complex problems. My work often involves critical thinking and thorough research, which aligns well with Stripe’s focus on reliability and developer-friendly infrastructure. I’m particularly drawn to your vision of enabling businesses to innovate faster and reach customers worldwide, and I believe my skills and passion could contribute meaningfully to this goal.<br><br>I’ve shared some of my projects and contributions on my GitHub (https://github.com) and LinkedIn (https://linkedin.com). I’d love to connect and discuss how I can bring value to Stripe’s mission. Looking forward to the possibility of collaborating with such an inspiring team.<br><br>Best regards,<br>Asad Irfan",
"excel_data": {
"company_email": "u2022120@gmail.com",
"role": "Software Engineer"
}
}
}"""
}
# Execute the function
path_ = os.path.join(os.getcwd(), 'backend/database.json')
# send_email_from_database(sample_db_row, path_) |