J
J
Julia Kovalenko2016-05-20 11:16:38
Yii
Julia Kovalenko, 2016-05-20 11:16:38

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
                    ]
                ])); 
    }
}

And rewrote the send () method, as I need. It now just throws the task into the German worker.
The worker looks like 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;
    }
}

The worker code is the code of the standard \yii\swiftmailer\Mailer::send() method.
As a result, I get an error:
Exception 'yii\base\UnknownMethodException' with message 'Calling unknown method: app\libraries\shakura\gearman\GearmanMailer::sendMessage()'

For some reason, params['this'] does not see the sendMessage() method in the worker.
I passed to the worker $this == an instance of the GearmanMailer class, which is inherited from \yii\swiftmailer\Mailer, in which the sendMessage() method is defined.
What am I doing wrong?
ps I tried to explicitly specify in the worker:
$swift_mailer = new \yii\swiftmailer\Mailer();
.....
$isSuccessful = $swift_mailer->sendMessage($params['message']);

As a result, I received:
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

1 answer(s)
M
melnikov_m, 2016-05-20
@kovalenko_jul_s

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 question

Ask a Question

731 491 924 answers to any question