Skip to content

Commit 65a0ec4

Browse files
committed
Replaced Swiftmailer with PHPMailer.
1 parent 4ed3faf commit 65a0ec4

File tree

3 files changed

+120
-3
lines changed

3 files changed

+120
-3
lines changed

Factory/FileLoggerSmtpFactory.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
use Qubus\Exception\Exception;
1313
use Qubus\Log\Logger;
1414
use Qubus\Log\Loggers\FileLogger;
15-
use Qubus\Log\Loggers\SwiftMailerLogger;
15+
use Qubus\Log\Loggers\PHPMailerLogger;
1616
use ReflectionException;
1717
use SplObjectStorage;
1818

@@ -36,10 +36,10 @@ public static function getLogger(): LoggerInterface
3636
object: new FileLogger(filesystem: $filesystem, threshold: LogLevel::INFO)
3737
);
3838

39-
$mail = SwiftMailerSmtpFactory::create();
39+
$mail = PHPMailerSmtpFactory::create();
4040

4141
if (env(key: 'LOGGER_FROM_EMAIL') !== null && env(key: 'LOGGER_TO_EMAIL') !== null) {
42-
$storage->attach(object: new SwiftMailerLogger(mailer: $mail, threshold: LogLevel::INFO, params: [
42+
$storage->attach(object: new PHPMailerLogger(mailer: $mail, threshold: LogLevel::INFO, params: [
4343
'from' => env(key: 'LOGGER_FROM_EMAIL'),
4444
'to' => env(key: 'LOGGER_TO_EMAIL'),
4545
'subject' => env(key: 'LOGGER_EMAIL_SUBJECT'),

Helpers/core.php

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,16 @@
55
namespace Codefy\Framework\Helpers;
66

77
use Codefy\Framework\Codefy;
8+
use Codefy\Framework\Factory\FileLoggerFactory;
9+
use Codefy\Framework\Support\CodefyMailer;
810
use Qubus\Config\Collection;
911
use Qubus\Dbal\Connection;
1012
use Qubus\Exception\Exception;
1113
use Qubus\Expressive\OrmBuilder;
14+
use ReflectionException;
1215

1316
use function file_exists;
17+
use function Qubus\Security\Helpers\__observer;
1418
use function Qubus\Support\Helpers\is_false__;
1519
use function Qubus\Support\Helpers\is_null__;
1620
use function rtrim;
@@ -106,3 +110,103 @@ function dbal(): Connection
106110
{
107111
return Codefy::$PHP->getDbConnection();
108112
}
113+
114+
/**
115+
* Alternative to PHP's native mail function with SMTP support.
116+
*
117+
* This is a simple mail function to see for testing or for
118+
* sending simple email messages.
119+
*
120+
* @param string|array $to Recipient(s)
121+
* @param string $subject Subject of the email.
122+
* @param string $message The email body.
123+
* @param array $headers An array of headers.
124+
* @param array $attachments An array of attachments.
125+
* @return bool
126+
* @throws Exception|ReflectionException|\PHPMailer\PHPMailer\Exception
127+
*/
128+
function mail(string|array $to, string $subject, string $message, array $headers = [], array $attachments = []): bool
129+
{
130+
// Instantiate CodefyMailer.
131+
$instance = new CodefyMailer(config: app(name: 'codefy.config'));
132+
133+
// Set the mailer transport.
134+
$instance = config(key: 'mailer.mail_transport') === 'smtp' ? $instance->withSmtp() : $instance->withIsMail();
135+
136+
// Detect HTML markdown.
137+
if (substr_count(haystack: $message, needle: '</') >= 1) {
138+
$instance = $instance->withHtml(isHtml: true);
139+
}
140+
141+
// Build recipient(s).
142+
$instance = $instance->withTo(address: $to);
143+
144+
// Set from name and from email from environment variables.
145+
__observer()->filter->addFilter('mail.from.name', fn() => env(key: 'MAILER_FROM_NAME'));
146+
__observer()->filter->addFilter('mail.from.email', fn() => env(key: 'MAILER_FROM_EMAIL'));
147+
// Set charset
148+
__observer()->filter->addFilter('mail.charset', fn() => 'utf-8');
149+
150+
// Loop through the filters.
151+
foreach (__observer()->filter->getHooks() as $hook) {
152+
if ($hook['hook'] === 'mail.from.name') {
153+
$fromName = $hook['callback']();
154+
}
155+
156+
if ($hook['hook'] === 'mail.from.email') {
157+
$fromEmail = $hook['callback']();
158+
}
159+
160+
if ($hook['hook'] === 'mail.charset') {
161+
$charset = $hook['callback']();
162+
}
163+
}
164+
165+
// Set email subject and body.
166+
$instance = $instance->withSubject(subject: $subject)->withBody(data: $message);
167+
168+
// Check for other headers and loop through them.
169+
if (!empty($headers)) {
170+
foreach ($headers as $name => $content) {
171+
if ($name === 'cc') {
172+
$instance = $instance->withCc(address: $content);
173+
}
174+
175+
if ($name === 'bcc') {
176+
$instance = $instance->withBcc(address: $content);
177+
}
178+
179+
if ($name === 'replyTo') {
180+
$instance = $instance->withReplyTo(address: $content);
181+
}
182+
183+
if (! in_array(needle: $name, haystack: ['MIME-Version','to','cc','bcc','replyTo'], strict: true)) {
184+
$instance = $instance->withCustomHeader(name: $name, value: $content);
185+
}
186+
}
187+
}
188+
189+
// Set X-Mailer header
190+
$instance = $instance->withXMailer(xmailer: 'CodefyPHP Framework ' . Application::APP_VERSION);
191+
192+
// Set email charset
193+
$instance = $instance->withCharset(charset: $charset ?? 'utf-8');
194+
195+
// Check if there are attachments and loop through them.
196+
if (!empty($attachments)) {
197+
foreach ($attachments as $filename => $filepath) {
198+
$filename = is_string(value: $filename) ? $filename : '';
199+
$instance = $instance->withAttachment(path: $filepath, name: $filename);
200+
}
201+
}
202+
203+
// Set sender.
204+
$instance = $instance->withFrom(address: $fromEmail ?? '', name: $fromName ?? '');
205+
206+
try {
207+
return $instance->send();
208+
} catch (\PHPMailer\PHPMailer\Exception $e) {
209+
FileLoggerFactory::getLogger()->error($e->getMessage(), ['function' => '\Codefy\Framework\Helpers\mail']);
210+
return false;
211+
}
212+
}

Support/CodefyMailer.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Codefy\Framework\Support;
6+
7+
use Codefy\Framework\Application;
8+
use Qubus\Mail\QubusMailer;
9+
10+
final class CodefyMailer extends QubusMailer
11+
{
12+
public const VERSION = Application::APP_VERSION;
13+
}

0 commit comments

Comments
 (0)