@@ -38,7 +38,7 @@ class Config:
3838
3939
4040app .config .from_object (Config )
41- Email_limit_API = app .config ["BREVO_API_KEY" ]
41+ Email_limit_API = app .config ["BREVO_API_KEY" ]
4242# Enable CORS securely
4343CORS (app , resources = {r"/api/*" : {"origins" : app .config ["CORS_ORIGINS" ]}})
4444
@@ -119,6 +119,16 @@ def init_db():
119119 status TEXT DEFAULT 'Rejected'
120120 )
121121 ''' )
122+ cur .execute (
123+ '''
124+ CREATE TABLE IF NOT EXISTS emails(
125+ id SERIAL PRIMARY KEY,
126+ email VARCHAR(255),
127+ content TEXT,
128+ created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
129+ )
130+ '''
131+ )
122132
123133 # Insert admin if not exists
124134 cur .execute ("SELECT * FROM admin WHERE user_id = 'Admin'" )
@@ -140,7 +150,6 @@ def init_db():
140150def error_response (message , status_code ):
141151 return jsonify ({"error" : message }), status_code
142152
143-
144153def send_email (to_email , subject , content ):
145154 sender = {"name" : "SRMU Club Notices" ,
146155 "email" : "srmu.clubnotices@gmail.com" }
@@ -157,7 +166,39 @@ def send_email(to_email, subject, content):
157166 except Exception as e :
158167 logging .error (f"Unexpected error sending email: { str (e )} " )
159168
169+ def loged_email ():
170+ conn = None
171+ cur = None
172+ if check_brevo_email_quota (Email_limit_API )== 0 :
173+ exit
174+ try :
175+ conn = get_db_connection ()
176+ cur = conn .cursor ()
177+ cur .execute ("SELECT email FROM emails" )
178+ users = cur .fetchall ()
179+ cur .execute ("SELECT content FROM emails" )
180+ contents = cur .fetchall ()
181+
182+ emails = [user ['email' ] for user in users ]
183+ contents = [content ['content' ] for content in contents ]
184+
185+ print (emails ,contents )
186+
187+ if len (emails ) > 200 :
188+ logging .warning (f"Number of emails to send ({ len (emails )} ) in Logs" )
189+
190+
191+ except Exception as e :
192+ logging .error (f"Error sending emails: { str (e )} " )
193+ finally :
194+ # Close the database connection and cursor
195+ if cur :
196+ cur .close ()
197+ if conn :
198+ conn .close ()
160199
200+
201+
161202def send_emails_to_all_users ():
162203 """Send emails to all users in the `users` table."""
163204 conn = None
@@ -173,7 +214,7 @@ def send_emails_to_all_users():
173214
174215 # Check if the number of emails exceeds 70
175216 if len (emails ) > 200 :
176- logging .warning (f"Number of emails to send ({ len (emails )} ) exceeds 70 . Proceeding anyway." )
217+ logging .warning (f"Number of emails to send ({ len (emails )} ) exceeds 200 . Proceeding anyway." )
177218
178219 # Send emails to all users
179220 subject = "Important Announcement"
@@ -182,11 +223,28 @@ def send_emails_to_all_users():
182223 <p>This is an important announcement from Your App.</p>
183224 <p>Thank you for using our service!</p>
184225 """
226+
227+ conn = get_db_connection ()
228+ cur = conn .cursor ()
229+
230+ # SQL query to insert email, content, and current datetime
231+ insert_query = """
232+ INSERT INTO emails (email, content, created_at)
233+ VALUES (%s, %s, %s);
234+ """
235+
236+ # Get the current date and time
237+ current_datetime = datetime .now ()
185238
186239 success_count = 0
240+ logged_email = 0
187241 for email in emails :
188- if send_email (email , subject , content ):
189- success_count += 1
242+ if check_brevo_email_quota (Email_limit_API )> 100 :
243+ send_email (to_email = email ,subject = subject ,content = content )
244+ success_count += 1
245+ else :
246+ cur .execute (insert_query , (email , content , current_datetime ))
247+ logged_email += 1
190248
191249 logging .info (f"Successfully sent { success_count } out of { len (emails )} emails." )
192250
@@ -200,32 +258,31 @@ def send_emails_to_all_users():
200258 if conn :
201259 conn .close ()
202260
261+
203262def send_approval_email (email , name , club , position ):
204263 """
205264 Send an email to the user notifying them of their approval.
206265 """
207- subject = "Your Application Has Been Approved"
208- content = f"""
209- <p>Dear { name } ,</p>
210- <p>We are pleased to inform you that your application has been approved!</p>
211- <p>Here are your details:</p>
212- <ul>
213- <li><strong>Club:</strong> { club } </li>
214- <li><strong>Position:</strong> { position } </li>
215- </ul>
216- <p>Thank you for joining us. We look forward to working with you!</p>
217- <p>Best regards,<br></p>
218- """
219- send_email (email , subject , content )
220-
221-
222266 try :
223- logging .info (f"Approval email sent to { email } " )
224-
225-
267+ subject = "Your Application Has Been Approved"
268+ content = f"""
269+ <p>Dear { name } ,</p>
270+ <p>We are pleased to inform you that your application has been approved!</p>
271+ <p>Here are your details:</p>
272+ <ul>
273+ <li><strong>Club:</strong> { club } </li>
274+ <li><strong>Position:</strong> { position } </li>
275+ </ul>
276+ <p>Thank you for joining us. We look forward to working with you!</p>
277+ <p>Best regards,<br></p>
278+ """
279+ if check_brevo_email_quota (Email_limit_API )> 50 :
280+ send_email (email , subject , content )
281+ logging .info (f"Approval email sent to { email } " )
282+
226283 except ApiException as e :
227284 logging .error (f"Failed to send approval email to { email } : { str (e )} " )
228-
285+
229286
230287# Schemas for Input Validation
231288class LoginSchema (Schema ):
@@ -336,11 +393,11 @@ def forgot_password():
336393 <p><a href="{ reset_link } ">Reset Password</a></p>
337394 <p>If you did not request this, please ignore this email.</p>
338395 """
339- if check_brevo_email_quota (Email_limit_API )!= 0 :
396+ if check_brevo_email_quota (Email_limit_API ) != 0 :
340397 print (check_brevo_email_quota (Email_limit_API ))
341398 send_email (email , "Password Reset" , content )
342399 else :
343- return jsonify ({"messeage" :"Email Was not sent,Due to Email Limit" })
400+ return jsonify ({"messeage" : "Email Was not sent,Due to Email Limit" })
344401
345402 return jsonify ({"message" : "Password reset email sent" }), 200
346403
@@ -398,7 +455,7 @@ def get_approvals(position, club_name):
398455
399456 # Fetch approvals based on hierarchy
400457 if position == 'admin' :
401-
458+
402459 cur .execute (
403460 "SELECT * FROM approval WHERE position = %s" ,
404461 (approval_hierarchy [position ],)
@@ -468,7 +525,8 @@ def approve_request(user_id):
468525
469526 except Exception as e :
470527 conn .rollback () # Rollback in case of error
471- logging .error (f"Error approving request: { str (e )} " ) # Log the real error
528+ logging .error (f"Error approving request: {
529+ str (e )} " ) # Log the real error
472530 return jsonify ({"error" : f"Failed to approve request: { str (e )} " }), 500
473531
474532 finally :
0 commit comments