diff --git a/Core/Lib/Email/NewMail.php b/Core/Lib/Email/NewMail.php index 8a86693132..1fac40c165 100644 --- a/Core/Lib/Email/NewMail.php +++ b/Core/Lib/Email/NewMail.php @@ -237,6 +237,36 @@ public function cc(string $email, string $name = ''): NewMail return $this; } + /** + * Limpia el BCC del mail + */ + public function clearBCC(): NewMail + { + $this->mail->clearBCCs(); + + return $this; + } + + /** + * Limpia el CC del mail + */ + public function clearCC(): NewMail + { + $this->mail->clearCCs(); + + return $this; + } + + /** + * Limpia el To del mail + */ + public function clearTo(): NewMail + { + $this->mail->clearAddresses(); + + return $this; + } + public static function create(): NewMail { return new static(); @@ -335,6 +365,8 @@ public function replyTo(string $address, string $name = ''): NewMail /** * Envía el correo. + * + * Si se envía correctamente, borra los BCC, CC y To del email después de enviarlo. * * @throws Exception * @throws LoaderError @@ -410,6 +442,10 @@ public function send(): bool if ($this->mail->send()) { $this->saveMailSent(); + // limpiar campos del email después de enviar + $this->clearTo(); + $this->clearCC(); + $this->clearBCC(); return true; } diff --git a/Test/Core/Lib/Email/NewMailTest.php b/Test/Core/Lib/Email/NewMailTest.php index 7d35ac4f3d..f3f8ec7e10 100644 --- a/Test/Core/Lib/Email/NewMailTest.php +++ b/Test/Core/Lib/Email/NewMailTest.php @@ -20,7 +20,9 @@ namespace FacturaScripts\Test\Core\Lib\Email; use FacturaScripts\Core\Lib\Email\NewMail; +use PHPMailer\PHPMailer\PHPMailer; use PHPUnit\Framework\TestCase; +use ReflectionClass; final class NewMailTest extends TestCase { @@ -67,4 +69,25 @@ public function testBCC(): void $this->assertEmpty($mailer->getCcAddresses()); $this->assertCount(1, $mailer->getBccAddresses()); } + + // testear que las funciones de borrado borran correctamente las cosas + public function testClear(): void + { + $mailer = NewMail::create() + ->to('to@facturascripts.com') + ->cc('cc@facturascripts.com') + ->bcc('bcc@facturascripts.com'); + + $this->assertNotEmpty($mailer->getToAddresses()); + $mailer->clearTo(); + $this->assertEmpty($mailer->getToAddresses()); + + $this->assertNotEmpty($mailer->getCcAddresses()); + $mailer->clearCC(); + $this->assertEmpty($mailer->getCcAddresses()); + + $this->assertNotEmpty($mailer->getBccAddresses()); + $mailer->clearBCC(); + $this->assertEmpty($mailer->getBccAddresses()); + } }