2017 © Pedro Peláez
 

library laravel-mail-auto-embed

Library for embed images in emails automatically

image

eduardokum/laravel-mail-auto-embed

Library for embed images in emails automatically

  • Thursday, July 12, 2018
  • by eduardokum
  • Repository
  • 2 Watchers
  • 40 Stars
  • 13,339 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 11 Forks
  • 2 Open issues
  • 6 Versions
  • 44 % Grown

The README.md

Packagist Packagist Packagist GitHub Actions GitHub forks, (*1)

Laravel Mail Auto Embed

Automatically parses your messages and embeds the images found into your mail, replacing the original online-version of the image., (*2)

Should work on Laravel 5.3+. Automatically tested for Laravel 5.4+ on PHP 7.0+., (*3)

Version Compatibility

Laravel Package
< 8.x 1.x
> 9.x 2.x

Install

You can install the package via composer:, (*4)

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:, (*5)

$providers = [
    ...
    \Eduardokum\LaravelMailAutoEmbed\ServiceProvider::class,
    ...
 ];

Usage

Its use is very simple, you write your markdown normally:, (*6)


@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:, (*7)

<img src="https://domain.com/products/product-1.png">, (*8)

by an embedded inline attachment of the image:, (*9)

<img src="cid:3991f143cf1a86257f8671883736613c@Swift.generated">., (*10)

It works for raw html too:, (*11)

<!-- 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:, (*12)

<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:, (*13)

<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:, (*14)

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., (*15)

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., (*16)

# .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., (*17)

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., (*18)

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:, (*19)

<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)., (*20)

In that case, make the entities you want to embed implement the EmbeddableEntity interface:, (*21)

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:, (*22)

<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., (*23)

We are currently using PSR-2. This is easy to implement and check with the PHP Coding Standards Fixer., (*24)

Donate with Paypal, (*25)

The Versions

12/07 2018

dev-master

9999999-dev https://github.com/eduardokum/laravel-mail-auto-embed

Library for embed images in emails automatically

  Sources   Download

MIT

The Requires

 

The Development Requires

by Eduardo Gusmão

eduardokum laravel-mail-auto-embed

21/09 2017

1.0.4

1.0.4.0 https://github.com/eduardokum/laravel-mail-auto-embed

Library for embed images in emails automatically

  Sources   Download

MIT

The Requires

 

The Development Requires

by Eduardo Gusmão

eduardokum laravel-mail-auto-embed

30/08 2017

1.0.3

1.0.3.0 https://github.com/eduardokum/laravel-mail-auto-embed

Library for embed images in emails automatically

  Sources   Download

MIT

The Requires

 

by Eduardo Gusmão

eduardokum laravel-mail-auto-embed

23/06 2017

1.0.2

1.0.2.0 https://github.com/eduardokum/laravel-mail-auto-embed

Library for embed images in emails automatically

  Sources   Download

MIT

The Requires

 

by Eduardo Gusmão

eduardokum laravel-mail-auto-embed

18/03 2017

1.0.1

1.0.1.0 https://github.com/eduardokum/laravel-mail-auto-embed

Library for embed images in emails automatically

  Sources   Download

MIT

The Requires

 

by Eduardo Gusmão

eduardokum laravel-mail-auto-embed

17/03 2017

1.0.0

1.0.0.0 https://github.com/eduardokum/laravel-mail-auto-embed

Library for embed images in emails automatically

  Sources   Download

MIT

The Requires

 

by Eduardo Gusmão

eduardokum laravel-mail-auto-embed