title: How do I get HTML email content before sending in Yii2?
body: <p>I want to replace all links in the HTML email with tracker. As far as I know there is this <code>EVENT_BEFORE_SEND</code> event. So I created some behavior that can be used like below</p>
  
  <pre><code>$mailer = \Yii::$app-&gt;mailer;
  /* @var $mailer \yii\mail\BaseMailer */
  $mailer-&gt;attachBehavior('archiver', [
     'class' =&gt; \app\MailTracker::class
  ]);
  </code></pre>
  
  <p>Here's the content of the <code>MyTracker</code> class.</p>
  
  <pre><code>class MailTracker extends Behavior {
      public function events() {
          return [
              \yii\mail\BaseMailer::EVENT_BEFORE_SEND =&gt; 'trackMail',
          ];
      }
  
      /**
       * @param \yii\mail\MailEvent $event
       */
       public function trackMail($event) {
          $message = $event-&gt;message;
  
          $htmlOutput = $this-&gt;how_do_i_get_the_html_output();
          $changedOutput = $this-&gt;changeLinkWithTracker($htmlOutput);
          $message-&gt;getHtmlBody($changedOutput);
       }
  }
  </code></pre>
  
  <p>The problem now is <code>\yii\mail\BaseMailer</code> doesn't provide method to get the HTML output rendered before sending.</p>
  
  <p>How to do this?</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>The only way I can get this is through this hacky way.</p>
  
  <pre><code>    /* @var $message \yii\swiftmailer\Message */
      if ($message instanceof \yii\swiftmailer\Message) {
          $swiftMessage = $message-&gt;getSwiftMessage();
          $r = new \ReflectionObject($swiftMessage);
          $parentClassThatHasBody = $r-&gt;getParentClass()
                  -&gt;getParentClass()
                  -&gt;getParentClass(); //\Swift_Mime_SimpleMimeEntity
          $body = $parentClassThatHasBody-&gt;getProperty('_immediateChildren');
          $body-&gt;setAccessible(true);
          $children = $body-&gt;getValue($swiftMessage);
          foreach ($children as $child) {
              if ($child instanceof \Swift_MimePart &amp;&amp;
                      $child-&gt;getContentType() == 'text/html') {
                  $html = $child-&gt;getBody();
                  break;
              }
          }
          print_r($html);
      }
  </code></pre>