@@ -107,17 +107,33 @@ def validate_recaptcha(recaptcha_response):
107
107
"""
108
108
Validates the ReCaptcha response.
109
109
"""
110
- if not recaptcha .verify (response = recaptcha_response ):
111
- raise ValueError ('Error: ReCaptcha verification failed!' )
110
+ try :
111
+ if not recaptcha .verify (response = recaptcha_response ):
112
+ logging .error ('ReCaptcha verification failed for response: %s' , recaptcha_response )
113
+ raise ValueError ('Error: ReCaptcha verification failed!' )
114
+ else :
115
+ logging .info ('ReCaptcha verification succeeded' )
116
+ except Exception as e :
117
+ logging .error ('ReCaptcha validation encountered an error: %s' , str (e ))
118
+ raise
112
119
113
120
def send_email (message ):
114
121
"""
115
- Sends the email using SendGrid.
122
+ Sends the email using SendGrid and logs detailed information for debugging .
116
123
"""
117
- sg = SendGridAPIClient (SENDGRIDAPIKEY )
118
- response = sg .send (message )
119
- if response .status_code not in [200 , 201 , 202 ]:
120
- raise ValueError (f"Error: Failed to send email. Status code: { response .status_code } " )
124
+ try :
125
+ sg = SendGridAPIClient (SENDGRIDAPIKEY )
126
+ response = sg .send (message )
127
+ logging .info ('SendGrid response status code: %s' , response .status_code )
128
+ if response .status_code not in [200 , 201 , 202 ]:
129
+ logging .error ('SendGrid failed with status code: %s, response body: %s' , response .status_code , response .body )
130
+ raise ValueError (f"Error: Failed to send email. Status code: { response .status_code } , body: { response .body } " )
131
+ else :
132
+ logging .info ('Email sent successfully. Status code: %s, response body: %s' , response .status_code , response .body )
133
+ except Exception as e :
134
+ logging .error ('Error sending email via SendGrid: %s' , str (e ))
135
+ raise
136
+
121
137
122
138
# Validate required environment variables
123
139
required_env_vars = ['RECAPTCHASITEKEY' , 'RECAPTCHASECRETKEY' , 'SENDGRIDAPIKEY' , 'SENDGRIDFROMEMAIL' ]
0 commit comments