narendravaghela / cakephp-sparkpost
SparkPostEmail 插件用于 CakePHP
dev-master
2016-10-20 13:58 UTC
Requires
- php: >=5.5.9
- cakephp/cakephp: >=3.0.0 <4.0
- sparkpost/php-sparkpost: ^1.2
Requires (Dev)
- cakephp/cakephp-codesniffer: dev-master
- phpunit/phpunit: 4.1.*
This package is auto-updated.
Last update: 2024-09-27 03:21:55 UTC
README
本插件提供了使用 SparkPost 进行邮件发送的功能。
要求
此插件有以下要求
- CakePHP 3.0.0 或更高版本。
- PHP 5.4.16 或更高版本。
安装
您可以使用 composer 将此插件安装到您的 CakePHP 应用程序中。
composer require narendravaghela/cakephp-sparkpost
安装后,加载插件
Plugin::load('SparkPostEmail');
或者,您可以使用 shell 命令来加载插件
$ bin/cake plugin load SparkPostEmail
配置
在 app.php 的 EmailTransport
设置中设置您的 SparkPost 凭据
'EmailTransport' => [ ... 'sparkpost' => [ 'className' => 'SparkPostEmail.SparkPost', 'apiKey' => '123456789123456789' // your api key ] ]
并在 Email
设置中为 sparkpost 创建新的交付配置文件。
'Email' => [ 'default' => [ 'transport' => 'default', 'from' => 'you@localhost', //'charset' => 'utf-8', //'headerCharset' => 'utf-8', ], 'sparkpost' => [ 'transport' => 'sparkpost' ] ]
使用方法
现在,您可以直接使用 CakePHP 的 Email
通过 SparkPost 发送电子邮件。
$email = new Email('sparkpost'); $result = $email->from(['foo@example.com' => 'Example Site']) ->to('bar@example.com') ->subject('Welcome to CakePHP') ->template('welcome') ->viewVars(['foo' => 'Bar']) ->emailFormat('both') ->attachments([ 'cake_icon1.png' => Configure::read('App.imageBaseUrl') . 'cake.icon.png', 'cake_icon2.png' => ['file' => Configure::read('App.imageBaseUrl') . 'cake.icon.png'], WWW_ROOT . 'favicon.ico' ]) ->send('Your email content here');
您还可以使用 ch:
前缀设置自定义标题。
$email = new Email('sparkpost'); $result = $email->from(['foo@example.com' => 'Example Site']) ->to('bar@example.com') ->addHeaders(['ch:My-Custom-Header' => 'Awesome header string']) ->addHeaders(['ch:Another-Custom-Header' => 'Awesome header string']) ->emailFormat('both') ->attachments([ 'cake_icon1.png' => Configure::read('App.imageBaseUrl') . 'cake.icon.png', 'cake_icon2.png' => ['file' => Configure::read('App.imageBaseUrl') . 'cake.icon.png'], WWW_ROOT . 'favicon.ico' ]) ->send('Your email content here');
您还可以使用 co:
前缀设置其他 SparkPost 支持的自定义参数。
$email = new Email('sparkpost'); $result = $email->from(['foo@example.com' => 'Example Site']) ->to('bar@example.com') ->emailFormat('both') ->addHeaders(['ch:My-Custom-Header' => 'Awesome header string']) ->addHeaders(['co:open_tracking' => true]) ->addHeaders(['co:click_tracking' => true]) ->addHeaders(['co:transactional' => true]) ->addHeaders(['co:metadata' => ['meta1' => 'value1', 'meta2' => 'value2']]) ->send('Your email content here');
就是这样。
问题报告
如果您在此插件或任何错误中遇到问题,请在本插件 GitHub 上的问题中打开问题。