Mailgun binary attachement

387 views Asked by At

Ok, I try to send a simple *.xlsx file with mailgun using Python 3.4.

send_message(..., attachement=["/tmp/demo.xlsx"])

The code in send_message is given here:

def send_message(subject, text, fromAdr, to, attachement=None):

    files = list()
    if attachement:
        files.extend([("attachement", (os.path.basename(f), open(f, "b+r", encoding="utf-8"))) for f in attachement])


    print(files)
    return requests.post(
        "https://api.mailgun.net/v2/lobnek.com/messages",
        auth=("api", "key-2flidf93tp-u9moz95r8alhbl-pih1g9"),
        files = files,
        data={"from": fromAdr,
              "to": to,
              "subject": subject,
              "text": text}
    )

I get no error messages but the attachment is not sent!

1

There are 1 answers

1
tripleee On BEST ANSWER

An xlsx file is a binary file, not a text file in UTF-8.

Your initial validation attempt would have failed, too, if you had actually tried to read (far enough into) the file; but your code merely opens and closes it.