adi-123 commited on
Commit
b1ae41c
·
verified ·
1 Parent(s): a814e5a

Update utils.py

Browse files
Files changed (1) hide show
  1. utils.py +45 -29
utils.py CHANGED
@@ -3,10 +3,10 @@ import os
3
  import streamlit as st
4
  import requests
5
  from transformers import pipeline
6
- from typing import Dict
7
  from together import Together
8
  from gtts import gTTS
9
- from gmail_smtplib_micro import GmailSMTPLib, Email
10
 
11
 
12
  # Image-to-text
@@ -76,47 +76,43 @@ def get_user_preferences() -> Dict[str, str]:
76
  return preferences
77
 
78
 
79
- def create_gmail_config():
80
- """
81
- Creates a Gmail configuration file if it doesn't exist
82
- """
83
- config_content = f"""[GMail]
84
- username = {os.getenv('SENDER_EMAIL', '')}
85
- password = {os.getenv('SENDER_PASSWORD', '')}
86
- """
87
- with open('pysnail.conf', 'w') as f:
88
- f.write(config_content)
89
-
90
  def send_story_email(recipient_email: str, story_text: str, audio_file_path: str) -> bool:
91
  """
92
- Send the story text and audio file to the specified email address
93
  Returns True if successful, False otherwise
94
  """
95
  try:
96
- # Create email content
 
 
 
 
 
 
 
97
  subject = "Your Generated Story"
98
- body = f"""Here's your generated story:
99
 
100
  {story_text}
101
 
102
  Enjoy!"""
103
 
104
- # Create email object
105
- email = Email(subject, body)
 
 
 
 
 
 
106
 
107
  # Add audio file attachment
108
- email.add_attachment(audio_file_path)
109
-
110
- # Create Gmail configuration if it doesn't exist
111
- if not os.path.exists('pysnail.conf'):
112
- create_gmail_config()
113
-
114
- # Initialize Gmail client and send email
115
- gmail = GmailSMTPLib('pysnail.conf')
116
- gmail.send_object(email, recipient_email)
117
 
 
 
118
  return True
119
-
120
  except Exception as e:
121
  print(f"Error sending email: {str(e)}")
122
  return False
@@ -126,4 +122,24 @@ def validate_email(email: str) -> bool:
126
  Basic email validation
127
  """
128
  pattern = r'^[\w\.-]+@[\w\.-]+\.\w+$'
129
- return re.match(pattern, email) is not None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
  import streamlit as st
4
  import requests
5
  from transformers import pipeline
6
+ from typing import Dict, Optional
7
  from together import Together
8
  from gtts import gTTS
9
+ from py_mailsender import Mailsender
10
 
11
 
12
  # Image-to-text
 
76
  return preferences
77
 
78
 
 
 
 
 
 
 
 
 
 
 
 
79
  def send_story_email(recipient_email: str, story_text: str, audio_file_path: str) -> bool:
80
  """
81
+ Send the story text and audio file to the specified email address using py-mailsender
82
  Returns True if successful, False otherwise
83
  """
84
  try:
85
+ # Get email credentials from environment variables
86
+ sender_email = os.getenv('SENDER_EMAIL')
87
+ sender_password = os.getenv('SENDER_PASSWORD')
88
+
89
+ if not sender_email or not sender_password:
90
+ raise ValueError("Email credentials not configured")
91
+
92
+ # Create email subject and content
93
  subject = "Your Generated Story"
94
+ content = f"""Here's your generated story:
95
 
96
  {story_text}
97
 
98
  Enjoy!"""
99
 
100
+ # Initialize mail sender
101
+ mailer = Mailsender(
102
+ sender_email,
103
+ sender_password,
104
+ recipient_email,
105
+ subject,
106
+ content
107
+ )
108
 
109
  # Add audio file attachment
110
+ mailer.add_attachment(audio_file_path)
 
 
 
 
 
 
 
 
111
 
112
+ # Send email
113
+ mailer.send()
114
  return True
115
+
116
  except Exception as e:
117
  print(f"Error sending email: {str(e)}")
118
  return False
 
122
  Basic email validation
123
  """
124
  pattern = r'^[\w\.-]+@[\w\.-]+\.\w+$'
125
+ return re.match(pattern, email) is not None
126
+
127
+ def validate_gmail_credentials(email: str, password: str) -> Optional[str]:
128
+ """
129
+ Validate Gmail credentials by attempting to create a test mailer
130
+ Returns None if successful, error message if failed
131
+ """
132
+ try:
133
+ test_mailer = Mailsender(
134
+ email,
135
+ password,
136
+ email, # Send to self for testing
137
+ "Test Connection",
138
+ "Testing credentials"
139
+ )
140
+ # Just initialize the connection without sending
141
+ test_mailer.initialize_connection()
142
+ test_mailer.close_connection()
143
+ return None
144
+ except Exception as e:
145
+ return str(e)