Skip to content

Commit e972530

Browse files
authored
Merge pull request #37 from Superbition/fix/email-sending
Fix email SMTP & STARTTLS transmission fatal error
2 parents 7186195 + 05612a8 commit e972530

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

src/Email/EmailManager.class.php

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

33
namespace Polyel\Email;
44

5+
use Polyel;
56
use PHPMailer\PHPMailer\SMTP;
67
use PHPMailer\PHPMailer\PHPMailer;
78
use PHPMailer\PHPMailer\Exception;
@@ -91,6 +92,6 @@ public function send(string $to, string $from, array $ccs, array $bccs, Email $e
9192

9293
$mailer->Body = $email->message;
9394

94-
$mailer->send();
95+
Polyel::task($mailer);
9596
}
96-
}
97+
}

src/Http/Server.class.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,12 @@ public function boot()
8080
SWOOLE_PROCESS
8181
);
8282

83+
Polyel::setServer($this->server);
84+
8385
echo "Setting up the HTTP server configuration\n";
8486
$this->server->set([
8587
'worker_num' => swoole_cpu_num(),
88+
'task_worker_num' => 1,
8689
'package_max_length' => config("server.maxUploadSize"),
8790
'document_root' => config("server.publicRoot"),
8891
'enable_static_handler' => true,
@@ -132,6 +135,19 @@ public function registerReactors()
132135
$response->send($HttpResponse);
133136
});
134137

138+
/*
139+
* Currently the Swoole task worker system is only used
140+
* to offload email transmission because SMTP connections that
141+
* use STARTTLS do not work under coroutines and the TCP hook.
142+
*
143+
* So this is the workaround for now until further releases.
144+
*/
145+
$this->server->on('Task', function($server, $taskId, $reactorId, $email)
146+
{
147+
// Task system is only used to send emails...
148+
$email->send();
149+
});
150+
135151
$this->server->on("WorkerStop", function($server, $workerId)
136152
{
137153
$this->databaseManager->closeWorkerPool();
@@ -163,4 +179,4 @@ private function setDefaultResponseHeaders($response)
163179
$response->header("X-Powered-By", "Polyel-PHP");
164180
$response->header("Content-Type", "text/html; charset=utf-8");
165181
}
166-
}
182+
}

src/Polyel.class.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,26 @@ class Polyel
66
{
77
private const version = '0.8.1';
88

9+
// Used to access the HTTP server object
10+
private static $server;
11+
912
private static Polyel\Container\Container $container;
1013

1114
public static function version()
1215
{
1316
return self::version;
1417
}
1518

19+
public static function setServer($server)
20+
{
21+
self::$server = $server;
22+
}
23+
24+
public static function task(&$data)
25+
{
26+
self::$server->task($data);
27+
}
28+
1629
public static function createContainer($baseClass)
1730
{
1831
self::$container = new Container($baseClass);

0 commit comments

Comments
 (0)