Skip to content

added commands #51

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file modified LICENSE
100644 → 100755
Empty file.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ and queue a new one by storing the correct data:
email template
- Third arguments is an array of options, possible options are
* `subject`: Email's subject
* `cc`: Email's carbon copy (string with email, Array with email as key, name as value or email as value (without name))
* `bcc`: Email's blind carbon copy (string with email, Array with email as key, name as value or email as value (without name))
* `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. (maximum supported length is 100 chars)
* `layout`: the name of the layout to be used to wrap email message
Expand Down
Empty file modified config/Migrations/20181120010607_add_error_message.php
100644 → 100755
Empty file.
Empty file.
Empty file modified config/Migrations/20190814000000_AlterTemplateToEmailQueue.php
100644 → 100755
Empty file.
43 changes: 43 additions & 0 deletions config/Migrations/20211105000000_AddCCAndBccToEmailQueue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

use Migrations\AbstractMigration;

class AddCcAndBccToEmailQueue extends AbstractMigration
{
/**
* Change Method.
*
* More information on this method is available here:
* http://docs.phinx.org/en/latest/migrations.html#the-change-method
* @return void
*/
public function change()
{
$table = $this->table('email_queue');
$table->addColumn('cc', 'string', [
'default' => null,
'limit' => 129,
'null' => true,
'after' => 'email'
]);
$table->addColumn('bcc', 'string', [
'default' => null,
'limit' => 129,
'null' => true,
'after' => 'cc'
]);
$table->addIndex([
'cc',
], [
'name' => 'BY_CC',
'unique' => false,
]);
$table->addIndex([
'bcc',
], [
'name' => 'BY_BCC',
'unique' => false,
]);
$table->update();
}
}
131 changes: 131 additions & 0 deletions src/Command/PreviewCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
<?php

declare(strict_types=1);

namespace EmailQueue\Command;

use Cake\Command\Command;
use Cake\Console\Arguments;
use Cake\Console\ConsoleIo;
use Cake\Console\ConsoleOptionParser;
use Cake\Core\Configure;
use Cake\Mailer\Mailer;
use Cake\ORM\TableRegistry;

/**
* Preview command.
*/
class PreviewCommand extends Command
{
/**
* Hook method for defining this command's option parser.
*
* @see https://book.cakephp.org/4/en/console-commands/commands.html#defining-arguments-and-options
* @param \Cake\Console\ConsoleOptionParser $parser The parser to be defined
* @return \Cake\Console\ConsoleOptionParser The built parser.
*/
public function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionParser
{
$parser = parent::buildOptionParser($parser);

return $parser;
}

/**
* Implement this method with your command's logic.
*
* @param \Cake\Console\Arguments $args The command arguments.
* @param \Cake\Console\ConsoleIo $io The console io
* @return null|void|int The exit code or null for success
*/
public function execute(Arguments $args, ConsoleIo $io)
{
$this->io = &$io;
Configure::write('App.baseUrl', '/');

$conditions = [];
if ($args->getArgumentAt(0)) {
$conditions['id IN'] = $args->getArgumentAt(0);
}

$emailQueue = TableRegistry::getTableLocator()->get('EmailQueue', ['className' => EmailQueueTable::class]);
$emails = $emailQueue->find()->where($conditions)->all()->toList();

if (!$emails) {
$this->io->out('No emails found');

return;
}

// $this->io->clear();
foreach ($emails as $i => $email) {
if ($i) {
$this->io->ask('Hit a key to continue');
// $this->clear();
}
$this->io->out('Email :' . $email['id']);
$this->preview($email);
}
}

/**
* Preview email
*
* @param array $e email data
* @return void
*/
public function preview($e)
{
$configName = $e['config'];
$template = $e['template'];
$layout = $e['layout'];
$headers = empty($e['headers']) ? [] : (array)$e['headers'];
$theme = empty($e['theme']) ? '' : (string)$e['theme'];

$email = new Mailer($configName);

// set cc
if (!empty($e['cc'])) {
$email->setCC($e['cc']);
}

// set bcc
if (!empty($e['bcc'])) {
$email->setBcc($e['bcc']);
}

if (!empty($e['attachments'])) {
$email->setAttachments(unserialize($e['attachments']));
}

$email->setTransport('Debug')
->setTo($e['email'])
->setSubject($e['subject'])
->setEmailFormat($e['format'])
->addHeaders($headers)
->setMessageId(false)
->setReturnPath($email->getFrom())
->setViewVars(unserialize($e['template_vars']));

$email->viewBuilder()
->setTheme($theme)
->setTemplate($template)
->setLayout($layout);

$return = $email->deliver();

$this->io->out('Content:');
$this->io->hr();
$this->io->out($return['message']);
$this->io->hr();
$this->io->out('Headers:');
$this->io->hr();
$this->io->out($return['headers']);
$this->io->hr();
$this->io->out('Data:');
$this->io->hr();
debug($e['template_vars']);
$this->io->hr();
$this->io->out('');
}
}
Loading