From 34b850c7808fea6856c3afd0bfbb8ae97c1c855a Mon Sep 17 00:00:00 2001 From: Dave Mc Nicoll Date: Fri, 25 Feb 2022 16:41:49 +0000 Subject: [PATCH] Bugfixes made on the interface --- src/Email/MailerInterface.php | 2 ++ src/Email/SwiftMailer.php | 17 ++++++++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/Email/MailerInterface.php b/src/Email/MailerInterface.php index eddf706..fac8716 100644 --- a/src/Email/MailerInterface.php +++ b/src/Email/MailerInterface.php @@ -8,5 +8,7 @@ interface MailerInterface public function setFrom($from) : self; + public function attach(\Swift_Attachment $attachment) : self; + public function setTo($to) : self; } \ No newline at end of file diff --git a/src/Email/SwiftMailer.php b/src/Email/SwiftMailer.php index cca9b3c..e18bf98 100644 --- a/src/Email/SwiftMailer.php +++ b/src/Email/SwiftMailer.php @@ -15,6 +15,8 @@ class SwiftMailer implements MailerInterface protected /*stringable|array*/ $cc; protected /*stringable|array*/ $bcc; + + protected array $attachments = []; public function __construct(EmailConfiguration $configuration) { $this->emailConfiguration = $configuration; @@ -32,9 +34,22 @@ class SwiftMailer implements MailerInterface ->setCC($this->cc ?? []) ->setBCC($this->bcc ?? []) ->setBody( $message, $html ? 'text/html' : 'text/plain' ); - + + foreach ($this->attachments as $attachment) { + $swiftObj->attach($attachment); + } + + $this->attachments = []; + return ( new \Swift_Mailer($this->transport) )->send($swiftObj); } + + public function attach(\Swift_Attachment $attachment) : self + { + $this->attachments[] = $attachment; + + return $this; + } public function setFrom(/*stringable|array*/ $from) : MailerInterface {