I’m having trouble sending an Excel spreadsheet through Mailgun’s API using Python. The email goes through fine but the attachment doesn’t show up. Here’s what I’m trying to do:
send_email(..., files=["/home/user/report.xlsx"])
My email function looks like this:
def send_email(title, message, sender, recipient, files=None):
attachments = []
if files:
attachments.extend([("attachment", (os.path.basename(file_path), open(file_path, "rb+", encoding="utf-8"))) for file_path in files])
print(attachments)
response = requests.post(
"https://api.mailgun.net/v3/mydomain.com/messages",
auth=("api", "key-abc123def456ghi789jkl012mno345pqr"),
files=attachments,
data={
"from": sender,
"to": recipient,
"subject": title,
"text": message
}
)
return response
The request seems to work without throwing any errors but when I check my email the xlsx file is missing. What am I doing wrong here?