Skip to content

Commit 0e17057

Browse files
committed
Allow disable log saving
1 parent 2926783 commit 0e17057

File tree

4 files changed

+29
-0
lines changed

4 files changed

+29
-0
lines changed

.phpstorm.meta.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
registerArgumentsSet(
1313
'config_keys',
14+
'add_logs',
1415
'charset',
1516
'connection_timeout',
1617
'crlf',

guide/index.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ The default configs for connecting to the mail server are as follows:
162162
'response_timeout' => 5,
163163
'hostname' => gethostname(),
164164
'keep_alive' => false,
165+
'add_logs' => true,
165166
];
166167
167168
$mailer = new SMTPMailer($config);
@@ -177,6 +178,13 @@ If you are going to send more than one message on the same connection, set
177178
**keep_alive** to ``true``.
178179
This will use the same connection for all submissions.
179180

181+
Add Logs
182+
########
183+
184+
It is possible to clear the logs after each submission using the ``resetLogs`` method.
185+
186+
Also, you can disable log saving, to save memory, by setting **add_logs** to ``false``.
187+
180188
Conclusion
181189
----------
182190

src/Mailer.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ public function __construct(
7575
'response_timeout' => 'int',
7676
'hostname' => 'string',
7777
'keep_alive' => 'bool',
78+
'add_logs' => 'bool',
7879
])]
7980
protected function makeConfig(array $config) : array
8081
{
@@ -90,6 +91,7 @@ protected function makeConfig(array $config) : array
9091
'response_timeout' => 5,
9192
'hostname' => \gethostname(),
9293
'keep_alive' => false,
94+
'add_logs' => true,
9395
], $config);
9496
}
9597

@@ -120,6 +122,7 @@ protected function getConfig(string $key) : mixed
120122
'response_timeout' => 'int',
121123
'hostname' => 'string',
122124
'keep_alive' => 'bool',
125+
'add_logs' => 'bool',
123126
])]
124127
public function getConfigs() : array
125128
{
@@ -167,6 +170,9 @@ public function resetLogs() : static
167170
*/
168171
protected function addLog(string $command, string $response) : static
169172
{
173+
if ( ! $this->getConfig('add_logs')) {
174+
return $this;
175+
}
170176
$this->logs[] = [
171177
'command' => $command,
172178
'responses' => \explode(\PHP_EOL, $response),

tests/Mailers/SMTPMailerTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ public function testLogs() : void
9090
{
9191
\sleep(5);
9292
$this->smtp->send($this->getMessage());
93+
self::assertNotEmpty($this->smtp->getLogs());
9394
$log = $this->smtp->getLogs()[0];
9495
self::assertSame('', $log['command']);
9596
self::assertStringStartsWith('220 ', $log['responses'][0]);
@@ -128,6 +129,19 @@ public function testLogs() : void
128129
self::assertEmpty($this->smtp->getLogs());
129130
}
130131

132+
public function testLogsDisabled() : void
133+
{
134+
\sleep(5);
135+
$smtp = new SMTPMailer([
136+
'host' => \getenv('SMTP_HOST'),
137+
'username' => \getenv('SMTP_USERNAME'),
138+
'password' => \getenv('SMTP_PASSWORD'),
139+
'add_logs' => false,
140+
]);
141+
$smtp->send($this->getMessage());
142+
self::assertEmpty($smtp->getLogs());
143+
}
144+
131145
public function testConfigs() : void
132146
{
133147
foreach ($this->smtp->getConfigs() as $key => $value) {

0 commit comments

Comments
 (0)