【发布时间】:2015-02-26 03:52:31
【问题描述】:
我想用跟踪器替换 HTML 电子邮件中的所有链接。据我所知,有这个EVENT_BEFORE_SEND 事件。所以我创建了一些可以像下面这样使用的行为
$mailer = \Yii::$app->mailer;
/* @var $mailer \yii\mail\BaseMailer */
$mailer->attachBehavior('archiver', [
'class' => \app\MailTracker::class
]);
这是MyTracker 类的内容。
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);
}
}
现在的问题是\yii\mail\BaseMailer 没有提供在发送之前渲染 HTML 输出的方法。
如何做到这一点?
更新
我能做到这一点的唯一方法就是通过这种 hacky 方式。
/* @var $message \yii\swiftmailer\Message */
if ($message instanceof \yii\swiftmailer\Message) {
$swiftMessage = $message->getSwiftMessage();
$r = new \ReflectionObject($swiftMessage);
$parentClassThatHasBody = $r->getParentClass()
->getParentClass()
->getParentClass(); //\Swift_Mime_SimpleMimeEntity
$body = $parentClassThatHasBody->getProperty('_immediateChildren');
$body->setAccessible(true);
$children = $body->getValue($swiftMessage);
foreach ($children as $child) {
if ($child instanceof \Swift_MimePart &&
$child->getContentType() == 'text/html') {
$html = $child->getBody();
break;
}
}
print_r($html);
}
【问题讨论】:
-
我最终不得不做同样的事情。非常令人沮丧。
-
为了避免使用反射,我设法通过调用
$message->getChildren()[0]->getBody()获取邮件正文(未编码)
标签: php yii2 swiftmailer