I’m encountering an issue where my file attachment works perfectly when testing locally, yet fails to attach on the production deployment. I suspect the difference in environment paths is causing the problem. Here is an updated sample of my email sending function:
import os
import requests
def dispatch_mail(mail_subject, sender_email, recipients_list, content_html, attach_file=None):
# Determine file path based on environment
current_env = os.getenv('APP_ENV', 'local')
if attach_file is not None:
if current_env == 'local':
file_path = os.path.abspath(attach_file)
else:
file_path = os.path.join('/var/app/', attach_file)
with open(file_path, 'rb') as file_data:
attachment = [('file', file_data.read())]
else:
attachment = None
response = requests.post(
'https://api.mailgun.net/v3/your_domain.com/messages',
auth=('api', 'YOUR_API_KEY'),
files=attachment,
data={
'from': f'Example App <no-reply@your_domain.com>',
'to': recipients_list,
'subject': mail_subject,
'html': content_html
}
)
return response
Any insights on why the attachment might not be transmitted on live production, despite working locally, would be greatly appreciated. Thank you for your help with this attachment issue.