I’m building an email forwarding system using the Mailgun PHP library. My users can send emails with inline images, but when I forward them through Mailgun, the images show up as broken links instead of displaying properly in the email body.
The issue is that I can’t figure out how to correctly handle inline attachments versus regular attachments. When I use the attachment parameter, images get added to the bottom of the email as downloadable files. When I use the inline parameter, I still get broken image links.
Here’s my webhook data structure:
$_POST = [
'attachment-count' => 2,
'content-id-map' => '{
"<[email protected]>": "attachment-1",
"<[email protected]>": "attachment-2"
}'
];
$_FILES = [
'attachment-1' => [
'name' => 'screenshot.jpg',
'type' => 'image/jpeg',
'tmp_name' => '/tmp/phpABC123',
'error' => 0,
'size' => 95000
],
'attachment-2' => [
'name' => 'chart.png',
'type' => 'image/png',
'tmp_name' => '/tmp/phpDEF456',
'error' => 0,
'size' => 45000
]
];
My processing code:
$attachments = [];
foreach($content_id_map as $content_id => $file_key) {
$uploaded_file = $_FILES[$file_key];
$attachment_data = [];
$attachment_data["filePath"] = $uploaded_file["tmp_name"];
$attachment_data["filename"] = $uploaded_file["name"];
$attachments[] = $attachment_data;
}
// Send email
$response = $this->mailgun->messages()->send($my_domain, [
'from' => $sender_email,
'to' => $recipient_email,
'subject' => $email_subject,
'html' => $html_content,
'h:Reply-To' => $sender_email,
'inline' => $attachments // or 'attachment' => $attachments
]);
The HTML contains image tags like:
<img src="cid:[email protected]" alt="screenshot" width="300" height="200">
How can I properly detect which images should be inline vs regular attachments, and make sure the inline ones display correctly in the email body?