Skip to content

Commit b6d47b6

Browse files
committed
add support for cc, bcc and added tests
1 parent a1f0972 commit b6d47b6

24 files changed

+1293
-207
lines changed

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,5 @@ During the creation of an e-mail, the recipient will be replaced by the test e-m
111111

112112
## Todo
113113

114-
- Add support for CC, BCC and attachments
114+
- Add support attachments
115115
- Add support for Mailables
116-
- Add tests

composer.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,21 @@
1818
"Buildcode\\LaravelDatabaseEmails\\": "src/"
1919
}
2020
},
21+
"autoload-dev": {
22+
"psr-4": {
23+
"Tests\\": "tests/"
24+
}
25+
},
2126
"extra": {
2227
"laravel": {
2328
"providers": [
2429
"Buildcode\\LaravelGhost\\LaravelDatabaseEmailsServiceProvider"
2530
]
2631
}
32+
},
33+
"require-dev": {
34+
"orchestra/testbench": "~3.4",
35+
"phpunit/phpunit": "^6.2",
36+
"orchestra/database": "~3.4"
2737
}
2838
}

config/laravel-database-emails.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,16 @@
5353

5454
],
5555

56+
/*
57+
|--------------------------------------------------------------------------
58+
| Cronjob Limit
59+
|--------------------------------------------------------------------------
60+
|
61+
| Limit the number of e-mails the cronjob may send at a time. This is useful
62+
| if you want to prevent overlapping cronjobs. Keep in mind we already
63+
| handle overlapping gracefully, however setting a limit is adviced.
64+
|
65+
*/
66+
67+
'limit' => 20
5668
];

database/migrations/emails.stub

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@ class Create{{tableClassName}}Table extends Migration
1616
Schema::create('{{table}}', function (Blueprint $table) {
1717
$table->increments('id');
1818
$table->string('label')->nullable();
19-
$table->string('recipient', 255);
20-
$table->binary('subject', 60);
19+
$table->binary('recipient');
20+
$table->binary('cc')->nullable();
21+
$table->binary('bcc')->nullable();
22+
$table->binary('subject');
2123
$table->string('view', 255);
2224
$table->binary('variables')->nullable();
2325
$table->binary('body');

phpunit.xml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit backupGlobals="false"
3+
backupStaticAttributes="false"
4+
bootstrap="vendor/autoload.php"
5+
colors="true"
6+
convertErrorsToExceptions="true"
7+
convertNoticesToExceptions="true"
8+
convertWarningsToExceptions="true"
9+
processIsolation="false"
10+
stopOnFailure="false"
11+
syntaxCheck="false">
12+
13+
<testsuites>
14+
<testsuite name="Orchestra\Testbench Test Suite">
15+
<directory suffix="Test.php">./tests/</directory>
16+
</testsuite>
17+
</testsuites>
18+
<php>
19+
<env name="APP_ENV" value="testing"/>
20+
<env name="APP_KEY" value="AckfSECXIvnK5r28GVIWUAxmbBSjTsmF"/>
21+
<env name="CACHE_DRIVER" value="array"/>
22+
<env name="SESSION_DRIVER" value="array"/>
23+
<env name="QUEUE_DRIVER" value="sync"/>
24+
<env name="MAIL_DRIVER" value="log"/>
25+
<env name="DB_CONNECTION" value="sqlite"/>
26+
<env name="DB_DATABASE" value=":memory:"/>
27+
</php>
28+
29+
<filter>
30+
<whitelist addUncoveredFilesFromWhitelist="false">
31+
<directory suffix=".php">src/</directory>
32+
</whitelist>
33+
</filter>
34+
</phpunit>

src/Config.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
namespace Buildcode\LaravelDatabaseEmails;
4+
5+
class Config
6+
{
7+
/**
8+
* Get the maximum number of times an e-mail may be attempted to be sent.
9+
*
10+
* @return int
11+
*/
12+
public static function maxRetryCount()
13+
{
14+
return max(config('laravel-database-emails.retry.attempts', 1), 1);
15+
}
16+
17+
/**
18+
* Determine if newly created e-mails should be encrypted.
19+
*
20+
* @return bool
21+
*/
22+
public static function encryptEmails()
23+
{
24+
return config('laravel-database-emails.encrypt', false);
25+
}
26+
27+
/**
28+
* Determine if newly created e-mails should be sent to the test e-mail address.
29+
*
30+
* @return bool
31+
*/
32+
public static function testing()
33+
{
34+
return config('laravel-database-emails.testing.enabled', function () {
35+
return function () {
36+
return false;
37+
};
38+
})();
39+
}
40+
41+
/**
42+
* Get the test e-mail address.
43+
*
44+
* @return string
45+
*/
46+
public static function testEmailAddress()
47+
{
48+
return config('laravel-database-emails.testing.email');
49+
}
50+
51+
/**
52+
* Get the number of e-mails the cronjob may send at a time.
53+
*
54+
* @return int
55+
*/
56+
public static function cronjobEmailLimit()
57+
{
58+
return config('laravel-database-emails.limit', 20);
59+
}
60+
}

src/CreateEmailTableCommand.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ class CreateEmailTableCommand extends Command
3838
/**
3939
* Create a new queue job table command instance.
4040
*
41-
* @param \Illuminate\Filesystem\Filesystem $files
42-
* @param \Illuminate\Support\Composer $composer
41+
* @param \Illuminate\Filesystem\Filesystem $files
42+
* @param \Illuminate\Support\Composer $composer
4343
* @return void
4444
*/
4545
public function __construct(Filesystem $files, Composer $composer)
@@ -71,30 +71,30 @@ public function fire()
7171
/**
7272
* Create a base migration file for the table.
7373
*
74-
* @param string $table
74+
* @param string $table
7575
* @return string
7676
*/
7777
protected function createBaseMigration($table = 'emails')
7878
{
7979
return $this->laravel['migration.creator']->create(
80-
'create_'.$table.'_table', $this->laravel->databasePath().'/migrations'
80+
'create_' . $table . '_table', $this->laravel->databasePath() . '/migrations'
8181
);
8282
}
8383

8484
/**
8585
* Replace the generated migration with the job table stub.
8686
*
87-
* @param string $path
88-
* @param string $table
89-
* @param string $tableClassName
87+
* @param string $path
88+
* @param string $table
89+
* @param string $tableClassName
9090
* @return void
9191
*/
9292
protected function replaceMigration($path, $table, $tableClassName)
9393
{
9494
$stub = str_replace(
9595
['{{table}}', '{{tableClassName}}'],
9696
[$table, $tableClassName],
97-
$this->files->get(__DIR__.'/../database/migrations/emails.stub')
97+
$this->files->get(__DIR__ . '/../database/migrations/emails.stub')
9898
);
9999

100100
$this->files->put($path, $stub);

src/Decorators/EncryptEmail.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ public function __construct(EmailDecorator $decorator)
1616
'variables' => encrypt(json_encode($this->email->getVariables())),
1717
'body' => encrypt(view($this->email->getView(), $this->email->getVariables())->render()),
1818
]);
19+
20+
if ($this->email->hasCc()) {
21+
$this->email->cc = encrypt($this->email->getCc());
22+
}
23+
24+
if ($this->email->hasBcc()) {
25+
$this->email->bcc = encrypt($this->email->getBcc());
26+
}
1927
}
2028

