When using Python 3.4 to send a .xlsx attachment via Mailgun, no error appears but the file isn’t delivered. How can this be fixed?
import os
import requests
def deliver_email(subject, body, sender, recipient, attach_files=None):
attachments = []
if attach_files:
for path in attach_files:
attachments.append(("file", (os.path.basename(path), open(path, "rb"), "application/octet-stream")))
print(attachments)
return requests.post(
"API_URL",
auth=("api", "YOUR_API_KEY"),
files=attachments,
data={
"from": sender,
"to": recipient,
"subject": subject,
"text": body
}
)
response = deliver_email("Test Subject", "Message Body", "[email protected]", "[email protected]", ["/tmp/sample.xlsx"])
print(response.status_code)