adriiita commited on
Commit
0098a2b
·
verified ·
1 Parent(s): 4c2392d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -8
app.py CHANGED
@@ -9,6 +9,20 @@ import json
9
  from pathlib import Path
10
  from typing import Dict, List, Optional
11
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  class UserProfile:
13
  def __init__(self, storage_path: str = "profiles.xlsx"):
14
  self.storage_path = storage_path
@@ -58,7 +72,8 @@ class EmailTemplate:
58
  class EmailGenie:
59
  def __init__(self):
60
  self.client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
61
- self.sendgrid_client = SendGridAPIClient(api_key=os.environ.get("SENDGRID_API_KEY"))
 
62
  self.user_profile = UserProfile()
63
  self.email_template = EmailTemplate()
64
 
@@ -92,19 +107,21 @@ class EmailGenie:
92
  def preview_email(self, email_content: str) -> str:
93
  return f"=== Email Preview ===\n\n{email_content}"
94
 
95
- def send_email(self, to_email: str, subject: str, content: str) -> bool:
 
 
 
96
  message = Mail(
97
- from_email=os.environ.get("SENDER_EMAIL"),
98
  to_emails=to_email,
99
  subject=subject,
100
  plain_text_content=content
101
  )
102
  try:
103
  self.sendgrid_client.send(message)
104
- return True
105
  except Exception as e:
106
- print(f"Error sending email: {e}")
107
- return False
108
 
109
  def create_interface():
110
  emailgenie = EmailGenie()
@@ -156,8 +173,8 @@ def create_interface():
156
  return emailgenie.preview_email(email_content)
157
 
158
  def send_email_handler(email, content):
159
- success = emailgenie.send_email(email, "Your Subject", content)
160
- return "Email sent successfully!" if success else "Error sending email"
161
 
162
  save_profile_btn.click(
163
  save_profile,
 
9
  from pathlib import Path
10
  from typing import Dict, List, Optional
11
 
12
+ def initialize_email_client():
13
+ sendgrid_key = os.environ.get("SENDGRID_API_KEY")
14
+ sender_email = os.environ.get("SENDER_EMAIL")
15
+
16
+ if not sendgrid_key or not sender_email:
17
+ print("Warning: SendGrid configuration missing. Email sending will be disabled.")
18
+ return None
19
+
20
+ try:
21
+ return SendGridAPIClient(api_key=sendgrid_key)
22
+ except Exception as e:
23
+ print(f"Error initializing SendGrid client: {e}")
24
+ return None
25
+
26
  class UserProfile:
27
  def __init__(self, storage_path: str = "profiles.xlsx"):
28
  self.storage_path = storage_path
 
72
  class EmailGenie:
73
  def __init__(self):
74
  self.client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
75
+ self.sendgrid_client = initialize_email_client()
76
+ self.sender_email = os.environ.get("SENDER_EMAIL")
77
  self.user_profile = UserProfile()
78
  self.email_template = EmailTemplate()
79
 
 
107
  def preview_email(self, email_content: str) -> str:
108
  return f"=== Email Preview ===\n\n{email_content}"
109
 
110
+ def send_email(self, to_email: str, subject: str, content: str) -> tuple[bool, str]:
111
+ if not self.sendgrid_client or not self.sender_email:
112
+ return False, "Email sending is not configured"
113
+
114
  message = Mail(
115
+ from_email=self.sender_email,
116
  to_emails=to_email,
117
  subject=subject,
118
  plain_text_content=content
119
  )
120
  try:
121
  self.sendgrid_client.send(message)
122
+ return True, "Email sent successfully"
123
  except Exception as e:
124
+ return False, f"Error sending email: {e}"
 
125
 
126
  def create_interface():
127
  emailgenie = EmailGenie()
 
173
  return emailgenie.preview_email(email_content)
174
 
175
  def send_email_handler(email, content):
176
+ success, message = emailgenie.send_email(email, "Your Subject", content)
177
+ return message
178
 
179
  save_profile_btn.click(
180
  save_profile,