I’m having trouble with PDF files getting corrupted when I send them via email using mailgun-js library in my Node.js application. The original PDF works perfectly on my system, but when it arrives in the recipient’s inbox, the file cannot be opened properly.
I’m using this approach to attach and send the document:
var document = fs.readFileSync(emailData.files[0].location);
var fileAttachment = new mailgun.Attachment({
data: document,
filename: emailData.files[0].title,
contentType: 'application/pdf'
});
var emailConfig = {
from: '[email protected]',
to: '[email protected]',
attachment: [fileAttachment],
subject: 'Document attached',
text: 'Please find the PDF document attached'
};
mailgun.messages().send(emailConfig, function(err, response) {
// handle response
});
What could be causing this corruption issue? Is there something wrong with how I’m reading or attaching the PDF file?
Had the same PDF corruption issue with Mailgun. The problem was how the buffer gets processed during transmission. Fixed it by explicitly setting the encoding when reading the file. Try adding the encoding parameter to your readFileSync, or better yet, switch to readFile with proper error handling. Binary data gets misinterpreted during the API call, causing the corruption. Also check your file size - Mailgun has attachment limits and larger PDFs sometimes get truncated instead of rejected, which corrupts files that partially load. I’d test with a small, simple PDF first to see if it’s a size or encoding issue.
First, double-check your file path. File corruption often happens when you’re sending before it’s fully loaded. Switch from readFileSync to fs.readFile with await - I’ve seen sync methods screw up binary data.
This sounds like a buffer handling issue during transmission. I’ve hit similar corruption problems with binary files through email APIs. Instead of reading the file directly as a buffer, convert it to base64 first, then create the attachment. Also make sure your Node.js version plays nice with whatever mailgun library version you’re running. Older versions of mailgun-js were notorious for mangling binary data. Try adding encoding specification when you read the file, or just upgrade to @mailgun/mailgun-js - it handles binary attachments way better.
hey, i had this same issue too. try encoding the pdf in base64. like use data: document.toString('base64')
instead of sending it as is. it helped me, hope it works for you!
Hit this exact PDF corruption issue last year building an automated doc delivery system. It’s not just encoding - it’s the whole mess of handling binary data through email APIs manually.
Everyone’s pushing base64 fixes, but you’re still stuck managing file handling, email sending, and error handling across multiple systems. Gets ugly when you scale.
I ditched the whole approach and went full automation. Instead of fighting mailgun-js buffer problems, I built a flow that handles PDF reading, encoding, email composition, and delivery monitoring in one shot. No more debugging corrupted attachments or guessing if files actually went through.
The automation does base64 conversion automatically, handles Mailgun API calls correctly, and includes retry logic for failed sends. Built-in logging tracks exactly what happened to each PDF.
This killed all my PDF corruption problems and made email delivery bulletproof. Way better than patching code every time some new edge case shows up.
Check out https://latenode.com for setting this up properly.