- Fixed some minor typos

This commit is contained in:
Dave Mc Nicoll 2023-05-31 18:45:16 +00:00
parent f0b2ab10fd
commit b284b67d91
2 changed files with 52 additions and 6 deletions

View File

@ -23,6 +23,8 @@ class EmailConfiguration
public $fromAddress = "";
public $fromName = "";
public $smtpVerifyPeer = false;
public function __construct($type = self::AUTH_TYPE_SMTP)
{

View File

@ -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,35 @@ 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) {
$list[] = "$name <$addr>";
}
return $list;
}
}