Skip to content
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

Added possibility for CC and BCC #43

Open
wants to merge 2 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
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
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();
}
}
35 changes: 25 additions & 10 deletions src/Database/Type/JsonType.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,7 @@ class JsonType extends BaseType implements OptionalConvertInterface
*/
public function toPHP($value, DriverInterface $driver)
{
if ($value === null) {
return;
}

return json_decode($value, true);
return $this->decodeJson($value);
}

/**
Expand All @@ -33,11 +29,7 @@ public function toPHP($value, DriverInterface $driver)
*/
public function marshal($value)
{
if (is_array($value) || $value === null) {
return $value;
}

return json_decode($value, true);
return $this->decodeJson($value);
}

/**
Expand All @@ -61,4 +53,27 @@ public function requiresToPhpCast(): bool
{
return true;
}

/**
* Returns the given value as an array (if it is json or already an array)
* or as a string (if it is already a string)
*
* @param array|string|null $value json string, array or string to decode
* @return array|string|null depending on the input, see description
*/
private function decodeJson($value)
{
if (is_array($value) || $value === null) {
return $value;
}

$jsonDecode = json_decode($value, true);

// check, if the value is null after json_decode to handle plain strings
if ($jsonDecode === null) {
return $value;
}

return $jsonDecode;
}
}
7 changes: 6 additions & 1 deletion src/Database/Type/SerializeType.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@ public function toPHP($value, DriverInterface $driver)
return null;
}

return unserialize($value);
try {
return unserialize($value);
} catch (\Exception $e) {
// return value, if the value isn't serialized
return $value;
}
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/EmailQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@ class EmailQueue
* @param array $options list of options for email sending. Possible keys:
*
* - subject : Email's subject
* - cc: array of carbon copy
* - bcc: array of blind carbon copy
* - 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)
* - headers: Key => Value list of extra headers for the email
* - theme: The View Theme to find the email templates
* - config : the name of the email config to be used for sending
*
* @return bool
*/
public static function enqueue($to, array $data, array $options = [])
Expand Down
6 changes: 6 additions & 0 deletions src/Model/Table/EmailQueueTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ public function initialize(array $config = []): void
* @param array $options list of options for email sending. Possible keys:
*
* - subject : Email's subject
* - cc: array of carbon copy
* - bcc: array of blind carbon copy
* - 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
Expand All @@ -66,6 +68,8 @@ public function enqueue($to, array $data, array $options = []): bool

$defaults = [
'subject' => '',
'cc' => '',
'bcc' => '',
'send_at' => new FrozenTime('now'),
'template' => 'default',
'layout' => 'default',
Expand Down Expand Up @@ -199,6 +203,8 @@ protected function _initializeSchema(TableSchemaInterface $schema): TableSchemaI
$schema->setColumnType('template_vars', $type);
$schema->setColumnType('headers', $type);
$schema->setColumnType('attachments', $type);
$schema->setColumnType('cc', $type);
$schema->setColumnType('bcc', $type);

return $schema;
}
Expand Down
10 changes: 10 additions & 0 deletions src/Shell/PreviewShell.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,16 @@ public function preview($e)

$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($e['attachments']);
}
Expand Down
10 changes: 10 additions & 0 deletions src/Shell/SenderShell.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,16 @@ public function main(): void
$transport->setConfig(['additionalParameters' => "-f $from"]);
}

// 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($e->attachments);
}
Expand Down
2 changes: 2 additions & 0 deletions tests/Fixture/EmailQueueFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class EmailQueueFixture extends TestFixture
public $fields = [
'id' => ['type' => 'uuid', 'null' => false],
'email' => ['type' => 'string', 'null' => false, 'default' => null, 'length' => 100, 'collate' => 'utf8_general_ci', 'charset' => 'utf8'],
'cc' => ['type' => 'string', 'null' => true, 'default' => null, 'length' => 129, 'collate' => 'utf8_general_ci', 'charset' => 'utf8'],
'bcc' => ['type' => 'string', 'null' => true, 'default' => null, 'length' => 129, 'collate' => 'utf8_general_ci', 'charset' => 'utf8'],
'from_name' => ['type' => 'string', 'null' => true, 'default' => null, 'length' => 100, 'collate' => 'utf8_general_ci', 'charset' => 'utf8'],
'from_email' => ['type' => 'string', 'null' => true, 'default' => null, 'length' => 100, 'collate' => 'utf8_general_ci', 'charset' => 'utf8'],
'subject' => ['type' => 'string', 'null' => false, 'default' => null, 'length' => 255, 'collate' => 'utf8_general_ci', 'charset' => 'utf8'],
Expand Down
26 changes: 26 additions & 0 deletions tests/TestCase/Model/Table/EmailQueueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ public function testEnqueue()
'error' => null,
'from_name' => null,
'from_email' => null,
'cc' => '',
'bcc' => ''
];
$sendAt = new Time($result['send_at']);
unset($result['id'], $result['created'], $result['modified'], $result['send_at']);
Expand Down Expand Up @@ -197,4 +199,28 @@ public function testProxy()
$this->assertEquals('custom', $email['template']);
$this->assertEquals('email', $email['layout']);
}

/**
* Test cc and bcc with string and array
*/
public function testCCAndBcc()
{
$cc = ['[email protected]', '[email protected]'];
$bcc = '[email protected]';

$result = EmailQueue::enqueue(
'[email protected]',
['a' => 'c'],
['subject' => 'Hey', 'cc' => $cc, 'bcc' => $bcc, 'template' => 'custom', 'layout' => 'email']
);
$this->assertTrue($result);
$email = $this->EmailQueue->find()
->where(['email' => '[email protected]'])
->first()
->toArray();

$this->assertEquals($cc[0], $email['cc'][0]);
$this->assertEquals($cc[1], $email['cc'][1]);
$this->assertEquals($bcc, $email['bcc']);
}
}