Skip to content

Commit

Permalink
Initial work on support Symfony Mail
Browse files Browse the repository at this point in the history
  • Loading branch information
LukeTowers committed Dec 9, 2021
1 parent 66c16d4 commit d952ab5
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 369 deletions.
62 changes: 62 additions & 0 deletions src/Mail/MailManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php namespace Winter\Storm\Mail;

use InvalidArgumentException;
use Illuminate\Mail\MailManager as BaseMailManager;

/**
* Overrides the Laravel MailManager
* - Replaces the Laravel Mailer class with the Winter Mailer class
* - Fires mailer.beforeRegister & mailer.register events
*/
class MailManager extends BaseMailManager
{
/**
* Resolve the given mailer.
*
* @param string $name
* @return \Winter\Storm\Mail\Mailer
*
* @throws \InvalidArgumentException
*/
protected function resolve($name)
{
$config = $this->getConfig($name);

if (is_null($config)) {
throw new InvalidArgumentException("Mailer [{$name}] is not defined.");
}

/*
* Extensibility
*/
$this->app['events']->fire('mailer.beforeRegister', [$this]);

// Once we have created the mailer instance we will set a container instance
// on the mailer. This allows us to resolve mailer classes via containers
// for maximum testability on said classes instead of passing Closures.
$mailer = new Mailer(
$name,
$this->app['view'],
$this->createSymfonyTransport($config),
$this->app['events']
);

if ($this->app->bound('queue')) {
$mailer->setQueue($this->app['queue']);
}

// Next we will set all of the global addresses on this mailer, which allows
// for easy unification of all "from" addresses as well as easy debugging
// of sent messages since these will be sent to a single email address.
foreach (['from', 'reply_to', 'to', 'return_path'] as $type) {
$this->setGlobalAddress($mailer, $config, $type);
}

/*
* Extensibility
*/
$this->app['events']->fire('mailer.register', [$this, $mailer]);

return $mailer;
}
}
48 changes: 6 additions & 42 deletions src/Mail/MailServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,54 +5,18 @@
class MailServiceProvider extends MailServiceProviderBase
{
/**
* Register the Illuminate mailer instance. Carbon copy of Illuminate method.
* Replace the Illuminate mailer instance with the Winter Mailer.
*
* @return void
*/
protected function registerIlluminateMailer()
{
$this->app->singleton('mailer', function ($app) {
/*
* Extensibility
*/
$this->app['events']->fire('mailer.beforeRegister', [$this]);

$config = $app->make('config')->get('mail');

/*
* Winter mailer
*/
$mailer = new Mailer(
$app['view'],
$app['swift.mailer'],
$app['events']
);

if ($app->bound('queue')) {
$mailer->setQueue($app['queue']);
}

foreach (['from', 'reply_to', 'to'] as $type) {
$this->setGlobalAddress($mailer, $config, $type);
}

/*
* Extensibility
*/
$this->app['events']->fire('mailer.register', [$this, $mailer]);

return $mailer;
$this->app->singleton('mail.manager', function ($app) {
return new MailManager($app);
});
}

/**
* Register the Swift Transport instance.
*
* @return void
*/
protected function registerSwiftTransport()
{
$this->app->singleton('swift.transport', function ($app) {
return new TransportManager($app);
$this->app->bind('mailer', function ($app) {
return $app->make('mail.manager')->mailer();
});
}
}
14 changes: 7 additions & 7 deletions src/Mail/Mailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Mailer extends MailerBase
* @param string|array $view
* @param array $data
* @param \Closure|string $callback
* @return mixed
* @return \Illuminate\Mail\SentMessage|null
*/
public function send($view, array $data = [], $callback = null)
{
Expand Down Expand Up @@ -86,7 +86,7 @@ public function send($view, array $data = [], $callback = null)
*
* Parameters:
* - $view: View code as a string
* - $message: Illuminate\Mail\Message object, check Swift_Mime_SimpleMessage for useful functions.
* - $message: Illuminate\Mail\Message object, check Symfony\Component\Mime\Email for useful functions.
* - $data: Array
*
* Example usage (stops the sending process):
Expand All @@ -112,7 +112,7 @@ public function send($view, array $data = [], $callback = null)
/*
* Send the message
*/
$this->sendSwiftMessage($message->getSwiftMessage());
$this->sendSymfonyMessage($message->getSymfonyMessage());
$this->dispatchSentEvent($message);

/**
Expand Down Expand Up @@ -145,7 +145,7 @@ public function send($view, array $data = [], $callback = null)
* @param array $data
* @param mixed $callback
* @param array $options
* @return void
* @return mixed
*/
public function sendTo($recipients, $view, array $data = [], $callback = null, $options = [])
{
Expand Down Expand Up @@ -285,7 +285,7 @@ protected function buildQueueMailable($view, $data, $callback, $queueName = null
*
* @param string $text
* @param mixed $callback
* @return int
* @return \Illuminate\Mail\SentMessage|null
*/
public function raw($view, $callback)
{
Expand All @@ -305,7 +305,7 @@ public function raw($view, $callback)
* @param string $view
* @param mixed $callback
* @param array $options
* @return int
* @return \Illuminate\Mail\SentMessage|null
*/
public function rawTo($recipients, $view, $callback = null, $options = [])
{
Expand Down Expand Up @@ -420,7 +420,7 @@ protected function addContent($message, $view, $plain, $raw, $data)
/*
* Subject
*/
$customSubject = $message->getSwiftMessage()->getSubject();
$customSubject = $message->getSymfonyMessage()->getSubject();
if (
empty($customSubject) &&
($subject = array_get($result['settings'], 'subject'))
Expand Down
104 changes: 0 additions & 104 deletions src/Mail/Transport/MandrillTransport.php

This file was deleted.

Loading

0 comments on commit d952ab5

Please sign in to comment.