Skip to content

Commit

Permalink
Adds option to force mailer from mailable
Browse files Browse the repository at this point in the history
  • Loading branch information
octoberapp committed Sep 12, 2023
1 parent 5062ac8 commit d329e99
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 21 deletions.
30 changes: 30 additions & 0 deletions src/Mail/Mailable.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ class Mailable extends MailableBase
*/
public $siteContext;

/**
* @var string forceMailer forces the mailer to use.
*/
public $forceMailer;

/**
* Build the message.
*
Expand Down Expand Up @@ -110,4 +115,29 @@ public function withLocale($locale, $callback)
return parent::withLocale($locale, $callback);
});
}

/**
* forceMailer forces sending using a different mail driver, useful if lazy loading
* the mail driver configuration for multisite.
* @param string $mailer
* @return $this
*/
public function forceMailer($mailer)
{
$this->forceMailer = $mailer;

return $this;
}

/**
* mailer sets the name of the mailer that should send the message.
* @param string $mailer
* @return $this
*/
public function mailer($mailer)
{
$this->mailer = $this->forceMailer ?: $mailer;

return $this;
}
}
56 changes: 35 additions & 21 deletions src/Mail/Mailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,27 +89,27 @@ public function send($view, array $data = [], $callback = null)
$this->setGlobalToAndRemoveCcAndBcc($message);
}

/**
* @event mailer.prepareSend
* Fires before the mailer processes the sending action
*
* Parameters:
* - $view: View code as a string
* - $message: Illuminate\Mail\Message object, check Swift_Mime_SimpleMessage for useful functions.
*
* Example usage (stops the sending process):
*
* Event::listen('mailer.prepareSend', function ((\October\Rain\Mail\Mailer) $mailerInstance, (string) $view, (\Illuminate\Mail\Message) $message) {
* return false;
* });
*
* Or
*
* $mailerInstance->bindEvent('mailer.prepareSend', function ((string) $view, (\Illuminate\Mail\Message) $message) {
* return false;
* });
*
*/
/**
* @event mailer.prepareSend
* Fires before the mailer processes the sending action
*
* Parameters:
* - $view: View code as a string
* - $message: Illuminate\Mail\Message object, check Swift_Mime_SimpleMessage for useful functions.
*
* Example usage (stops the sending process):
*
* Event::listen('mailer.prepareSend', function ((\October\Rain\Mail\Mailer) $mailerInstance, (string) $view, (\Illuminate\Mail\Message) $message) {
* return false;
* });
*
* Or
*
* $mailerInstance->bindEvent('mailer.prepareSend', function ((string) $view, (\Illuminate\Mail\Message) $message) {
* return false;
* });
*
*/
if (
($this->fireEvent('mailer.prepareSend', [$view, $message], true) === false) ||
(Event::fire('mailer.prepareSend', [$this, $view, $message], true) === false)
Expand Down Expand Up @@ -300,6 +300,20 @@ protected function buildQueueMailable($view, $data, $callback, $queue)
call_user_func($callback, $mailable);
}

/**
* @event mailer.buildQueueMailable
* Process the mailable object used when adding mail to the queue
*
* Example usage:
*
* Event::listen('mailer.buildQueueMailable', function ((\October\Rain\Mail\Mailer) $mailerInstance, (\October\Rain\Mail\Mailable) $mailable) {
* $mailable->mailer('smtp');
* });
*
*/
$this->fireEvent('mailer.buildQueueMailable', [$mailable]);
Event::fire('mailer.buildQueueMailable', [$this, $mailable]);

return $mailable;
}

Expand Down

0 comments on commit d329e99

Please sign in to comment.