I'm trying to send an email with an attachment using Mailgun API. I can easily achieve this using curl:
curl -V -s --user 'XXX' \
https://api.eu.mailgun.net/v3/XXX/messages \
-F from='Excited User <YOU@YOUR_DOMAIN_NAME>' \
-F to='XXXX' \
-F subject='Hello' \
-F text='Testing some Mailgun awesomness!' \
--form-string html='<html>HTML version of the body</html>' \
-F attachment=@files/example.csv
the same using python's requests library. But when I'm trying to do the same using httpx library:
build_request_kwargs: dict = {
"url": "/messages",
"method": "POST",
"data": {
"template": "template_name",
"from": from_,
"to": to,
"subject": subject,
"h:X-Mailgun-Variables": json.dumps(template_data),
},
"files": [("file", ("file", io.BytesIO(b"<file content>")))]
}
client = httpx.AsyncClient(base_url=base_url, auth=("api", api_key))
async with client:
request = client.build_request(**build_request_kwargs)
response = await client.send(request)
response.raise_for_status()
email is delivered but without any attachment. I was trying to set each file encoding to application/octet-stream
but it didn't work either. I'm not sure what's the difference between curl or requests multipart/form-data
request and httpx.