I am trying to switch our application from python mail to Mailgun, but I am having problems with email messages that have attachments. In particular, PDFs that are generated by the application (not stored in the file system).
You have no problem sending emails without attachments.
We are currently creating a PDF as such:
pdf = StringIO() draw_pdf(pdf, params) pdf.seek(0) attachment = MIMEApplication(pdf.read()) attachment.add_header("Content-Disposition", "attachment", filename=filename) pdf.close()
And then attach and send it as such:
from django.core.mail import EmailMultiAlternatives msg = EmailMultiAlternatives(subject, text_content, from_email, to_email) if html_content: msg.attach_alternative(html_content, "text/html") if attachment: msg.attach(attachment) msg.send()
Works great ... how can we convert to a Mailgun call?
I tried various things, including just passing it as a file as it is (unsuccessfully):
requests.post(mailgun_url, auth=("api", mailgun_api), data=data, files=attachment)
The above works great without an application. data contains in, from, o: tags ... etc.
Any help would be greatly appreciated. Thanks!
EDIT
I managed to get it working by modifying my PDF code and getting the correct request.post structure:
filename = "pdf_attachment.pdf" pdf = StringIO() draw_pdf(pdf, params) pdf.seek(0) attachment = ("attachment", (filename, pdf.read())) r = requests.post(mailgun_url, auth=("api", mailgun_api), data=data, files=[attachment])