pnglabz/cakephp-elastic-email

此软件包已被废弃,不再维护。作者建议使用 sprintcube/cakephp-elastic-email 软件包。

CakePHP 3 的 ElasticEmail 插件 - 使用 Elastic Email API 发送事务性电子邮件

安装: 4

依赖项: 0

建议者: 0

安全: 0

星标: 4

关注者: 3

分支: 0

开放问题: 2

类型:cakephp-plugin

v1.0.2 2018-08-20 06:06 UTC

This package is auto-updated.

Last update: 2022-02-01 13:13:50 UTC


README

Build Status codecov Software License Latest Stable Version Total Downloads

此插件提供使用 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 证书错误,请按照以下步骤操作

  1. 打开 http://curl.haxx.se/ca/cacert.pem
  2. 复制整个页面并保存为 "cacert.pem"
  3. 打开您的 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.');

日程安排

您可以将邮件安排在未来的日期发送。您可以将时间设置至最多一年之后,即524160分钟。

$email = new Email('elasticemail');
$emailInstance = $email->getTransport();
$emailInstance->setScheduleTime(60); // after 1 hour from sending time
$email->send();

问题报告

如果您在使用此插件时遇到问题或发现任何错误,请在GitHub上提交一个问题。