Embedding inline images in emails using Mailgun with Node.js

I’m trying to include inline images in my email using Mailgun’s Node.js library but running into some issues. The official docs show a curl example with -F inline=@files/image.png but I can’t figure out how to translate this to Node.js.

Here’s my current attempt:

MailgunService.sendEmail({
    key: 'my-api-key',
    domainName: 'mydomain.com',
    recipient: user.emailAddress,
    recipientName: user.fullName,
    emailSubject: 'Welcome to our platform',
    body: '<div><img src="cid:welcome-banner.jpg" width="500" height="80" style="display:block;" /></div>',
    inlineAttachment: 'assets/welcome-banner.jpg',
    senderEmail: '[email protected]',
    senderName: 'Support Team'
}).then(function(result) {
    console.log('Email sent successfully');
}).catch(function(error) {
    console.log('Failed to send email:', error);
});

The email sends fine but the image doesn’t show up. I think the issue is with how I’m referencing the inline file path. How do I properly attach an inline image so it displays in the email content? Any help would be appreciated.

Had this same issue a few months back. The problem’s usually that your CID reference doesn’t match the attachment filename exactly. When you write cid:welcome-banner.jpg in your HTML, Mailgun wants the inline attachment to have that exact filename as the CID. You’re probably passing just a file path string instead of using Mailgun’s proper attachment format. Try using fs.createReadStream() to read the file and set the filename explicitly: inline: [{ data: fs.createReadStream('assets/welcome-banner.jpg'), filename: 'welcome-banner.jpg' }]. Your HTML reference has to match perfectly - if the attachment’s named ‘welcome-banner.jpg’, your src needs to be ‘cid:welcome-banner.jpg’. Also check that your asset path is right relative to where your script’s running, since path resolution can mess things up.

your custom service wrapper isn’t handling inline attachments right. I use mailgun’s official sdk directly and format it like inline: [{data: fs.readFileSync('path/to/image.jpg'), filename: 'welcome-banner.jpg', contentType: 'image/jpeg'}]. ur wrapper probably expects a different format - check what the inlineAttachment property actually does in your MailgunService code.

You’re passing a string path instead of a proper attachment object. The Mailgun Node.js SDK wants inline attachments in the inline property as file objects. Try this: inline: [fs.createReadStream('assets/welcome-banner.jpg')] (don’t forget to import fs). Mailgun auto-assigns the CID based on filename, so cid:welcome-banner.jpg should work once you attach it properly. I ran into the same issue - the SDK handles CID mapping differently than those REST API examples show. Also check your file path. Relative paths can bite you depending on where you’re running the script.

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