Compare commits
No commits in common. "master" and "dev" have entirely different histories.
|
@ -23,12 +23,10 @@ class EmailConfiguration
|
|||
public $fromAddress = "";
|
||||
|
||||
public $fromName = "";
|
||||
|
||||
public $smtpVerifyPeer = false;
|
||||
|
||||
public function __construct($type = self::AUTH_TYPE_SMTP)
|
||||
{
|
||||
$this->type = $type;
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -1,46 +0,0 @@
|
|||
<?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;
|
||||
}
|
||||
}
|
|
@ -6,13 +6,7 @@ interface MailerInterface
|
|||
{
|
||||
public function send(string $subject, string $message, bool $html = true) : bool;
|
||||
|
||||
public function setFrom(string|\Stringable|array $from) : self;
|
||||
public function setFrom($from) : self;
|
||||
|
||||
public function attach(mixed $attachment) : self;
|
||||
|
||||
public function setTo(string|\Stringable|array $to) : self;
|
||||
|
||||
public function setBCC(string|\Stringable|array $bcc) : self;
|
||||
|
||||
public function setCC(string|\Stringable|array $cc) : self;
|
||||
public function setTo($to) : self;
|
||||
}
|
|
@ -4,10 +4,16 @@ namespace TheBugs\Email;
|
|||
|
||||
class SwiftMailer implements MailerInterface
|
||||
{
|
||||
use EmailInfoTrait;
|
||||
protected $emailConfiguration;
|
||||
|
||||
protected $transport;
|
||||
|
||||
protected $from;
|
||||
|
||||
protected $to;
|
||||
|
||||
protected $cc;
|
||||
|
||||
public function __construct(EmailConfiguration $configuration) {
|
||||
$this->emailConfiguration = $configuration;
|
||||
|
||||
|
@ -22,22 +28,30 @@ class SwiftMailer implements MailerInterface
|
|||
->setFrom( $this->from )
|
||||
->setTo( $this->to )
|
||||
->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(mixed $attachment) : self
|
||||
|
||||
public function setFrom(/*stringable|array*/ $from) : MailerInterface
|
||||
{
|
||||
$this->attachments[] = $attachment;
|
||||
|
||||
$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;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,99 +0,0 @@
|
|||
<?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;
|
||||
|
||||
if ($this->emailConfiguration->smtpUsername) {
|
||||
$this->mailer = new Mailer(Transport::fromDsn(sprintf("smtp://%s:%s@%s:%d?verify_peer=%s", $this->emailConfiguration->smtpUsername, $this->emailConfiguration->smtpPassword, $this->emailConfiguration->smtpHost, $this->emailConfiguration->smtpPort, $this->emailConfiguration->smtpVerifyPeer ? 'true' : 'false')));
|
||||
}
|
||||
else {
|
||||
$this->mailer = new Mailer(Transport::fromDsn(sprintf("smtp://%s:%d?verify_peer=%s", $this->emailConfiguration->smtpHost, $this->emailConfiguration->smtpPort, $this->emailConfiguration->smtpVerifyPeer ? 'true' : 'false')));
|
||||
}
|
||||
}
|
||||
|
||||
public function send(string $subject, string $message, bool $html = true) : bool
|
||||
{
|
||||
$email = ( new Email() )
|
||||
->subject($subject)
|
||||
->from(...$this->from())
|
||||
->to(...$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 = [];
|
||||
|
||||
try {
|
||||
$this->mailer->send($email);
|
||||
}
|
||||
catch(\Exception $e) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function attach(mixed $attachment) : self
|
||||
{
|
||||
$this->attachments[] = $attachment;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
protected function from() : array
|
||||
{
|
||||
return $this->arrayToAddress($this->from);
|
||||
}
|
||||
|
||||
protected function to() : array
|
||||
{
|
||||
return $this->arrayToAddress($this->to);
|
||||
}
|
||||
|
||||
protected function cc() : array
|
||||
{
|
||||
return $this->arrayToAddress($this->cc ?? []);
|
||||
}
|
||||
|
||||
protected function bcc() : array
|
||||
{
|
||||
return $this->arrayToAddress($this->bcc ?? []);
|
||||
}
|
||||
|
||||
protected function arrayToAddress(string|\Stringable|array $array) : array
|
||||
{
|
||||
$list = [];
|
||||
|
||||
foreach((array) $array as $addr => $name) {
|
||||
if (is_numeric($addr)) {
|
||||
$list[] = $name;
|
||||
}
|
||||
else {
|
||||
$list[] = "$name <$addr>";
|
||||
}
|
||||
}
|
||||
|
||||
return $list;
|
||||
}
|
||||
}
|
|
@ -9,10 +9,10 @@ use Psr\Http\Message\ResponseFactoryInterface,
|
|||
Psr\Http\Server\MiddlewareInterface,
|
||||
Psr\Http\Server\RequestHandlerInterface;
|
||||
|
||||
use Laminas\Diactoros\Response,
|
||||
Laminas\Diactoros\ServerRequest,
|
||||
Laminas\Diactoros\Stream,
|
||||
Laminas\Diactoros\Uri;
|
||||
use Zend\Diactoros\Response,
|
||||
Zend\Diactoros\ServerRequest,
|
||||
Zend\Diactoros\Stream,
|
||||
Zend\Diactoros\Uri;
|
||||
|
||||
class JavascriptMiddleware implements MiddlewareInterface
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue