Fake51
Fake51

Reputation: 525

Embedding image with swiftmailer makes gmail show html as attachment

I am trying to create an html email with Swiftmailer and to embed an image in the html bit. If I just send the mail with plain text and with html versions, the html version shows up just fine, with a broken image icon (of course). If I embed the image, though, I only see the plain text version and both html version and image show up as attachments.

Does anything look wrong or stick out with this approach?

Email headers:

Message-ID: <[email protected]>
Date: Sun, 15 Feb 2015 16:04:55 +0100
Subject: Subject here
From: Fastaval <[email protected]>
To: [email protected]
MIME-Version: 1.0
Content-Type: multipart/related;
 boundary="_=_swift_v4_1424012695_eef1c60fde0ddd3f8a9ad82190b115f8_=_"

Plain text header:

--_=_swift_v4_1424012695_eef1c60fde0ddd3f8a9ad82190b115f8_=_
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: quoted-printable

Html header:

--_=_swift_v4_1424012695_eef1c60fde0ddd3f8a9ad82190b115f8_=_
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: quoted-printable

Embedded image header:

--_=_swift_v4_1424012695_eef1c60fde0ddd3f8a9ad82190b115f8_=_
Content-Type: image/jpeg; name=Banner15.jpg
Content-Transfer-Encoding: base64
Content-Disposition: inline; filename=Banner15.jpg
Content-ID: <[email protected]>

The html bit with the image:

<div><img alt=3D"banner" src=3D"cid:8e75618e41588e7e5=
[email protected]"/></div>

I'm generating the email like so:

$this->_message = Swift_Message::newInstance()
    ->setFrom($from)
    ->setTo($to)
    ->setSubject($subject)
    ->setBody($message, 'text/plain');

$html = '<div><img alt="banner" src="banner-src"/></div>';
$html = str_replace('banner-src', $this->_message->embed(Swift_Image::fromPath('Banner15.jpg')), $html);

$this->_message->addPart($html, 'text/html');

Upvotes: 0

Views: 2166

Answers (2)

C.Ramp
C.Ramp

Reputation: 268

The message is old but if it can help.

You can do an attachement to your message, give it a filename. Extract of the documentation (swiftmailer doc):

$message->attach(Swift_Attachment::fromPath('/path/to/image.jpg')->setFilename('cool.jpg'));

You call the filename in the src of your img in the html.

Hope it will help someone.

Upvotes: 0

Zoon
Zoon

Reputation: 1088

I had the same problem and ended up doing this:

Leave out the text/plain body, and the text/html part shows up just fine.

Upvotes: 1

Related Questions