eduardokum / laravel-mail-auto-embed
Library for embed images in emails automatically
Fund package maintenance!
eduardokum
Installs: 1 402 249
Dependents: 5
Suggesters: 1
Security: 0
Stars: 166
Watchers: 3
Forks: 36
Open Issues: 2
Requires
- php: ^7.2|^8.0
- ext-curl: *
- ext-dom: *
- ext-fileinfo: *
- illuminate/contracts: ^6.0|^7.0|^8.0|^9.0|^10.0|^11.0
- illuminate/mail: ^6.0|^7.0|^8.0|^9.0|^10.0|^11.0
- illuminate/support: ^6.0|^7.0|^8.0|^9.0|^10.0|^11.0
- masterminds/html5: ^2.7
Requires (Dev)
- orchestra/testbench: ^4.0|^5.0|^6.0|^7.0|^8.0|^9.0
- phpunit/phpunit: ^8.5.30|^9.0|^10.0|^11.0
- squizlabs/php_codesniffer: ^3.5
README
Laravel Mail Auto Embed
Automatically parses your messages and embeds the images found into your mail, replacing the original online-version of the image.
Should work on Laravel 5.3+. Automatically tested for Laravel 5.4+ on PHP 7.0+.
Version Compatibility
Install
You can install the package via composer:
composer require eduardokum/laravel-mail-auto-embed
This package uses Laravel 5.5 Package Auto-Discovery. For previous versions of Laravel, you need to add the following Service Provider:
$providers = [ ... \Eduardokum\LaravelMailAutoEmbed\ServiceProvider::class, ... ];
Usage
Its use is very simple, you write your markdown normally:
<!-- eg: resources/vendor/mail/markdown/order-shipped.blade.php --> @component('mail::message') # Order Shipped Your order has been shipped! @component('mail::button', ['url' => $url]) View Order @endcomponent Purchased product: ![product](https://domain.com/products/product-1.png) Thanks,<br> {{ config('app.name') }} @endcomponent
When sending, it will replace the link that would normally be generated:
<img src="https://domain.com/products/product-1.png">
by an embedded inline attachment of the image:
<img src="cid:3991f143cf1a86257f8671883736613c@Swift.generated">
.
It works for raw html too:
<!-- eg: resources/vendor/mail/html/header.blade.php --> <tr> <td class="header"> <a href="{{ $url }}"> <img src="https://domain.com/logo.png" class="img-header"> </a> </td> </tr>
If you do not want to use automatic embedding for specific images (because they
are hosted elsewhere, if you want to use some kind of image tracker, etc.),
simply add the attribute data-skip-embed
in the image tag:
<img src="https://domain.com/logo.png" data-skip-embed class="img-header">
Local resources
For local resources that are not available publicly, use file://
urls:
<img src="file://{{ resource_path('assets/img/logo.png') }}" alt="Logo" border="0"/>
Configuration
The defaults are set in config/mail-auto-embed.php
. You can copy this file to
your own config directory to modify the values using this command:
php artisan vendor:publish --provider="Eduardokum\LaravelMailAutoEmbed\ServiceProvider"
Explicit embedding configuration
By default, images are embedded automatically, unless you add the
data-skip-embed
attribute.
You can also disable auto-embedding globally by setting the MAIL_AUTO_EMBED
environment variable to false
, or by modifying the enabled
property in the
published config. You can then enable embedding for individual images with the
data-auto-embed
attribute.
# .env MAIL_AUTO_EMBED=false
return [ /* |-------------------------------------------------------------------------- | Mail auto embed |-------------------------------------------------------------------------- | | If true, images will be automatically embedded. | If false, only images with the 'data-auto-embed' attribute will be embedded | */ 'enabled' => false, // … ];
<p> <!-- Won't be embedded --> <img src="https://domain.com/logo.png" class="img-header"> </p> <p> <!-- Explicit embedding --> <img src="https://domain.com/item.png" data-auto-embed> </p>
Base64 embedding
If you prefer to use Base64 instead of inline attachments, you can do so by
setting the MAIL_AUTO_EMBED_METHOD
environment variable or the method
config property to base64
.
return [ // … /* |-------------------------------------------------------------------------- | Mail embed method |-------------------------------------------------------------------------- | | Supported: "attachment", "base64" | */ 'method' => 'base64', ];
Note that it will increase the e-mail size, and that it won't be decoded by some e-mail clients such as Gmail.
Mixed embedding methods
If you want to use both inline attachment and Base64 depending on the image,
you can specify the embedding method as the data-auto-embed
attribute value:
<p> <img src="https://domain.com/logo.png" data-auto-embed="base64"> </p> <p> <img src="https://domain.com/item.png" data-auto-embed="attachment"> </p>
Embedding entities
You might want to embed images that don't actually exist in your filesystem (stored in the database).
In that case, make the entities you want to embed implement the
EmbeddableEntity
interface:
namespace App\Models; use Eduardokum\LaravelMailAutoEmbed\Models\EmbeddableEntity; use Illuminate\Database\Eloquent\Model; class Picture extends Model implements EmbeddableEntity { /** * @param mixed $id * @return Picture */ public static function findEmbeddable($id) { return static::find($id); } /** * @return mixed */ public function getRawContent() { return $this->data; } /** * @return string */ public function getFileName() { return 'profile_'.$this->id.'.png'; } /** * @return string */ public function getMimeType() { return 'image/png'; } }
Then, you can use the embed:ClassName:id
syntax in your e-mail template:
<p> <img src="embed:App\Models\Picture:123"> </p>
Contributing
Please feel free to submit pull requests if you can improve or add any features.
We are currently using PSR-2. This is easy to implement and check with the PHP Coding Standards Fixer.