diff --git a/src/Service/EmailService.php b/src/Service/EmailService.php index d7bf0fc..0f7cabe 100644 --- a/src/Service/EmailService.php +++ b/src/Service/EmailService.php @@ -130,6 +130,8 @@ class EmailService string $html, array $cc = [], array $bcc = [], + array $attachmentPaths = [], + array $attachments = [], int $priority = Email::PRIORITY_NORMAL ): bool { $email = (new Email()) @@ -141,6 +143,22 @@ class EmailService ->subject($subject) ->html($html); + foreach ($attachmentPaths as $attachmentPath) { + if (is_string($attachmentPath)) { + $email = $email->attachFromPath($attachmentPath); + } else { + $email = $email->attachFromPath($attachmentPath['path'], $attachmentPath['name'] ?? null, $attachmentPath['mimeType'] ?? null); + } + } + + foreach ($attachments as $attachment) { + if (is_string($attachment)) { + $email = $email->attach($attachment); + } else { + $email = $email->attach($attachment['content'], $attachment['name'] ?? null, $attachment['mimeType'] ?? null); + } + } + $this->mailer->send($email); return true; diff --git a/tests/Unit/EmailServiceTest.php b/tests/Unit/EmailServiceTest.php index 6534654..55d4c01 100644 --- a/tests/Unit/EmailServiceTest.php +++ b/tests/Unit/EmailServiceTest.php @@ -1,4 +1,5 @@ $dsn, 'mailSenderName' => $mail_sender_name, 'mailSender' => $mail_sender]); $result = $email->sendHtml( 'dongyun.li@luxcreo.ai', 'HDK Unit Test HTML', @@ -26,9 +31,15 @@ it('assertions that send HTML is available', function () use ($email) { HTML ); expect($result)->toBeTrue(); -})->skip(); +}); + +it('assertions that send Text is available', function () { + $dsn = 'smtp://account@luxcreo.ai:Qfsd8866@smtp.qiye.aliyun.com:465'; + $mail_sender_name = 'LuxCreo'; + $mail_sender = 'account@luxcreo.ai'; + // $email = new EmailService($dsn, $mail_sender_name, $mail_sender); + $email = make(EmailService::class, ['dsn' => $dsn, 'mailSenderName' => $mail_sender_name, 'mailSender' => $mail_sender]); -it('assertions that send Text is available', function () use ($email) { $result = $email->sendText( 'dongyun.li@luxcreo.ai', 'HDK Unit Test Text', @@ -37,9 +48,15 @@ it('assertions that send Text is available', function () use ($email) { Text ); expect($result)->toBeTrue(); -})->skip(); +}); + +it('assertions Error Receiver can be catch', function () { + $dsn = 'smtp://account@luxcreo.ai:Qfsd8866@smtp.qiye.aliyun.com:465'; + $mail_sender_name = 'LuxCreo'; + $mail_sender = 'account@luxcreo.ai'; + // $email = new EmailService($dsn, $mail_sender_name, $mail_sender); + $email = make(EmailService::class, ['dsn' => $dsn, 'mailSenderName' => $mail_sender_name, 'mailSender' => $mail_sender]); -it('assertions Error Receiver can be catch', function () use ($email) { try { $email->sendHtml( 'unknown@luxcreo.ai', @@ -62,4 +79,30 @@ Text } catch (TransportException $t) { expect($t->getCode())->toBe(554); } -})->skip('会报错必须运行在协程环境下'); +}); + +it('should can contain attachment', function () { + $dsn = 'smtp://account@luxcreo.ai:Qfsd8866@smtp.qiye.aliyun.com:465'; + $mail_sender_name = 'LuxCreo'; + $mail_sender = 'account@luxcreo.ai'; + // $email = new EmailService($dsn, $mail_sender_name, $mail_sender); + $email = make(EmailService::class, ['dsn' => $dsn, 'mailSenderName' => $mail_sender_name, 'mailSender' => $mail_sender]); + + $email->sendHtml( + 'dongyun.li@luxcreo.ai', + 'HDK Unit Test HTML', + <<Hello, World! +HTML, + [], + [], + [ + [ + 'path' => __DIR__ . '/../../README.md', + 'name' => 'README.md', + 'mimeType' => 'text/markdown', + ], + ], + [] + ); +})->only();