diff --git a/src/Message.php b/src/Message.php index 123c1a0..77696d6 100644 --- a/src/Message.php +++ b/src/Message.php @@ -248,7 +248,8 @@ public function getBcc(): ?array */ public function sender(string $email, ?string $name = null): self { - $email = $this->normalizeEmail($email); + $emails = $this->normalizeEmail($email); + $emails = is_array($email) ? $email : [$emails]; if (is_string($email)) { $this->getMessage()->setSender($email, $name); @@ -785,7 +786,7 @@ protected function createEmbedViaData(string $data, ?string $name = null): \Swif * * @param string|array|\Traversable $email * - * @return array|string + * @return string|array */ protected function normalizeEmail($email) { diff --git a/tests/functional/ManagerSMTPCest.php b/tests/functional/ManagerSMTPCest.php index 484dc53..d13e5d7 100644 --- a/tests/functional/ManagerSMTPCest.php +++ b/tests/functional/ManagerSMTPCest.php @@ -200,4 +200,69 @@ public function mailerManagerCreateMessageFromView(FunctionalTester $I) curl_close($ch); $I->assertEquals(200, $httpCode); } + + 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); + } }