Answer the question
In order to leave comments, you need to log in
How to override the default \yii\swiftmailer\Mailer::send() method in Yii2?
I need to override the \yii\swiftmailer\Mailer::send() method.
I want to send emails through Gearman.
I created a class that I inherited from \yii\swiftmailer\Mailer.
class GearmanMailer extends \yii\swiftmailer\Mailer
{
private $_view = [];
private $_viewPath;
private $_message;
public function send($message){
\Yii::$app->gearman->getDispatcher()->background('SendMailWorker', new JobWorkload([
'params' => [
'message' => $message,
'this' => $this
]
]));
}
}
class SendMailWorker extends JobBase
{
public $thread_limit = 1;
public function execute(\GearmanJob $job = null)
{
$params = $this->getWorkload($job)->getParams();
if (!$params['this']->beforeSend($params['message'])) {
return false;
}
$address = $params['message']->getTo();
if (is_array($address)) {
$address = implode(', ', array_keys($address));
}
\Yii::info('Sending email "' . $params['message']->getSubject() . '" to "' . $address . '"', __METHOD__);
if ($params['this']->useFileTransport) {
$isSuccessful = $params['this']->saveMessage($params['message']);
} else {
$isSuccessful = $params['this']->sendMessage($params['message']);
}
$params['this']->afterSend($params['message'], $isSuccessful);
return $isSuccessful;
}
}
Exception 'yii\base\UnknownMethodException' with message 'Calling unknown method: app\libraries\shakura\gearman\GearmanMailer::sendMessage()'
$swift_mailer = new \yii\swiftmailer\Mailer();
.....
$isSuccessful = $swift_mailer->sendMessage($params['message']);
Exception 'yii\base\UnknownMethodException' with message 'Calling unknown method: yii\swiftmailer\Mailer::sendMessage()'
Answer the question
In order to leave comments, you need to log in
sendMessage() in yii\swiftmailer\Mailer is declared as a protected function and therefore does not see...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question