rsvpify/laravel-mail-auto-embed
自动嵌入电子邮件中的图片库
Requires
- php: >=5.5.0
- illuminate/contracts: >=5.3
- illuminate/mail: >=5.3
- illuminate/support: >=5.3
Requires (Dev)
- orchestra/testbench: ~3.0
- phpunit/phpunit: ~5.0|~6.0
This package is auto-updated.
Last update: 2024-09-23 00:58:14 UTC
README
Laravel 邮件自动嵌入
安装
您可以通过 composer 安装此包
$ composer require rsvpify/laravel-mail-auto-embed
此包使用 Laravel 5.5 包自动发现
对于 Laravel 的早期版本,您需要添加以下服务提供者
$providers = [ ... \Rsvpify\LaravelMailAutoEmbed\ServiceProvider::class, ... ];
使用方法
其使用非常简单,您只需正常编写 markdown
@component('mail::message')
# Order Shipped
Your order has been shipped!
@component('mail::button', ['url' => $url])
View Order
@endcomponent
Purchased product:

Thanks,<br>
{{ config('app.name') }}
@endcomponent
在发送时,它将替换通常生成的链接
<img src="https://domain.com/products/product-1.png">
用图像的嵌入式附件替换
<img src="cid:3991f143cf1a86257f8671883736613c@Swift.generated">
.
它也适用于原始 html
// 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>
如果您不想为特定的图像使用自动嵌入(因为它们托管在其他地方,如果您想使用某种图像跟踪器等),只需在图像标签中添加属性 data-skip-embed
<img src="https://domain.com/logo.png" data-skip-embed class="img-header">
本地资源
对于不公开提供的本地资源,请使用 file://
url,例如
<img src="file://{{ resource_path('assets/img/logo.png') }}" alt="Logo" border="0"/>
配置
默认值在 config/mail-auto-embed.php
中设置。您可以将此文件复制到您的配置目录,并使用以下命令修改值
php artisan vendor:publish --provider="Rsvpify\LaravelMailAutoEmbed\ServiceProvider"
显式嵌入配置
默认情况下,图像会自动嵌入,除非您添加 data-skip-embed
属性。
您还可以通过设置环境变量 MAIL_AUTO_EMBED
为 false
来全局禁用自动嵌入。然后,您可以使用 data-auto-embed
属性为特定图像启用嵌入。
# .env
MAIL_AUTO_EMBED=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 嵌入
如果您更喜欢使用 Base64 而不是内联附件,可以通过将环境变量 MAIL_AUTO_EMBED_METHOD
设置为 base64
来实现。
请注意,它会增加电子邮件的大小,并且一些电子邮件客户端(如 Gmail)不会对其进行解码。
混合嵌入方法
如果您想根据图像使用内联附件和 Base64,您可以在 data-auto-embed
属性值中指定嵌入方法。
<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>
嵌入实体
您可能想要嵌入实际上不存在于您的文件系统中(存储在数据库中)的图像。
在这种情况下,请使您想要嵌入的实体实现 EmbeddableEntity
接口。
namespace App\Models; use Rsvpify\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'; } }
然后,您可以在电子邮件模板中使用 embed:ClassName:id
语法。
<p> <img src="embed:App\Models\Picture:123"> </p>
贡献
如果您能改进或添加任何功能,请随时提交拉取请求。
我们目前正在使用 PSR-2。这很容易实现并使用 PHP Coding Standards Fixer 进行检查。