-
Notifications
You must be signed in to change notification settings - Fork 118
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Laravel 10 #181
Laravel 10 #181
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,68 +4,80 @@ | |
|
||
use Illuminate\Support\Arr; | ||
use Sendportal\Base\Services\Messages\MessageTrackingOptions; | ||
use Swift_Mailer; | ||
use Swift_Message; | ||
use Swift_SmtpTransport; | ||
use Swift_TransportException; | ||
|
||
use Symfony\Component\Mailer\Transport; | ||
use Symfony\Component\Mailer\Transport\Dsn; | ||
use Symfony\Component\Mailer\Transport\Smtp\EsmtpTransport; | ||
use Symfony\Component\Mailer\Transport\Smtp\EsmtpTransportFactory; | ||
use Symfony\Component\Mailer\Mailer; | ||
use Symfony\Component\Mime\Email; | ||
use Symfony\Component\Mime\Address; | ||
use Symfony\Component\Mailer\Exception\TransportException; | ||
|
||
class SmtpAdapter extends BaseMailAdapter | ||
{ | ||
/** @var Swift_Mailer */ | ||
/** @var Mailer */ | ||
protected $client; | ||
|
||
/** @var Swift_SmtpTransport */ | ||
/** @var ESmtpTransport */ | ||
protected $transport; | ||
|
||
public function send(string $fromEmail, string $fromName, string $toEmail, string $subject, MessageTrackingOptions $trackingOptions, string $content): string | ||
{ | ||
$failedRecipients = []; | ||
|
||
// $failedRecipients = []; | ||
try { | ||
$result = $this->resolveClient()->send($this->resolveMessage($subject, $content, $fromEmail, $fromName, $toEmail), $failedRecipients); | ||
} catch (Swift_TransportException $e) { | ||
$result = $this->resolveClient()->send($this->resolveMessage($subject, $content, $fromEmail, $fromName, $toEmail)); | ||
} catch (TransportException $e) { | ||
return $this->resolveMessageId(0); | ||
} | ||
|
||
return $this->resolveMessageId($result); | ||
} | ||
|
||
protected function resolveClient(): Swift_Mailer | ||
protected function resolveClient(): Mailer | ||
{ | ||
if ($this->client) { | ||
return $this->client; | ||
} | ||
|
||
$this->client = new Swift_Mailer($this->resolveTransport()); | ||
$this->client = new Mailer($this->resolveTransport()); | ||
|
||
return $this->client; | ||
} | ||
|
||
protected function resolveTransport(): Swift_SmtpTransport | ||
protected function resolveTransport(): EsmtpTransport | ||
{ | ||
if ($this->transport) { | ||
return $this->transport; | ||
} | ||
|
||
$this->transport = new Swift_SmtpTransport( | ||
$factory = new EsmtpTransportFactory(); | ||
|
||
$encryption = Arr::get($this->config, 'encryption'); | ||
|
||
$scheme = !is_null($encryption) && $encryption === 'tls' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I kinda feel like this should just be exposed in the config instead of some behind the scenes magic. Thoughts? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. encryption isn't an explicit config in symfony, so the examples I saw where checking for tls and the port to determine the schema, then mailer is configured only by a dsn string... |
||
? ((Arr::get($this->config, 'port') == 465) ? 'smtps' : 'smtp') | ||
: ''; | ||
|
||
$dsn = new Dsn( | ||
$scheme, | ||
Arr::get($this->config, 'host'), | ||
Arr::get($this->config, 'port'), | ||
Arr::get($this->config, 'encryption') | ||
Arr::get($this->config, 'username'), | ||
Arr::get($this->config, 'password'), | ||
Arr::get($this->config, 'port') | ||
); | ||
|
||
$this->transport->setUsername(Arr::get($this->config, 'username')); | ||
$this->transport->setPassword(Arr::get($this->config, 'password')); | ||
$this->transport->setAuthMode('login'); | ||
$this->transport = $factory->create($dsn); | ||
|
||
return $this->transport; | ||
} | ||
|
||
protected function resolveMessage(string $subject, string $content, string $fromEmail, string $fromName, string $toEmail): Swift_Message | ||
protected function resolveMessage(string $subject, string $content, string $fromEmail, string $fromName, string $toEmail): Email | ||
{ | ||
$msg = new Swift_Message($subject, $content, 'text/html'); | ||
|
||
$msg->setTo($toEmail); | ||
$msg->setFrom($fromEmail, $fromName); | ||
$msg = (new Email()) | ||
->from(new Address($fromEmail, $fromName)) | ||
->to($toEmail) | ||
->subject($subject) | ||
->html($content); | ||
|
||
return $msg; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there anyone else that can help out on this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems it's not possible to get failed recipients. see symfony/symfony#33812