Skip to content

Commit 7b88325

Browse files
committed
Fixed filters and imported Application class.
1 parent 86fa586 commit 7b88325

File tree

1 file changed

+6
-20
lines changed

1 file changed

+6
-20
lines changed

Helpers/core.php

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Codefy\Framework\Helpers;
66

7+
use Codefy\Framework\Application;
78
use Codefy\Framework\Codefy;
89
use Codefy\Framework\Factory\FileLoggerFactory;
910
use Codefy\Framework\Support\CodefyMailer;
@@ -148,25 +149,10 @@ function mail(string|array $to, string $subject, string $message, array $headers
148149
$instance = $instance->withTo(address: $to);
149150

150151
// Set from name and from email from environment variables.
151-
__observer()->filter->addFilter('mail.from.name', fn() => env(key: 'MAILER_FROM_NAME'));
152-
__observer()->filter->addFilter('mail.from.email', fn() => env(key: 'MAILER_FROM_EMAIL'));
152+
$fromName = __observer()->filter->applyFilter('mail.from.name', env(key: 'MAILER_FROM_NAME'));
153+
$fromEmail = __observer()->filter->applyFilter('mail.from.email', env(key: 'MAILER_FROM_EMAIL'));
153154
// Set charset
154-
__observer()->filter->addFilter('mail.charset', fn() => 'utf-8');
155-
156-
// Loop through the filters.
157-
foreach (__observer()->filter->getHooks() as $hook) {
158-
if ($hook['hook'] === 'mail.from.name') {
159-
$fromName = $hook['callback']();
160-
}
161-
162-
if ($hook['hook'] === 'mail.from.email') {
163-
$fromEmail = $hook['callback']();
164-
}
165-
166-
if ($hook['hook'] === 'mail.charset') {
167-
$charset = $hook['callback']();
168-
}
169-
}
155+
$charset = __observer()->filter->applyFilter('mail.charset', fn() => 'utf-8');
170156

171157
// Set email subject and body.
172158
$instance = $instance->withSubject(subject: $subject)->withBody(data: $message);
@@ -196,7 +182,7 @@ function mail(string|array $to, string $subject, string $message, array $headers
196182
$instance = $instance->withXMailer(xmailer: 'CodefyPHP Framework ' . Application::APP_VERSION);
197183

198184
// Set email charset
199-
$instance = $instance->withCharset(charset: $charset ?? 'utf-8');
185+
$instance = $instance->withCharset(charset: $charset ?: 'utf-8');
200186

201187
// Check if there are attachments and loop through them.
202188
if (!empty($attachments)) {
@@ -207,7 +193,7 @@ function mail(string|array $to, string $subject, string $message, array $headers
207193
}
208194

209195
// Set sender.
210-
$instance = $instance->withFrom(address: $fromEmail ?? '', name: $fromName ?? '');
196+
$instance = $instance->withFrom(address: $fromEmail, name: $fromName ?: '');
211197

212198
try {
213199
return $instance->send();

0 commit comments

Comments
 (0)