Like Arnoud, I can't test directly with the API you're using, but some local experimentation suggests this might work:
In[2]:= HTTPRequest[
"https://api.mailgun.net/v3/mydomain.com/messages", <|
Method -> "POST", "User" -> "api:MY_API_KY",
"Body" -> {"from" -> "test@mydomain.com", "to" -> "me@me.com",
"subject" -> "With Attachment",
"text" -> <|
"Content" ->
StringToByteArray["Diacritics test: àáâãäåçèéêëìíî"],
"MIMEType" -> "text/plain;charset=utf-8"|>,
"attachment" -> File[FindFile["ExampleData/mathematica.pdf"]]}|>,
CharacterEncoding -> "UTF8"] // URLRead[#, "Body"] &
When character encodings are an issue I generally find it better to manually encode the relevant portions of a request with StringToByteArray. The documentation for HTTPRequest suggests a ByteArray should work, but unfortunately it doesn't:
In[2]:= HTTPRequest[
"https://api.mailgun.net/v3/mydomain.com/messages", <|
Method -> "POST", "User" -> "api:MY_API_KY",
"Body" -> {"from" -> "test@mydomain.com", "to" -> "me@me.com",
"subject" -> "With Attachment",
"text" -> <|
"Content" ->
StringToByteArray["Diacritics test: àáâãäåçèéêëìíî"],
"MIMEType" -> "text/plain;charset=utf-8"|>,
"attachment" -> File[FindFile["ExampleData/mathematica.pdf"]]}|>,
CharacterEncoding -> "UTF8"] // URLRead[#, "Body"] &
During evaluation of In[2]:= General::erropts: The value{{from,Automatic} ->test@mydomain.com, {to,Automatic} ->me@me.com, {subject,Automatic} -> With Attachment, {text,text/plain;charset=utf-8,58b41feb-7b98-458f-afc8-8fd836c4461e} ->ByteArray[45 bytes], {attachment,Automatic}->File[/Applications/Mathematica 12.2.app/Contents/Documentation/English/System/ExampleData/mathematica.pdf]}specified for the option MultipartElements is invalid.
Out[2]= Failure["ConnectionFailure",
Association[
"URL" -> "https://api.mailgun.net/v3/mydomain.com/messages",
"MessageTemplate" -> "Unable to perform the request.",
"MessageParameters" -> Association[],
"HTTPRequest" -> HTTPRequest[
"https://webhook.site/efe31524-3df5-4d3f-8d80-b5be37df83b0",
Association[
Method -> "POST", "User" -> "api:MY_API_KY",
"Body" -> {"from" -> "test@mydomain.com", "to" -> "me@me"subject" -> "With Attachment",
"text" -> Association[
"Content" -> ByteArray[{68, 105, 97, 99, 114, 105, 116, 105,
99, 115, 32, 116, 101, 115, 116, 58, 32, 195, 160, 195,
161, 195, 162, 195, 163, 195, 164, 195, 165, 195, 167, 195,
168, 195, 169, 195, 170, 195, 171, 195, 172, 195, 173,
195, 174}], "MIMEType" -> "text/plain;charset=utf-8"],
"attachment" -> File["/Applications/Mathematica12.2.app/Contents/Documentation/English/System/ExampleData/mathematica.pdf"]}], CharacterEncoding -> "UTF8"], "Counter" -> 1]]