Compare commits

...
5 Commits
5 changed files with 117 additions and 33 deletions

No files matched your search

+46
View File
@@ -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;
}
}
+4 -2
View File
@@ -6,7 +6,9 @@ interface MailerInterface
{ {
public function send(string $subject, string $message, bool $html = true) : bool; 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 setTo($to) : self; public function attach(mixed $attachment) : self;
public function setTo(string|\Stringable|array $to) : self;
} }
+13 -27
View File
@@ -4,16 +4,10 @@ namespace TheBugs\Email;
class SwiftMailer implements MailerInterface class SwiftMailer implements MailerInterface
{ {
protected $emailConfiguration; use EmailInfoTrait;
protected $transport; protected $transport;
protected $from;
protected $to;
protected $cc;
public function __construct(EmailConfiguration $configuration) { public function __construct(EmailConfiguration $configuration) {
$this->emailConfiguration = $configuration; $this->emailConfiguration = $configuration;
@@ -28,30 +22,22 @@ class SwiftMailer implements MailerInterface
->setFrom( $this->from ) ->setFrom( $this->from )
->setTo( $this->to ) ->setTo( $this->to )
->setCC($this->cc ?? []) ->setCC($this->cc ?? [])
->setBCC($this->bcc ?? [])
->setBody( $message, $html ? 'text/html' : 'text/plain' ); ->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); return ( new \Swift_Mailer($this->transport) )->send($swiftObj);
} }
public function setFrom(/*stringable|array*/ $from) : MailerInterface public function attach(mixed $attachment) : self
{ {
$this->from = $from; $this->attachments[] = $attachment;
return $this;
}
public function setTo(/*stringable|array*/ $to) : MailerInterface
{
$this->to = $to;
return $this; return $this;
} }
public function setCC(/*stringable|array*/ $cc) : MailerInterface
{
$this->cc = $cc;
return $this;
}
} }
+50
View File
@@ -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;
}
}
+4 -4
View File
@@ -9,10 +9,10 @@ use Psr\Http\Message\ResponseFactoryInterface,
Psr\Http\Server\MiddlewareInterface, Psr\Http\Server\MiddlewareInterface,
Psr\Http\Server\RequestHandlerInterface; Psr\Http\Server\RequestHandlerInterface;
use Zend\Diactoros\Response, use Laminas\Diactoros\Response,
Zend\Diactoros\ServerRequest, Laminas\Diactoros\ServerRequest,
Zend\Diactoros\Stream, Laminas\Diactoros\Stream,
Zend\Diactoros\Uri; Laminas\Diactoros\Uri;
class JavascriptMiddleware implements MiddlewareInterface class JavascriptMiddleware implements MiddlewareInterface
{ {