Skip to content
This repository has been archived by the owner on Jul 16, 2021. It is now read-only.

Embedding an image twice in one email #411

Closed
KKSzymanowski opened this issue Feb 10, 2017 · 8 comments
Closed

Embedding an image twice in one email #411

KKSzymanowski opened this issue Feb 10, 2017 · 8 comments

Comments

@KKSzymanowski
Copy link

Is there any built-in functionality for embedding the same image several times in one email?

If I do

<img src="{{ $message->embed($path) }}" />
<img src="{{ $message->embed($path) }}" />

recipient receives a message with two identical attachments.

One way I can get it to attach it once is to save the CID of an attachment and use it twice:

<?php $cid = $message->embed($path); ?>
<img src="{{ $cid }}" />
<img src="{{ $cid }}" />

Do you know any other options?
Maybe the Illuminate\Mail\Message::embed should check if the same file path occurred before in the message and if so, return the previous CID instead of generating a new one.
It would look something like this:

/**
 * CIDs of files embedded in the message.
 *
 * @var array
 */
protected $embeddedFiles = [];

/**
 * Embed a file in the message and get the CID.
 *
 * @param  string  $file
 * @return string
 */
public function embed($file)
{
    if(isset($this->embeddedFiles[$file])) {
        return $this->embeddedFiles[$file];
    }

    $cid = $this->swift->embed(Swift_Image::fromPath($file));

    $this->embeddedFiles[$file] = $cid;

    return $cid;
}

It will be a breaking change only in an edge case when someone changes the file contents between two calls to embed, so it's probably nothing to worry about.

@tomschlick
Copy link

Probably a good thing to add...

I'm just curious what the use case is though? Why would you want to embed the exact same image twice?

@garygreen
Copy link

Why would you want to embed the exact same image twice?

Maybe the site logo? One on the header and one on the footer? Dunno. Seems like a good thing to support.

@KKSzymanowski
Copy link
Author

KKSzymanowski commented Feb 10, 2017

@tomschlick
I can tell you my use case. My client wants a big logo at the top of the email as well as a small one in the footer.
The logo weighs about 40kB and with several thousand emails sent daily(most to which customers respond) it makes quite a difference in server space to reduce the email size from ~80kB to ~40kB.
Of course I could compress the small footer logo to ~10kB but 40kB is still better than 50kB.

@tomschlick
Copy link

Ah ok, I wasn't thinking about things like that. I was thinking more along the lines of photo attachments.

👍 from me. You're probably good to submit a PR on this. I don't see how it could be a bad thing to do this.

@KKSzymanowski
Copy link
Author

@taylorotwell Can I have your opinion about this before submitting a PR?

@taylorotwell
Copy link
Member

Sounds ok to me.

@garygreen
Copy link

All merged, can be closed now? 😄

@KKSzymanowski
Copy link
Author

Sure.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants