I want to replace all links in the HTML email with tracker. As far as I know there is this EVENT_BEFORE_SEND event. So I created some behavior that can be used like below
$mailer = \Yii::$app->mailer;
/* @var $mailer \yii\mail\BaseMailer */
$mailer->attachBehavior('archiver', [
'class' => \app\MailTracker::class
]);
Here’s the content of the MyTracker class.
class MailTracker extends Behavior {
public function events() {
return [
\yii\mail\BaseMailer::EVENT_BEFORE_SEND => 'trackMail',
];
}
/**
* @param \yii\mail\MailEvent $event
*/
public function trackMail($event) {
$message = $event->message;
$htmlOutput = $this->how_do_i_get_the_html_output();
$changedOutput = $this->changeLinkWithTracker($htmlOutput);
$message->getHtmlBody($changedOutput);
}
}
The problem now is \yii\mail\BaseMailer doesn’t provide method to get the HTML output rendered before sending.
How to do this?