1818load_dotenv ()
1919
2020class Config :
21- MAX_CONTENT_LENGTH = 15 * 1024 * 1024 # 15 MB
21+ MAX_CONTENT_LENGTH = 40 * 1024 * 1024 # 40MB - this is the SES limit and is greated than the 20MB limit imposed in the dropzone/frontend to allow for PGP overhead
2222 EMAIL_DOMAIN = "@ethereum.org"
23- DEFAULT_RECIPIENT_EMAIL = " [email protected] " 23+ DEFAULT_RECIPIENT_EMAIL = os . getenv ( 'DEFAULT_RECIPIENT_EMAIL' , ' [email protected] ' ) 2424 NUMBER_OF_ATTACHMENTS = int (os .getenv ('NUMBEROFATTACHMENTS' , 10 ))
2525 SECRET_KEY = os .getenv ('SECRET_KEY' , 'you-should-set-a-secret-key' )
2626
@@ -134,39 +134,63 @@ def validate_turnstile(turnstile_response):
134134
135135def send_email (message ):
136136 """
137- Sends the email using AWS SES and logs detailed information for debugging.
137+ Sends the email using AWS SES V2 and logs detailed information for debugging.
138138 """
139139 try :
140- # Send the email
141- response = ses_client .send_raw_email (
142- Source = message ['From' ],
143- Destinations = [message ['To' ]],
144- RawMessage = {
145- 'Data' : message .as_string ()
140+ # Convert MIME message to bytes for SES V2
141+ raw_message_data = message .as_string ().encode ('utf-8' )
142+
143+ # Check message size before sending (AWS SES limit is 40MB)
144+ message_size_mb = len (raw_message_data ) / (1024 * 1024 )
145+ if message_size_mb > 40 :
146+ logging .error (f'Email message size ({ message_size_mb :.2f} MB) exceeds AWS SES limit of 40MB' )
147+ raise ValueError (f'Error: Email message is too large ({ message_size_mb :.2f} MB). AWS SES has a 40MB limit. Please reduce the size of attachments.' )
148+
149+ logging .info (f'Sending email with size: { message_size_mb :.2f} MB' )
150+
151+ # Send the email using SES V2
152+ response = ses_client .send_email (
153+ FromEmailAddress = message ['From' ],
154+ Destination = {
155+ 'ToAddresses' : [message ['To' ]]
156+ },
157+ Content = {
158+ 'Raw' : {
159+ 'Data' : raw_message_data
160+ }
146161 }
147162 )
148163
149164 # Log the response
150165 message_id = response ['MessageId' ]
151- logging .info ('AWS SES email sent successfully. MessageId: %s' , message_id )
166+ logging .info ('AWS SES V2 email sent successfully. MessageId: %s' , message_id )
152167
153168 except ClientError as e :
154169 error_code = e .response ['Error' ]['Code' ]
155170 error_message = e .response ['Error' ]['Message' ]
156- logging .error ('AWS SES error: Code=%s, Message=%s' , error_code , error_message )
171+ logging .error ('AWS SES V2 error: Code=%s, Message=%s' , error_code , error_message )
157172
158173 # Provide user-friendly error messages
159- if error_code == 'MessageRejected' :
174+ if error_code == '413' or error_code == 'RequestEntityTooLarge' :
175+ # Log the message size for debugging
176+ message_size_mb = len (raw_message_data ) / (1024 * 1024 )
177+ logging .error (f'Email message size: { message_size_mb :.2f} MB' )
178+ raise ValueError ('Error: Email message is too large. AWS SES has a 40MB limit for raw messages. Please reduce the size of attachments.' )
179+ elif error_code == 'MessageRejected' :
160180 raise ValueError ('Error: Email was rejected by AWS SES. Please check the email configuration.' )
161181 elif error_code == 'MailFromDomainNotVerified' :
162182 raise ValueError ('Error: The sender email domain is not verified in AWS SES.' )
163183 elif error_code == 'ConfigurationSetDoesNotExist' :
164184 raise ValueError ('Error: AWS SES configuration error.' )
185+ elif error_code == 'AccountSuspendedException' :
186+ raise ValueError ('Error: AWS SES account is suspended.' )
187+ elif error_code == 'SendingPausedException' :
188+ raise ValueError ('Error: AWS SES sending is paused for this account.' )
165189 else :
166190 raise ValueError (f'Error: Failed to send email. { error_message } ' )
167191
168192 except Exception as e :
169- logging .error ('Error sending email via AWS SES: %s' , str (e ))
193+ logging .error ('Error sending email via AWS SES V2 : %s' , str (e ))
170194 raise
171195
172196
@@ -366,9 +390,9 @@ def send_identifier_to_kissflow(grant_id, legal_identifier):
366390AWS_REGION = os .environ ['AWS_REGION' ]
367391FROMEMAIL = os .environ ['SES_FROM_EMAIL' ]
368392
369- # Initialize AWS SES client
393+ # Initialize AWS SES V2 client
370394ses_client = boto3 .client (
371- 'ses ' ,
395+ 'sesv2 ' ,
372396 region_name = AWS_REGION ,
373397 aws_access_key_id = AWS_ACCESS_KEY_ID ,
374398 aws_secret_access_key = AWS_SECRET_ACCESS_KEY
@@ -377,8 +401,6 @@ def send_identifier_to_kissflow(grant_id, legal_identifier):
377401app = Flask (__name__ )
378402app .config .from_object (Config )
379403
380-
381-
382404# Initialize rate limiting
383405limiter = Limiter (get_forwarded_address , app = app , default_limits = ["200 per day" , "50 per hour" ])
384406
@@ -388,6 +410,15 @@ def send_identifier_to_kissflow(grant_id, legal_identifier):
388410 logging .basicConfig (filename = log_file , level = logging .INFO )
389411else :
390412 logging .basicConfig (level = logging .INFO )
413+
414+ # DEBUG: Print Config values on startup
415+ logging .info ("=== DEBUG: Config Values on Startup ===" )
416+ logging .info (f"MAX_CONTENT_LENGTH: { Config .MAX_CONTENT_LENGTH } " )
417+ logging .info (f"EMAIL_DOMAIN: { Config .EMAIL_DOMAIN } " )
418+ logging .info (f"DEFAULT_RECIPIENT_EMAIL: { Config .DEFAULT_RECIPIENT_EMAIL } " )
419+ logging .info (f"NUMBER_OF_ATTACHMENTS: { Config .NUMBER_OF_ATTACHMENTS } " )
420+ logging .info (f"SECRET_KEY: { '[SET]' if Config .SECRET_KEY != 'you-should-set-a-secret-key' else '[USING DEFAULT - PLEASE SET!]' } " )
421+ logging .info ("=====================================" )
391422
392423@app .route ('/' , methods = ['GET' ])
393424def index ():
0 commit comments