sprintcube/cakephp-elastic-email
CakePHP 3 的 ElasticEmail 插件 - 使用 Elastic Email API 发送事务性电子邮件
Requires
- php: >=5.6.0
- cakephp/cakephp: ^3.4.0
Requires (Dev)
- cakephp/cakephp-codesniffer: ^3.0
- phpunit/phpunit: ^5.7|^6.0
This package is auto-updated.
Last update: 2024-09-27 03:06:56 UTC
README
此插件提供使用 Elastic Email 的电子邮件投递。
需求
此插件有以下需求
- CakePHP 3.4.0 或更高版本。
- PHP 5.6 或更高版本。
安装
您可以使用 composer 将此插件安装到您的 CakePHP 应用程序中。
composer require sprintcube/cakephp-elastic-email
安装后,请 加载插件
Plugin::load('ElasticEmail');
或者,您可以使用 shell 命令加载插件
$ bin/cake plugin load ElasticEmail
设置
在 app.php 中的 EmailTransport
设置中设置您的 Elastic Email Api 密钥
'EmailTransport' => [ ... 'elasticemail' => [ 'className' => 'ElasticEmail.ElasticEmail', 'apiKey' => 'your-api-key' // your api key ] ]
如果您遇到 SSL 证书错误,请按照以下步骤操作
- 打开 http://curl.haxx.se/ca/cacert.pem
- 复制整个页面并保存为 "cacert.pem" 文件
- 打开您的 php.ini 文件并插入或更新以下行: curl.cainfo = "[pathtofile]\cacert.pem"
并在 Email
设置中创建新的投递配置文件。
'Email' => [ 'default' => [ 'transport' => 'default', 'from' => 'you@localhost', //'charset' => 'utf-8', //'headerCharset' => 'utf-8', ], 'elasticemail' => [ 'transport' => 'elasticemail' ] ]
使用方法
现在,您可以使用 CakePHP 的 Email
发送通过 Elastic Email 的电子邮件。
$email = new Email('elasticemail'); $email->setFrom(['you@yourdomain.com' => 'CakePHP Elastic Email']) ->setSender('someone@example.com', 'Someone') ->setTo('foo@example.com.com') ->addTo('bar@example.com') ->setHeaders(['X-Custom' => 'headervalue']) ->setSubject('Email from CakePHP Elastic Email plugin') ->send('Message from CakePHP Elastic Email plugin');
就是这样。
高级使用
您还可以使用一些其他选项通过 Elastic Email API 发送电子邮件。为此,获取传输实例并在发送电子邮件之前调用适当的方法。
事务性电子邮件
您可以标记电子邮件为 transactional
电子邮件。
$email = new Email('elasticemail'); $emailInstance = $email->getTransport(); $emailInstance->isTransactional(true); $email->send();
自定义头部
您可以通过传递自己的头部信息。它必须以 "X-" 为前缀。像使用默认的 Email::setHeaders
方法一样:
$email = new Email('elasticemail'); $email->setFrom(['you@yourdomain.com' => 'CakePHP Elastic Email']) ->setSender('someone@example.com', 'Someone') ->setTo('foo@example.com.com') ->addTo('bar@example.com') ->setHeaders([ 'X-Custom' => 'headervalue', 'X-MyHeader' => 'myvalue' ]) ->setSubject('Email from CakePHP Elastic Email plugin') ->send('Message from CakePHP Elastic Email plugin');
请确保您已从 Elastic Email 设置中启用自定义头部。
附件
使用 Email::setAttachments
方法设置您的附件。
$email = new Email('elasticemail'); $email->setFrom(['you@yourdomain.com' => 'CakePHP Elastic Email']) ->setSender('someone@example.com', 'Someone') ->setTo('foo@example.com.com') ->addTo('bar@example.com') ->setAttachments([ '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' ]) ->setSubject('Email from CakePHP Elastic Email plugin') ->send('Message from CakePHP Elastic Email plugin');
您需要在您的账户中拥有一些信用额度才能发送附件。否则,您将收到
Not enough credit for campaign.
错误。
模板
您可以使用在 Elastic Email 后端创建的模板。通过使用他们的 API 或从 URL 中获取模板 ID。使用 setTemplate
方法设置模板 ID。
$email = new Email('elasticemail'); $emailInstance = $email->getTransport(); $emailInstance->setTemplte(123); $email->send();
模板变量
Elastic Email 提供了一种使用模板变量替换模板内容的好方法。您可以在模板中使用变量如 {firstname}、{lastname} 并传递它们的替换值。
$mergeVars = [ 'firstname' => 'Foo', 'lastname' => 'Bar', 'title' => 'Good Title' ]; $email = new Email('elasticemail'); $emailInstance = $email->getTransport(); $emailInstance->setMergeVariables($mergeVars); $email->setFrom(['from@example.com' => 'CakePHP Elastic Email']) ->setTo('to@example.com') ->setEmailFormat('both') ->setSubject('{title} - Email from CakePHP Elastic Email plugin') ->send('Hello {firstname} {lastname}, <br> This is an email from CakePHP Elastic Email plugin.');
调度
您可以在未来的日期安排发送电子邮件。您可以将时间设置到未来的 1 年内,即 524160 分钟。
$email = new Email('elasticemail'); $emailInstance = $email->getTransport(); $emailInstance->setScheduleTime(60); // after 1 hour from sending time $email->send();
报告问题
如果您在此插件或任何错误中遇到问题,请在 GitHub 上创建一个问题。