Compare commits
3
Commits
f0b2ab10fd
...
v1.0.0
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1f61c964bc | ||
|
|
fe6fdd3987 | ||
|
|
b284b67d91 |
No files matched your search
@@ -23,10 +23,12 @@ class EmailConfiguration
|
||||
public $fromAddress = "";
|
||||
|
||||
public $fromName = "";
|
||||
|
||||
public $smtpVerifyPeer = false;
|
||||
|
||||
public function __construct($type = self::AUTH_TYPE_SMTP)
|
||||
{
|
||||
$this->type = $type;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -11,4 +11,8 @@ interface MailerInterface
|
||||
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;
|
||||
}
|
||||
@@ -13,17 +13,23 @@ class SymfonyMailer implements MailerInterface
|
||||
|
||||
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)));
|
||||
|
||||
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(...( (array)$this->to ))
|
||||
->cc(...($this->cc ?? []))
|
||||
->bcc(...($this->bcc ?? []));
|
||||
->from(...$this->from())
|
||||
->to(...$this->to())
|
||||
->cc(...$this->cc())
|
||||
->bcc(...$this->bcc());
|
||||
|
||||
if ($html) {
|
||||
$email->html($message);
|
||||
@@ -38,7 +44,14 @@ class SymfonyMailer implements MailerInterface
|
||||
|
||||
$this->attachments = [];
|
||||
|
||||
return $this->mailer->send($email);
|
||||
try {
|
||||
$this->mailer->send($email);
|
||||
}
|
||||
catch(\Exception $e) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function attach(mixed $attachment) : self
|
||||
@@ -47,4 +60,40 @@ class SymfonyMailer implements MailerInterface
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user