2129
public function getEmail()

src/Decorators/PrepareEmail.php

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Buildcode\LaravelDatabaseEmails\Decorators;
44

5+
use Buildcode\LaravelDatabaseEmails\Config;
56
use Buildcode\LaravelDatabaseEmails\Email;
67

78
class PrepareEmail implements EmailDecorator
@@ -12,21 +13,48 @@ public function __construct(Email $email)
1213
{
1314
$this->email = $email;
1415

15-
$email->body = view($email->getView(), $email->getVariables());
16+
if ($email->hasCc()) {
17+
if (!is_array($email->cc)) {
18+
$email->cc = [$email->cc];
19+
}
1620

17-
$email->variables = json_encode($email->getVariables());
21+
if (Config::testing()) {
22+
$email->cc = array_map(function () {
23+
return Config::testEmailAddress();
24+
}, $email->cc);
25+
}
1826

19-
if ($email->hasScheduledDate()) {
20-
$email->scheduled_at = $email->getScheduledDateAsCarbon()->toDateTimeString();
27+
$email->cc = json_encode($email->cc);
28+
}
29+
30+
if ($email->hasBcc()) {
31+
if (!is_array($email->bcc)) {
32+
$email->bcc = [$email->bcc];
33+
}
34+
35+
if (Config::testing()) {
36+
$email->bcc = array_map(function () {
37+
return Config::testEmailAddress();
38+
}, $email->bcc);
39+
}
40+
41+
$email->bcc = json_encode($email->bcc);
2142
}
2243

23-
$this->email->encrypted = config('laravel-database-emails.encrypt', false);
2444

25-
$test = config('laravel-database-emails.testing.enabled', false);
45+
$email->body = view($email->getView(), $email->hasVariables() ? $email->getVariables() : [])->render();
2646

27-
if ($test()) {
28-
$email->recipient = config('laravel-database-emails.testing.email');
47+
$email->variables = json_encode($email->getVariables());
48+
49+
if ($email->isScheduled()) {
50+
$email->scheduled_at = $email->getScheduledDateAsCarbon()->toDateTimeString();
2951
}
52+
53+
if (Config::testing()) {
54+
$email->recipient = Config::testEmailAddress();
55+
}
56+
57+
$this->email->encrypted = Config::encryptEmails();
3058
}
3159

3260
public function getEmail()

0 commit comments

Comments
 (0)