Zend Framework 2: Send Html Mail with Simple Template

Yesterday, I spent several hours solving this problem.

In Laravel Framework, you can do as following:

Mail::send("mails.reset", $data, function ($message) use ($email) {
    $message
        ->from("noreply@infinitescript.com", "The Home of Class8")
        ->subject("Reset Your Password");
    $message->to($email);
});

Step 1: Create an Email Template

First of all, you need to create an email template in the views folder, such as /view/mails/reset.phtml.

<table cellspacing="0" cellpadding="0" border="0">
  <tbody>
    <tr>
      <td
        style="
          padding: 15px 15px 0 15px;
          background: #fff;
          border-radius: 4px 4px 0 0;
        "
      >
        <div>
          <img
            src="http://lab.haozhexie.com/CourseOcean/img/logo.png"
            alt="CourseOcean"
            height="85"
            width="290"
          />
        </div>
      </td>
    </tr>
    <tr>
      <td
        style="
          padding: 15px;
          background: #fff;
          border-radius: 0 0 4px 4px;
          font-size: 12px;
        "
      >
        We received a request to reset the password for your account,
        <?=$this->username?>.<br /><br />
        If you made this request, click the link below. If you didn't make this
        request, you can ignore this email.<br /><br />
        <a
          href="http://lab.haozhexie.com/CourseOcean/accounts/resetPassword?email=<?=$this->email?>&amp;keycode=<?=$this->keycode?>"
          target="_blank"
          style="color: #005399; text-decoration: none"
          >http://lab.haozhexie.com/CourseOcean/accounts/resetPassword?email=<?=$this->email?>&amp;keycode=<?=$this->keycode?></a
        ><br /><br />
        Yours, <br />
        CourseOcean.<br /><br />
        <div
          style="
            border-top: 3px solid #eee;
            color: #999;
            font-size: 11px;
            line-height: 1.2;
          "
        >
          <br />Powered by
          <a
            href="http://lab.haozhexie.com/CourseOcean/"
            target="_blank"
            style="color: #005399; text-decoration: none"
            >CourseOcean</a
          >. All rights reserved.<br />
        </div>
      </td>
    </tr>
  </tbody>
</table>

Step 2: Complete Sending Email Function

function sendResetPasswordEmail($username, $email) {
    $keycode = $this->generateRandomString(32);
    $view = new \Zend\View\Renderer\PhpRenderer();
    $resolver = new \Zend\View\Resolver\TemplateMapResolver();
    $resolver->setMap([
        "mailTemplate" => __DIR__ . "/../../../view/mails/reset.phtml",
    ]);
    $view->setResolver($resolver);

    $viewModel = new ViewModel();
    $viewModel->setTemplate("mailTemplate")->setVariables([
        "username" => $username,
        "email" => $email,
        "keycode" => $keycode,
    ]);

    $bodyPart = new \Zend\Mime\Message();
    $bodyMessage = new \Zend\Mime\Part($view->render($viewModel));
    $bodyMessage->type = "text/html";
    $bodyPart->setParts([$bodyMessage]);

    $message = new \Zend\Mail\Message();
    $message
        ->addFrom("noreply@infinitescript.com", "CourseOcean")
        ->addTo($email)
        ->setSubject("Reset Your Password")
        ->setBody($bodyPart)
        ->setEncoding("UTF-8");
    $transport = new \Zend\Mail\Transport\Sendmail();
    $transport->send($message);
}
Contact Us
  • Tencent AI Lab, Shenzhen, China
  • cshzxie [at] gmail [dot] com