crabstudio / email-queue
CakePHP 3.x 的 EmailQueue 插件
    1.0.6
    2017-02-22 10:31 UTC
Requires
- php: >=5.4.16
- cakephp/cakephp: ~3.0
Requires (Dev)
Suggests
- crabstudio/recaptcha: Allows you to use Google recaptcha
README
CakePHP 3 的 EmailQueue 插件
注意
本插件基于 Lorenzo cakephp-email-queue,但略有不同
- Sent to `one|multiple` people
- CC to `none|one|multiple` people
- BCC to `none|one|multiple` people
- Included helpers `Html, Text, Number`
安装
您可以使用 composer 将此插件安装到您的 CakePHP 应用程序中。
安装 composer 包的推荐方法是
composer require crabstudio/email-queue
然后在您的命令行中输入以下命令加载此插件
bin/cake plugin load EmailEnqueue --bootstrap
或者将此行粘贴到 config/bootstrap.php 文件的末尾
Plugin::load('EmailQueue', ['bootstrap' => true]);
创建所需表
有两种方法可以做到
使用迁移工具
bin/cake migrations migrate --plugin EmailQueue
将 sql 文件加载到您的数据库中
sql file located at: config/schema/email_queue.sql
用法
在您想存储新电子邮件到队列的任何地方调用 enqueue 函数。
/**
 * Stores a new email message in the queue.
 *
 * @param mixed|array $to           email or array of emails as recipients
 * @param array $data    associative array of variables to be passed to the email template
 * @param array $options list of options for email sending.
 *
 * $options Possible keys:
 * - subject : Email's subject
 * - send_at : date time sting representing the time this email should be sent at (in UTC)
 * - template :  the name of the element to use as template for the email message
 * - layout : the name of the layout to be used to wrap email message
 * - format: Type of template to use (html, text or both)
 * - config : the name of the email config to be used for sending
 * @param null|mixed|array $cc           null or email or array of emails as cc
 * @param null|mixed|array $bcc          null or email or array of emails as bcc
 * @param null|mixed|array $reply_to     null or email or array of emails as reply_to
 *
 * @return bool
 */
enqueue($to, array $data, array $options = [], $cc = null, $bcc = null, $reply_to = null)
示例
// In src/PostsController.php
public function send_email($id) {
	$post = $this->Posts->get($id);
	$result = enqueue(
		'customer@crabstudio.info',
		[
			'post' => $post,
			'request' => $this->request
		],
		[
			'subject' => __('New post notification'),
			'format' => 'html',
			'template' => 'Post/new_post_notification',  //template located here src/Template/Email/html/Post/new_post_notification.ctp
			'layout' => 'notification' //layout located here src/Template/Layout/Email/html/notification.ctp
			'config' => 'default',
		],
		'cc_to_me@crabstudio.info',
		'bcc_to_you@crabstudio.info',
		'reply_to_support@crabstudio.info'
	);
	if ($result) {
		$this->Flash->success(__('Enqueue email ok'));
	} else {
		$this->Flash->error(__('Enqueue email not ok'));
	}
}
安排任务
Linux
打开 crontab,然后设置 cronjob 如下
*       *       *       *       *       cd /var/www/your_project && bin/cake EmailQueue.sender
Windows
打开 任务计划程序,然后按照 这个教程 进行操作