From cbfc87e12dd907db68c7645008f78489dd47c609 Mon Sep 17 00:00:00 2001
From: Dave M <info@mcnd.ca>
Date: Mon, 22 May 2023 16:03:46 +0000
Subject: [PATCH] - Migrating to SymfonyMailer from Swift

---
 src/Email/EmailInfoTrait.php  | 46 ++++++++++++++++++++++++++++++++
 src/Email/MailerInterface.php |  6 ++---
 src/Email/SwiftMailer.php     | 42 ++---------------------------
 src/Email/SymfonyMailer.php   | 50 +++++++++++++++++++++++++++++++++++
 4 files changed, 101 insertions(+), 43 deletions(-)
 create mode 100644 src/Email/EmailInfoTrait.php
 create mode 100644 src/Email/SymfonyMailer.php

diff --git a/src/Email/EmailInfoTrait.php b/src/Email/EmailInfoTrait.php
new file mode 100644
index 0000000..cd2fe32
--- /dev/null
+++ b/src/Email/EmailInfoTrait.php
@@ -0,0 +1,46 @@
+<?php
+
+namespace TheBugs\Email;
+
+trait EmailInfoTrait
+{
+    protected /*stringable*/ $from;
+
+    protected string|\Stringable|array $to;
+
+    protected string|\Stringable|array $cc;
+
+    protected string|\Stringable|array $bcc;
+
+    protected array $attachments = [];
+
+    protected EmailConfiguration $emailConfiguration;
+
+    public function setFrom(string|\Stringable|array $from) : MailerInterface
+    {
+        $this->from = $from;
+
+        return $this;
+    }
+
+    public function setTo(string|\Stringable|array $to) : MailerInterface
+    {
+        $this->to = $to;
+
+        return $this;
+    }
+
+    public function setCC(string|\Stringable|array $cc) : MailerInterface
+    {
+        $this->cc = $cc;
+
+        return $this;
+    }
+
+    public function setBCC(string|\Stringable|array $bcc) : MailerInterface
+    {
+        $this->bcc = $bcc;
+
+        return $this;
+    }
+}
\ No newline at end of file
diff --git a/src/Email/MailerInterface.php b/src/Email/MailerInterface.php
index fac8716..f25a10f 100644
--- a/src/Email/MailerInterface.php
+++ b/src/Email/MailerInterface.php
@@ -6,9 +6,9 @@ interface MailerInterface
 {     
     public function send(string $subject, string $message, bool $html = true) : bool;
     
-    public function setFrom($from) : self;
+    public function setFrom(string|\Stringable|array $from) : self;
     
-    public function attach(\Swift_Attachment $attachment) : self;
+    public function attach(mixed $attachment) : self;
 
-    public function setTo($to) : self;
+    public function setTo(string|\Stringable|array $to) : self;
 }
\ No newline at end of file
diff --git a/src/Email/SwiftMailer.php b/src/Email/SwiftMailer.php
index e18bf98..90a91ce 100644
--- a/src/Email/SwiftMailer.php
+++ b/src/Email/SwiftMailer.php
@@ -4,20 +4,10 @@ namespace TheBugs\Email;
 
 class SwiftMailer implements MailerInterface
 {
-    protected $emailConfiguration;
+    use EmailInfoTrait;
     
     protected $transport;
     
-    protected /*stringable*/ $from;
-    
-    protected /*stringable|array*/ $to;
-    
-    protected /*stringable|array*/ $cc;
-    
-    protected /*stringable|array*/ $bcc;
-
-    protected array $attachments = [];
-    
     public function __construct(EmailConfiguration $configuration) {
         $this->emailConfiguration = $configuration;
         
@@ -44,38 +34,10 @@ class SwiftMailer implements MailerInterface
         return ( new \Swift_Mailer($this->transport) )->send($swiftObj);
     }
 
-    public function attach(\Swift_Attachment $attachment) : self
+    public function attach(mixed $attachment) : self
     {
         $this->attachments[] = $attachment;
 
         return $this;
     }
-    
-    public function setFrom(/*stringable|array*/ $from) : MailerInterface
-    {
-        $this->from = $from;
-        
-        return $this;
-    } 
-    
-    public function setTo(/*stringable|array*/ $to) : MailerInterface
-    {
-        $this->to = $to;
-        
-        return $this;
-    }
-    
-    public function setCC(/*stringable|array*/ $cc) : MailerInterface
-    {
-        $this->cc = $cc;
-        
-        return $this;
-    }
-    
-    public function setBCC(/*stringable|array*/ $bcc) : MailerInterface
-    {
-        $this->bcc = $bcc;
-        
-        return $this;
-    }
 }
\ No newline at end of file
diff --git a/src/Email/SymfonyMailer.php b/src/Email/SymfonyMailer.php
new file mode 100644
index 0000000..3cc0997
--- /dev/null
+++ b/src/Email/SymfonyMailer.php
@@ -0,0 +1,50 @@
+<?php
+
+namespace TheBugs\Email;
+
+use Symfony\Component\Mailer\{ Mailer, Transport };
+use Symfony\Component\Mime\Email;
+
+class SymfonyMailer implements MailerInterface
+{
+    use EmailInfoTrait;
+
+    public Mailer $mailer;
+
+    public function __construct(EmailConfiguration $configuration) {
+        $this->emailConfiguration = $configuration;
+        $this->mailer = new Mailer(Transport::fromDsn(spritf("smtp://%s:%s@%s:%d", $this->emailConfiguration->smtpUsername, $this->emailConfiguration->smtpPassword, $this->emailConfiguration->smtpHost, $this->emailConfiguration->smtpPort)));
+    }
+
+    public function send(string $subject, string $message, bool $html = true) : bool
+    {
+        $email = ( new Email() )
+            ->subject($subject)
+            ->from($this->from)
+            ->to(...( (array)$this->to ))
+            ->cc(...($this->cc ?? []))
+            ->bcc(...($this->bcc ?? []));
+
+        if ($html) {
+            $email->html($message);
+        }
+        else {
+            $email->text($message);
+        }
+
+        foreach ($this->attachments as $path) {
+            $email->attachFromPath($path);
+        }
+
+        $this->attachments = [];
+
+        return $this->mailer->send($email);
+    }
+
+    public function attach(mixed $attachment) : self
+    {
+        $this->attachments[] = $attachment;
+
+        return $this;
+    }
+}
\ No newline at end of file