From b65f664c67442aa3a285817dc85ab0516cce7290 Mon Sep 17 00:00:00 2001 From: hagy Date: Mon, 7 Mar 2022 13:26:31 +0900 Subject: [PATCH] fix return value for single email address of Message::normalizeEmail() --- src/Message.php | 6 +-- tests/functional/ManagerSMTPCest.php | 65 ++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 3 deletions(-) diff --git a/src/Message.php b/src/Message.php index 628fe9c..92b5f78 100644 --- a/src/Message.php +++ b/src/Message.php @@ -265,6 +265,7 @@ public function getBcc() public function sender($email, $name = null) { $emails = $this->normalizeEmail($email); + $emails = is_array($email) ? $email : [$emails]; foreach ($emails as $email) { $this->getMessage()->setSender($email, $name); @@ -843,7 +844,7 @@ protected function createEmbedViaData($data, $name = null) * * @param string|array|\Traversable $email * - * @return array + * @return string|array */ protected function normalizeEmail($email) { @@ -862,8 +863,7 @@ protected function normalizeEmail($email) return $emails; } else { - $emails[] = $this->getManager()->normalizeEmail($email); - return $emails; + return $this->getManager()->normalizeEmail($email); } } } diff --git a/tests/functional/ManagerSMTPCest.php b/tests/functional/ManagerSMTPCest.php index 1f4d902..9b2170b 100644 --- a/tests/functional/ManagerSMTPCest.php +++ b/tests/functional/ManagerSMTPCest.php @@ -184,4 +184,69 @@ public function mailerManagerCreateMessageFromView(FunctionalTester $I) $I->assertEquals($body, $mail->Content->Body); $I->assertStringContainsString('Subject: ' . $subject, $mail->Raw->Data); } + + public function mailerManagerCreateMessageWithName(FunctionalTester $I) + { + $sender = 'example_sender@gmail.com'; + $name = 'Example Name'; + $from = 'example_from@gmail.com'; + $to = 'example_to@gmail.com'; + $subject = 'Hello SMTP'; + $body = 'Lorem Ipsum'; + + $mailer = new Manager($this->config); + + $message = $mailer->createMessage() + ->sender($sender, $name) + ->from($from, $name) + ->to($to, $name) + ->subject($subject) + ->content($body); + + $message->send(); + + $opts = [ + 'http' => [ + 'method' => 'GET', + 'header' => 'Accept-language: en\r\n' + ] + ]; + + $context = stream_context_create($opts); + + // Get all mail send in the MailHog SMTP + $dataMail = file_get_contents($this->baseUrl . 'messages', false, $context); + $dataMail = \json_decode($dataMail); + + //Check that there are one mail send + $I->assertCount(3, $dataMail); + + $mail = $dataMail[0]; + + $mailFromData = $mail->From; + $mailToData = end($mail->To); + + $mailFrom = $mailFromData->Mailbox . '@' . $mailFromData->Domain; + $mailTo = $mailToData->Mailbox . '@' . $mailToData->Domain; + + $I->assertEquals($sender, $mailFrom); + $I->assertEquals($to, $mailTo); + + $headers = $mail->Content->Headers; + + $mailSenderWithName = end($headers->Sender); + $senderWithName = sprintf('%s <%s>', $name, $sender); + $I->assertEquals($senderWithName, $mailSenderWithName); + + $mailFromWithName = end($headers->From); + $fromWithName = sprintf('%s <%s>', $name, $from); + $I->assertEquals($fromWithName, $mailFromWithName); + + $mailToWithName = end($headers->To); + $toWithName = sprintf('%s <%s>', $name, $to); + $I->assertEquals($toWithName, $mailToWithName); + + $I->assertEquals($body, $mail->Content->Body); + $I->assertStringContainsString('Subject: ' . $subject, $mail->Raw->Data); + } }