I’m working with Mailgun to send notification emails when user data exports are ready. The emails include download links pointing to files stored on AWS S3.
My current email template looks something like:
File processing complete! <a href="s3-bucket-url" download>Click to download</a>
The issue I’m facing is that when users click the link, some files open directly in the browser instead of downloading. I think this might be related to MIME type configuration, but I’m not sure how to properly set it up so the browser always treats it as a download rather than trying to display the content.
The download attribute doesn’t work reliably across browsers when you’re linking to external domains like S3. Instead, set the Content-Disposition header to “attachment” directly on the S3 object. If you’re using the AWS SDK, add ContentDisposition: ‘attachment; filename=“your-file-name.ext”’ to your upload parameters. You can also handle this through S3 bucket policies or CloudFront with custom headers. Don’t just change the MIME type - browsers will still try to display certain files inline unless you use the Content-Disposition header.
I’ve hit this exact issue before. The problem is browsers ignore the download attribute on cross-origin requests, which is what you get with S3 URLs. Easiest fix is to set the right metadata when uploading to S3. Add Content-Disposition: “attachment” metadata to your uploads (include the filename too if you want). This way S3 always serves the file with headers that force downloads, no matter how someone accesses it. With boto3, just add Metadata={‘Content-Disposition’: ‘attachment’} to your put_object call. Works across all browsers and you don’t need any server proxying or extra infrastructure.
you could also create a proxy endpoint on your server that grabs the S3 file and serves it with the right headers. sometimes it’s way easier than tweaking S3 configs - just cache the responses so you’re not hitting S3 constantly.