add inline image to a message sent with swiftmailer
None of the answers really worked for me. I had to include inline images, using CID. What I had to do, to make it work:
$attachment = Swift_Image::newInstance($data, $filename, $mimeType)
->setDisposition('inline');
$cid = $message->embed($attachment); // Generates "cid:something"
Important part is using Swift_Image class. Then the image in html should be:
<img src="cid:something" ... />
I think this solution works without doing something hacky with swiftmailer (ver. 5.4.2). It is exactly how the documentation says.
Remember to test multiple email clients (gmail, thunderbird, apple mail, web clients...) if the inline images work. For example using Swift_Attachment, inline images showed in gmail, but not in some web clients. Using Swift_Image, it worked everywhere.
The accepted answer doesn't work (version tested: 5.4.2). (Mine works but could be perfected)
Instead, looking inside the "Original" (Gmail: Show Original) I've found that swiftmailer is omitting adding 2 headers to the attachment, namely:
Content-ID: <ABC123>
X-Attachment-Id: ABC123
ABC123 is the cid we have to put in the body where we want the inline to be showed:
So thanks to this question: I found the way to fix it for swiftmailer (that is even against swiftmailer documentation, but it works while theirs do not)
this is the final (ugly) code:
$attachment = Swift_Attachment::fromPath('image.jpg')->setDisposition('inline');
$attachment->getHeaders()->addTextHeader('Content-ID', '<ABC123>');
$attachment->getHeaders()->addTextHeader('X-Attachment-Id', 'ABC123');
$cid = $message->embed($attachment);
$img = '<img src="cid:ABC123"/>';
$html = "
<html>
<head>
</head>
<body>
$img
</body>
</html>
";
$message->setBody($html, 'text/html');
I know this is an old question (with an accepted answer), but for anyone having the same problem, the Swift_Image::fromPath($image_path)
method expects the image path to be local (a filesystem path):
Swift_Image::fromPath("/local/path/to/image.jpg");
// Local path = great success
Thus the below will fail if your image path is external (starting with https://
, etc.):
Swift_Image::fromPath("https://external.path/to/image.jpg");
// External path = epic fail
For external images, you should use:
# Load the image file
$data = file_get_contents($image_path);
# Get the filename
$filename = explode("?",basename($image_path))[0];
# Get the mime type
$finfo = new \finfo(FILEINFO_MIME_TYPE);
$mime_type = $finfo->buffer($buffer);
# Load the image file
$image_file = new \Swift_Image($data, $filename, $mime_type);
# Set the disposition to inline
$image_file->setDisposition('inline');
After all the running around i found an alternate solution. Swiftmailer allows 2 methods in which to perform the same thing.
one is the embed() function
and the other one is the attach() function
so to the code above, i removed the "embed()" since it wasn't working for me and added these 2 lines below and it works
->attach(Swift_Attachment::fromPath('path to image here.jpg')
->setDisposition('inline'));
and it worked 100%