larablocks / pigeon
为Laravel 5提供的更灵活的电子邮件消息构建器,包括链式方法、可重用的消息配置以及消息布局和模板视图管理。
Requires
- php: >=5.6.4
- illuminate/config: ~5.4
- illuminate/log: ~5.4
- illuminate/mail: ~5.4
Requires (Dev)
- fzaninotto/faker: ~1.5
- mockery/mockery: 0.9.*
- phpunit/phpunit: ~5.7
README
为Laravel 5提供的更灵活的电子邮件消息构建器,包括链式方法、可重用的消息类型配置以及电子邮件布局和模板视图管理。
注意:所有Larablocks包都将与主要Laravel框架版本发布同步。(例如,Pigeon 5.4.*已测试与Laravel 5.4.*兼容,而Pigeon 5.1.*已测试与Laravel 5.1.*兼容。)
安装
将 larablocks/pigeon
添加到 composer.json
的要求中
{ "require": { "larablocks/pigeon": "~5.4" } }
使用 composer update
更新您的包或使用 composer install
安装。
Laravel集成
要将此集成到您的Laravel项目中,您需要添加服务提供者。打开 app.php
,并向提供者数组中添加一个新项目。
Larablocks\Pigeon\PigeonServiceProvider::class,
然后,您可以添加一个外观以方便使用。在您的 app.php
配置文件中,将以下行添加到 aliases
数组中。
'Pigeon' => Larablocks\Pigeon\Pigeon::class,
注意:Pigeon外观将自动加载,因此您不需要将其添加到 app.php
文件中,但您可能仍想记录别名。
要发布默认配置文件 config/pigeon.php
以及默认电子邮件视图文件,请使用Artisan命令
php artisan vendor:publish --provider="Larablocks\Pigeon\PigeonServiceProvider"
如果您希望不发布视图文件,只发布配置,请使用Artisan命令
php artisan vendor:publish --provider="Larablocks\Pigeon\PigeonServiceProvider" --tag="config"
作为外观使用
Pigeon::
设置通用消息属性
Pigeon将在您构建消息之前加载配置中 default
区域中设置的属性。
####设置消息地址
所有这些地址添加方法都可以与任何地址添加函数(to、cc、bcc、replyTo、from、sender)一起使用
添加没有名称的单个地址
Pigeon::to('john.doe@domain.com') Pigeon::cc('john.doe@domain.com') Pigeon::bcc('john.doe@domain.com') Pigeon::replyTo('john.doe@domain.com') Pigeon::from('john.doe@domain.com') Pigeon::sender('john.doe@domain.com')
添加带有名称的单个地址
Pigeon::to('john.doe@domain.com', 'John Doe') ....
添加没有名称的地址数组
Pigeon::to(['john.doe@domain.com', 'jane.doe@domain.com']) ...
添加有名称和没有名称的地址数组
Pigeon::to(['john.doe@domain.com' => 'John Doe', 'jane.doe@domain.com']) ...
####设置主题
Pigeon::subject('My Subject')
####文件附件
添加没有选项的单个文件
Pigeon::attach('/path/to/file/attachment')
添加带有选项的单个文件
Pigeon::attach('/path/to/file/attachment', ['as' => 'Attachment', 'mime' => 'jpg'])
添加文件数组
Pigeon::attach([ [ 'path' => '/path/to/file/attachment1' 'options' => [] ], [ 'path' => '/path/to/file/attachment2' 'options' => ['as' => 'Attachment 2', 'mime' => 'pdf'] ] ])
设置消息视图属性
####设置布局视图文件
Pigeon::layout('emails.layouts.my_layout_view')
####设置模板视图文件
Pigeon::template('emails.templates.my_template_view')
####传递视图变量
传递简单变量
Pigeon::pass([ 'stringVariable' => 'test string', 'intVariable' => 2, 'boolVariable' => true ])
传递对象变量
$user = new User(); $user->first_name = 'John'; $user->last_name = 'Doe'; Pigeon::pass([ 'userObjectVariable' => $user ])
如果多次使用 pass()
,则将先前传递的变量与当前传递的集合合并。
注意:请确保将布局和模板视图文件中预先定义的所有变量传递给您的Pigeon消息。
####清除视图变量
清除先前传递的所有视图变量
Pigeon::clear()
自定义消息类型
自定义消息类型将在 config/pigeon.php
文件中配置。在此文件中,您可以找到如何正确设置自定义消息类型的示例。
默认
在 config\pigeon.php
中设置Pigeon发送的所有消息的默认值
'default' => [ 'to' => [], 'cc' => [], 'bcc' => [], 'replyTo' => [], 'from' => [], // if nothing is entered here, your mail.php default will still be used 'sender' => [], 'attachments' => [], 'subject' => 'Pigeon Delivery', 'layout' => 'emails.layouts.default', 'template' => 'emails.templates.default', 'message_variables' => [] ]
####加载自定义消息
在 config\pigeon.php
中设置特定消息类型要使用Pigeon发送的默认值
'custom_message_type' => [ 'from' => ['from@myapp.com' => 'My Custom App'], 'subject' => 'My Pigeon Custom Message', 'layout' => 'emails.layouts.default', 'template' => 'emails.templates.default' ]
这将加载为custom_message_type
定义的所有消息属性。
Pigeon::type('custom_message_type');
加载顺序
默认 -> 自定义消息(如果已设置和加载) -> 使用单个Pigeon函数设置的属性
发送消息
####发送消息
Pigeon::send();
####发送原始消息
将字符串作为send()函数的参数传递,它将使用该字符串作为原始消息发送,并忽略任何分配的视图文件或视图变量。
Pigeon::send('This is my raw message');
示例 - 一同使用
Pigeon::to(['john.doe@domain.com', 'jane.doe@domain.com']) ->cc('fred.doe@domain.com') ->bcc('george.doe@domain.com') ->subject('This is the Subject') ->attach('/path/to/file/attachment') ->layout('emails.layouts.my_layout_view') ->template('emails.templates.my_template_view') ->pass(['firstVariable' => 'test string', 'secondVariable' => 2, 'thirdVariable' => true]) ->send();
示例 - 简单调用
Pigeon::to('me@domain.com')->subject('Testing Pigeon')->send('Sending myself a quick raw message');
示例 - 发送自定义消息
Pigeon::type('custom_message_type')->to('me@domain.com')->send();
作为传递依赖项的使用
传递Pigeon作为依赖项将传递接口Larablocks\Pigeon\PigeonInterface
。目前,唯一实现此接口的库是Laravel提供的Larablocks\Pigeon\IlluminateMailer
,但我们希望允许未来使用其他邮件库。Pigeon的配置文件config/pigeon.php
自动将IlluminateMailer设置为默认邮件库。
'library' => 'IlluminateMailer',
###将Pigeon传递给构造函数
public function __construct(Larablocks\Pigeon\PigeonInterface $pigeon) { $this->pigeon = $pigeon; }
###启动新的默认消息
$this->pigeon->to('me@domain.com')->subject('Pigeon Raw Test Message')->send('Sending myself a quick raw message');
###启动新的自定义消息类型
$this->pigeon->type('custom_message_type')->to('me@domain.com')->send();
许可证
Pigeon是开源软件,许可协议为MIT许可协议