From a76ce2cbadb83463a7f1f8d9558f9c732a2ed72e Mon Sep 17 00:00:00 2001 From: kato Date: Thu, 4 Jun 2026 15:17:41 +0900 Subject: [PATCH 1/4] =?UTF-8?q?fix=20#4406=20=E3=80=90=E3=83=A1=E3=83=BC?= =?UTF-8?q?=E3=83=AB=E3=80=91=E3=83=A1=E3=83=BC=E3=83=AB=E8=A8=AD=E5=AE=9A?= =?UTF-8?q?=E3=81=A7=E7=AE=A1=E7=90=86=E8=80=85=E3=83=A1=E3=83=BC=E3=83=AB?= =?UTF-8?q?=E3=82=A2=E3=83=89=E3=83=AC=E3=82=B9=E3=82=92=E3=82=AB=E3=83=B3?= =?UTF-8?q?=E3=83=9E=E5=8C=BA=E5=88=87=E3=82=8A=E3=81=A7=E8=A4=87=E6=95=B0?= =?UTF-8?q?=E8=A8=AD=E5=AE=9A=E3=81=99=E3=82=8B=E3=81=A8=E3=82=A8=E3=83=A9?= =?UTF-8?q?=E3=83=BC=E3=81=AB=E3=81=AA=E3=82=8B=E5=95=8F=E9=A1=8C=E3=82=92?= =?UTF-8?q?=E8=A7=A3=E6=B1=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../bc-mail/src/Mailer/MailMessageMailer.php | 11 +++-- .../TestCase/Mailer/MailMessageMailerTest.php | 41 +++++++++++++++++++ 2 files changed, 48 insertions(+), 4 deletions(-) diff --git a/plugins/bc-mail/src/Mailer/MailMessageMailer.php b/plugins/bc-mail/src/Mailer/MailMessageMailer.php index 2b1e3de397..859be55793 100644 --- a/plugins/bc-mail/src/Mailer/MailMessageMailer.php +++ b/plugins/bc-mail/src/Mailer/MailMessageMailer.php @@ -50,12 +50,15 @@ public function sendFormToAdmin( 'toAdmin' => [], ], $options); if (strpos($adminMail, ',') !== false) { - [$fromAdmin] = explode(',', $adminMail); + $adminMails = array_map('trim', explode(',', $adminMail)); + $adminMails = array_values(array_filter($adminMails, static fn($mail) => $mail !== '')); + $fromAdmin = $adminMails[0] ?? $adminMail; + $toAdmin = $adminMails; } else { - $fromAdmin = $adminMail; + $toAdmin = $adminMail; } $data['other']['mode'] = 'admin'; - $this->setTo($adminMail) + $this->setTo($toAdmin) ->setFrom($fromAdmin, $this->getFrom($mailContent)) ->setSubject($mailContent->subject_admin) ->setAttachments($attachments) @@ -102,7 +105,7 @@ public function sendFormToUser( 'toUser' => [], ], $options); if (strpos($adminMail, ',') !== false) { - [$fromAdmin] = explode(',', $adminMail); + [$fromAdmin] = array_map('trim', explode(',', $adminMail)); } else { $fromAdmin = $adminMail; } diff --git a/plugins/bc-mail/tests/TestCase/Mailer/MailMessageMailerTest.php b/plugins/bc-mail/tests/TestCase/Mailer/MailMessageMailerTest.php index b10d606ded..48d4b64c31 100644 --- a/plugins/bc-mail/tests/TestCase/Mailer/MailMessageMailerTest.php +++ b/plugins/bc-mail/tests/TestCase/Mailer/MailMessageMailerTest.php @@ -84,6 +84,47 @@ public function testSendFormToAdmin() $this->assertEquals('fields test', $vars['mailFields']); $this->assertEquals('admin', $vars['other']['mode']); } + /** + * test sendFormToAdmin with comma separated admin mail + */ + public function testSendFormToAdminWithMultipleRecipients() + { + //準備 + $data['message'] = 'message test'; + $data['mailContent'] = 'content test'; + $data['mailFields'] = 'fields test'; + $mailContent = MailContentFactory::make([ + 'description' => 'description test', + 'sender_1' => 'sender_1', + 'sender_name' => 'name 111', + 'subject_user' => 'subject_user 111', + 'subject_admin' => 'subject_admin 111', + 'form_template' => 'default', + 'mail_template' => 'mail_default', + 'redirect_url' => '/', + ])->getEntity(); + + //テスト + $this->MailMessageMailer->sendFormToAdmin( + $mailContent, + 'abc@example.com,def@example.com', + 'abcUser@example.com', + $data, + [], + [] + ); + + //戻り値を確認 + $this->assertEquals([ + 'abc@example.com' => 'abc@example.com', + 'def@example.com' => 'def@example.com', + ], $this->MailMessageMailer->getTo()); + $this->assertEquals(['abcUser@example.com' => 'abcUser@example.com'], $this->MailMessageMailer->getReplyTo()); + + $vars = $this->MailMessageMailer->viewBuilder()->getVars(); + $this->assertEquals('admin', $vars['other']['mode']); + } + /** * test sendFormToUser From e7a72a1093d9c521d964cdadaf9b277e62daa21d Mon Sep 17 00:00:00 2001 From: kato Date: Thu, 4 Jun 2026 15:31:07 +0900 Subject: [PATCH 2/4] =?UTF-8?q?=E3=82=B3=E3=83=91=E3=82=A4=E3=83=AD?= =?UTF-8?q?=E3=83=83=E3=83=88=E3=81=AE=E3=82=A2=E3=83=89=E3=83=90=E3=82=A4?= =?UTF-8?q?=E3=82=B9=E3=82=92=E5=85=83=E3=81=AB=E8=AA=BF=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../bc-mail/src/Mailer/MailMessageMailer.php | 39 +++++++++++-------- 1 file changed, 23 insertions(+), 16 deletions(-) diff --git a/plugins/bc-mail/src/Mailer/MailMessageMailer.php b/plugins/bc-mail/src/Mailer/MailMessageMailer.php index 859be55793..00ceb97cdf 100644 --- a/plugins/bc-mail/src/Mailer/MailMessageMailer.php +++ b/plugins/bc-mail/src/Mailer/MailMessageMailer.php @@ -49,14 +49,7 @@ public function sendFormToAdmin( $options = array_merge([ 'toAdmin' => [], ], $options); - if (strpos($adminMail, ',') !== false) { - $adminMails = array_map('trim', explode(',', $adminMail)); - $adminMails = array_values(array_filter($adminMails, static fn($mail) => $mail !== '')); - $fromAdmin = $adminMails[0] ?? $adminMail; - $toAdmin = $adminMails; - } else { - $toAdmin = $adminMail; - } + [$fromAdmin, $toAdmin] = $this->parseAndNormalizeMailAddresses($adminMail); $data['other']['mode'] = 'admin'; $this->setTo($toAdmin) ->setFrom($fromAdmin, $this->getFrom($mailContent)) @@ -69,9 +62,10 @@ public function sendFormToAdmin( if($mailContent->sender_2) { $this->setBcc(strpos($mailContent->sender_2, ',') === false? $mailContent->sender_2: explode(',', $mailContent->sender_2)); } - if($userMail) { + [$replyToUser] = $this->parseAndNormalizeMailAddresses($userMail); + if($replyToUser) { // カンマ区切りで複数設定されていた場合先頭のアドレスをreplayToに利用 - $this->setReplyTo(strpos($userMail, ',') === false? $userMail : strstr($userMail, ',', true)); + $this->setReplyTo($replyToUser); } if (empty($options['toAdmin'])) return; foreach($options['toAdmin'] as $key => $value) { @@ -104,13 +98,10 @@ public function sendFormToUser( $options = array_merge([ 'toUser' => [], ], $options); - if (strpos($adminMail, ',') !== false) { - [$fromAdmin] = array_map('trim', explode(',', $adminMail)); - } else { - $fromAdmin = $adminMail; - } + [$fromAdmin] = $this->parseAndNormalizeMailAddresses($adminMail); + [, $toUser] = $this->parseAndNormalizeMailAddresses($userMail); $data['other']['mode'] = 'user'; - $this->setTo($userMail) + $this->setTo($toUser) ->setFrom($fromAdmin, $this->getFrom($mailContent)) ->setReplyTo($fromAdmin) ->setSubject($mailContent->subject_user) @@ -142,4 +133,20 @@ public function getFrom(EntityInterface $mailContent) { return $mailContent->content->site->display_name; } + /** + * メールアドレス文字列を分割・正規化する + * + * @param string $mailAddresses + * @return array{0: string, 1: array} + */ + private function parseAndNormalizeMailAddresses(string $mailAddresses): array + { + $recipients = array_values(array_filter( + array_map('trim', explode(',', $mailAddresses)), + static fn($mail) => $mail !== '' + )); + $primaryAddress = $recipients[0] ?? ''; + return [$primaryAddress, $recipients]; + } + } From 08a82884d5c605b8d3084baabb21e46c31f7e830 Mon Sep 17 00:00:00 2001 From: kato Date: Thu, 4 Jun 2026 15:45:34 +0900 Subject: [PATCH 3/4] =?UTF-8?q?=E3=83=86=E3=82=B9=E3=83=88=E3=82=B3?= =?UTF-8?q?=E3=83=BC=E3=83=89=E3=82=82=E3=82=B3=E3=83=91=E3=82=A4=E3=83=AD?= =?UTF-8?q?=E3=83=83=E3=83=88=E3=81=AE=E3=82=A2=E3=83=89=E3=83=90=E3=82=A4?= =?UTF-8?q?=E3=82=B9=E3=82=92=E5=85=83=E3=81=AB=E8=AA=BF=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../TestCase/Mailer/MailMessageMailerTest.php | 77 ++++++++++++++++++- 1 file changed, 76 insertions(+), 1 deletion(-) diff --git a/plugins/bc-mail/tests/TestCase/Mailer/MailMessageMailerTest.php b/plugins/bc-mail/tests/TestCase/Mailer/MailMessageMailerTest.php index 48d4b64c31..ae650383d3 100644 --- a/plugins/bc-mail/tests/TestCase/Mailer/MailMessageMailerTest.php +++ b/plugins/bc-mail/tests/TestCase/Mailer/MailMessageMailerTest.php @@ -125,6 +125,44 @@ public function testSendFormToAdminWithMultipleRecipients() $this->assertEquals('admin', $vars['other']['mode']); } + /** + * test sendFormToAdmin trims and filters empty addresses + */ + public function testSendFormToAdminTrimsAndFiltersRecipients() + { + //準備 + $data['message'] = 'message test'; + $data['mailContent'] = 'content test'; + $data['mailFields'] = 'fields test'; + $mailContent = MailContentFactory::make([ + 'description' => 'description test', + 'sender_1' => 'sender_1', + 'sender_name' => 'name 111', + 'subject_user' => 'subject_user 111', + 'subject_admin' => 'subject_admin 111', + 'form_template' => 'default', + 'mail_template' => 'mail_default', + 'redirect_url' => '/', + ])->getEntity(); + + //テスト + $this->MailMessageMailer->sendFormToAdmin( + $mailContent, + 'abc@example.com, def@example.com,', + ' abcUser@example.com, ', + $data, + [], + [] + ); + + //戻り値を確認 + $this->assertEquals([ + 'abc@example.com' => 'abc@example.com', + 'def@example.com' => 'def@example.com', + ], $this->MailMessageMailer->getTo()); + $this->assertEquals(['abcUser@example.com' => 'abcUser@example.com'], $this->MailMessageMailer->getReplyTo()); + } + /** * test sendFormToUser @@ -147,7 +185,7 @@ public function testSendFormToUser() ])->getEntity(); //テスト - $this->MailMessageMailer->sendFormToUser($mailContent, 'abc@example.com', 'abcUser@example.com', $data, [], []); + $this->MailMessageMailer->sendFormToUser($mailContent, 'abc@example.com', 'abcUser@example.com', $data, []); //戻り値を確認 $this->assertEquals(['abc@example.com' => 'abc@example.com'], $this->MailMessageMailer->getReplyTo()); @@ -160,6 +198,43 @@ public function testSendFormToUser() $this->assertEquals('user', $vars['other']['mode']); } + /** + * test sendFormToUser trims and filters empty addresses + */ + public function testSendFormToUserTrimsAndFiltersRecipients() + { + //準備 + $data['message'] = 'message test'; + $data['mailContent'] = 'content test'; + $data['mailFields'] = 'fields test'; + $mailContent = MailContentFactory::make([ + 'description' => 'description test', + 'sender_1' => 'sender_1', + 'sender_name' => 'name 111', + 'subject_user' => 'subject_user 111', + 'subject_admin' => 'subject_admin 111', + 'form_template' => 'default', + 'mail_template' => 'mail_default', + 'redirect_url' => '/', + ])->getEntity(); + + //テスト + $this->MailMessageMailer->sendFormToUser( + $mailContent, + 'abc@example.com, def@example.com,', + 'abcUser@example.com, defUser@example.com,', + $data, + [] + ); + + //戻り値を確認 + $this->assertEquals(['abc@example.com' => 'abc@example.com'], $this->MailMessageMailer->getReplyTo()); + $this->assertEquals([ + 'abcUser@example.com' => 'abcUser@example.com', + 'defUser@example.com' => 'defUser@example.com', + ], $this->MailMessageMailer->getTo()); + } + /** * test getFrom */ From 46e52e0472af7bea75eec81308ba7f386a778842 Mon Sep 17 00:00:00 2001 From: kato Date: Mon, 8 Jun 2026 16:33:02 +0900 Subject: [PATCH 4/4] =?UTF-8?q?Copilot=E3=81=AB=E5=BE=93=E3=81=A3=E3=81=A6?= =?UTF-8?q?=E5=85=83=E3=81=8B=E3=82=89=E3=81=82=E3=81=A3=E3=81=9F=E3=80=8C?= =?UTF-8?q?=E3=82=B3=E3=83=A1=E3=83=B3=E3=83=88=E5=86=85=E3=81=AE=E3=80=8C?= =?UTF-8?q?replayTo=E3=80=8D=E3=81=AF=E3=82=BF=E3=82=A4=E3=83=9D=E3=81=A7?= =?UTF-8?q?=E3=80=81=E5=AE=9F=E9=9A=9B=E3=81=AE=E3=83=98=E3=83=83=E3=83=80?= =?UTF-8?q?=E5=90=8D=E3=81=AB=E5=90=88=E3=82=8F=E3=81=9B=E3=81=A6=E3=80=8C?= =?UTF-8?q?replyTo=E3=80=8D=E3=81=AB=E4=BF=AE=E6=AD=A3=E3=81=97=E3=81=9F?= =?UTF-8?q?=E6=96=B9=E3=81=8C=E8=AA=A4=E8=A7=A3=E3=81=8C=E3=81=82=E3=82=8A?= =?UTF-8?q?=E3=81=BE=E3=81=9B=E3=82=93=E3=80=8D=E3=81=AB=E5=AF=BE=E5=BF=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plugins/bc-mail/src/Mailer/MailMessageMailer.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/bc-mail/src/Mailer/MailMessageMailer.php b/plugins/bc-mail/src/Mailer/MailMessageMailer.php index 00ceb97cdf..2e3a7c017d 100644 --- a/plugins/bc-mail/src/Mailer/MailMessageMailer.php +++ b/plugins/bc-mail/src/Mailer/MailMessageMailer.php @@ -64,7 +64,7 @@ public function sendFormToAdmin( } [$replyToUser] = $this->parseAndNormalizeMailAddresses($userMail); if($replyToUser) { - // カンマ区切りで複数設定されていた場合先頭のアドレスをreplayToに利用 + // カンマ区切りで複数設定されていた場合先頭のアドレスをreplyToに利用 $this->setReplyTo($replyToUser); } if (empty($options['toAdmin'])) return;