How to include file attachments when sending emails through Mailgun API using Delphi

I’m having trouble with my Delphi application. I can send emails through Mailgun API but the attachments don’t show up when the email reaches the recipient. The email itself works fine but it’s missing the attached file. I’m using Indy components for the HTTP requests.

Here’s what I’m currently doing:

try
  HttpClient.Request.CharSet := 'utf-8';
  HttpClient.Request.ContentType := 'multipart/form-data';
  HttpClient.Request.BasicAuthentication := True;
  HttpClient.Request.Username := 'api';
  HttpClient.Request.Password := 'my-api-key';

  FormData := TIdMultiPartFormDataStream.Create;
  FormData.AddFormField('from', UTF8Encode('Test User<[email protected]>'), 'utf-8').ContentTransfer := '8bit';
  FormData.AddFormField('to','[email protected]');
  FormData.AddFormField('subject', UTF8Encode('Test: subject line'), 'utf-8').ContentTransfer := '8bit';
  FormData.AddFormField('text', UTF8Encode('This is my message content'), 'utf-8').ContentTransfer := '8bit';
  
  FormData.AddFile('Document.pdf', 'c:\Document.pdf', '');
  Response := HttpClient.Post('https://api.mailgun.net/v3/samples.mailgun.org/messages', FormData);
finally
  FormData.Free;
end;

What could be wrong with my attachment handling?

I had the same attachment issues with Mailgun and Delphi. Turned out it was character encoding and file handling problems. Your code looks right, but wrap the file path in validation first. Indy gets weird with file streams when there’s spaces or special characters in the path. What fixed it for me: make sure the file handle gets released before the HTTP request. Windows locks files sometimes and Mailgun gets an empty attachment. Copy your file to a temp location first, then attach from there. Also check if your API key can handle attachments - some Mailgun keys only do text messages depending on your account setup.

This looks like a multipart form data encoding issue. Don’t set the ContentType manually when using TIdMultiPartFormDataStream - Indy needs to generate the boundary parameter automatically. Remove that HttpClient.Request.ContentType = ‘multipart/form-data’ line and let Indy handle it.

Also check your file size. Mailgun caps attachments at 25MB per message. I’ve seen cases where larger files seem to upload fine but never show up in the actual email. Test with a smaller file first to rule this out.

I encountered the same issue with Mailgun attachments in Delphi. Typically, the problem arises from the MIME type parameter. Presently, you are passing an empty string for the content type, which could cause Mailgun to ignore the attachment or process it incorrectly.

Instead, you should modify your code to use:

FormData.AddFile('attachment', 'c:\Document.pdf', 'application/pdf');

This change includes adding the appropriate MIME type and modifying the field name from ‘Document.pdf’ to ‘attachment’. Mailgun expects the form field to be labeled as ‘attachment’ (or ‘attachment[0]’, ‘attachment[1]’ for multiple files) rather than the file name itself. The filename is handled automatically within the multipart headers.

Additionally, ensure that your file path is valid and accessible at runtime. I’ve found that this can fail if relative paths do not resolve correctly in the production environment.

double-check ur Mailgun domain in the endpoint URL. I had the same issue n was posting to the wrong subdomain. also, try explicitly adding the filename parameter in ur AddFile method - Mailgun sometimes needs that to handle attachments properly.

first, make sure ur file actually exists b4 adding it to the form data. i ran into the same issue and my file path was wrong. also, try setting the content-type header after u create the FormData, not before. indy sometimes gets confused with multipart boundaries if u set the header too early.

This topic was automatically closed 4 days after the last reply. New replies are no longer allowed.