Cannot re-use Hazaar\Mail\Adapter to send multiple emails via SMTP #218

Closed
opened 2021-02-11 01:55:40 +00:00 by jamie · 1 comment
jamie commented 2021-02-11 01:55:40 +00:00 (Migrated from git.hazaar.io)

There seems to be an issue with the socket used to connect to the SMTP server.

Example, with the default transport configured as SMTP:

<?php
function send($recipients){

  $mail = new \Hazaar\Mail\Adapter();

  $mail->setSubject('Testing');

  $mail->setBody('This is a test!');

  foreach($recipients as $recipient){

    $mail->clear();

    $mail->addTo($recipient);

    $mail->send();

  }
    
}

The first email will be sent, but the second will throw an exception:

2021-02-11 12:03:33 -     ERR - PROCESS ERROR:
PHP Notice:  Undefined property: Hazaar\Mail\Transport\Smtp::$socket in /home/jamie/Source/abe-web/ABE/vendor/hazaarlabs/hazaar-mail/src/Transport/Smtp.php on line 30
PHP Stack trace:
PHP   1. {main}() /home/jamie/Source/abe-web/ABE/vendor/hazaarlabs/hazaar-warlock/src/Runner.php:0
PHP   2. Hazaar\Warlock\Process::runner($application = *uninitialized*, $service_name = *uninitialized*) /home/jamie/Source/abe-web/ABE/vendor/hazaarlabs/hazaar-warlock/src/Runner.php:29
PHP   3. ReflectionMethod->invokeArgs(*uninitialized*, *uninitialized*) /home/jamie/Source/abe-web/ABE/vendor/hazaarlabs/hazaar-warlock/src/Process.php:550
PHP   4. Application\Model\Admin\Subsystem\ClientEmail->run($id = *uninitialized*) /home/jamie/Source/abe-web/ABE/vendor/hazaarlabs/hazaar-warlock/src/Process.php:550
PHP   5. Hazaar\Mail\Adapter->send($params = *uninitialized*) /home/jamie/Source/abe-web/ABE/application/models/Admin/Subsystem/ClientEmail.php:142
PHP   6. Hazaar\Mail\Transport\Smtp->send($to = *uninitialized*, $subject = *uninitialized*, $message = *uninitialized*, $extra_headers = *uninitialized*) /home/jamie/Source/abe-web/ABE/vendor/hazaarlabs/hazaar-mail/src/Adapter.php:369
PHP   7. Hazaar\Mail\Transport\Smtp->connect(*uninitialized*, *uninitialized*) /home/jamie/Source/abe-web/ABE/vendor/hazaarlabs/hazaar-mail/src/Transport/Smtp.php:82
There seems to be an issue with the socket used to connect to the SMTP server. Example, with the default transport configured as SMTP: ```php <?php function send($recipients){ $mail = new \Hazaar\Mail\Adapter(); $mail->setSubject('Testing'); $mail->setBody('This is a test!'); foreach($recipients as $recipient){ $mail->clear(); $mail->addTo($recipient); $mail->send(); } } ``` The first email will be sent, but the second will throw an exception: ``` 2021-02-11 12:03:33 - ERR - PROCESS ERROR: PHP Notice: Undefined property: Hazaar\Mail\Transport\Smtp::$socket in /home/jamie/Source/abe-web/ABE/vendor/hazaarlabs/hazaar-mail/src/Transport/Smtp.php on line 30 PHP Stack trace: PHP 1. {main}() /home/jamie/Source/abe-web/ABE/vendor/hazaarlabs/hazaar-warlock/src/Runner.php:0 PHP 2. Hazaar\Warlock\Process::runner($application = *uninitialized*, $service_name = *uninitialized*) /home/jamie/Source/abe-web/ABE/vendor/hazaarlabs/hazaar-warlock/src/Runner.php:29 PHP 3. ReflectionMethod->invokeArgs(*uninitialized*, *uninitialized*) /home/jamie/Source/abe-web/ABE/vendor/hazaarlabs/hazaar-warlock/src/Process.php:550 PHP 4. Application\Model\Admin\Subsystem\ClientEmail->run($id = *uninitialized*) /home/jamie/Source/abe-web/ABE/vendor/hazaarlabs/hazaar-warlock/src/Process.php:550 PHP 5. Hazaar\Mail\Adapter->send($params = *uninitialized*) /home/jamie/Source/abe-web/ABE/application/models/Admin/Subsystem/ClientEmail.php:142 PHP 6. Hazaar\Mail\Transport\Smtp->send($to = *uninitialized*, $subject = *uninitialized*, $message = *uninitialized*, $extra_headers = *uninitialized*) /home/jamie/Source/abe-web/ABE/vendor/hazaarlabs/hazaar-mail/src/Adapter.php:369 PHP 7. Hazaar\Mail\Transport\Smtp->connect(*uninitialized*, *uninitialized*) /home/jamie/Source/abe-web/ABE/vendor/hazaarlabs/hazaar-mail/src/Transport/Smtp.php:82 ```
jamie commented 2021-02-11 02:30:11 +00:00 (Migrated from git.hazaar.io)

Found the issue. It was pretty simple. I was using unset($this->socket); instead of $this->socket = null; which meant the local member variable would no longer exist on the second attempt to send stuff.

Found the issue. It was pretty simple. I was using `unset($this->socket);` instead of `$this->socket = null;` which meant the local member variable would no longer exist on the second attempt to send stuff.
jamie (Migrated from git.hazaar.io) closed this issue 2021-02-11 02:46:24 +00:00
jamie self-assigned this 2025-09-04 01:15:28 +00:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: hazaar/framework#218
No description provided.