enlitepro/enlite-mail

简单邮件发送模板服务

v1.2.1 2013-11-27 09:04 UTC

This package is not auto-updated.

Last update: 2024-09-23 14:21:34 UTC


README

为ZF2提供的简单邮件发送模板模块。

安装

推荐通过composer进行安装。

{
    "require": {
        "enlitepro/enlite-mail": "~1.2.0"
    }
}

config/application.config.php 中的 modules 添加 EnliteMail

配置

默认使用的模块

  • 使用 Zend\Mail\Transport\Sendmail 作为传输
  • 使用默认渲染器

要在服务定位器中使用其他写入,请将其添加到配置中

array(
    'enlite_mail' => array(
        'renderer' => 'YOUR_RENDERER_FOR_MAIL', // default ViewRenderer
        'transport' => 'YOUR_TRANSPORT_FOR_MAIL', // default MailTransport
        'from_mail' => 'YOUR_MAIL',
        'from_name' => 'YOUR_NAME',
    )
)

例如

array(
    'enlite_mail' => array(
        'renderer' => 'ZfcTwigRenderer',
        'transport' => 'MailTransport',
    )
)

要更改传输,您可以在服务管理器的 "MailTransport" 键中设置新的传输。例如

array(
    'service_manager' => array(
        'invokables' => array(
            'MailTransport' => 'Zend\Mail\Transport\Sendmail',
        ),
    )
);

使用方法

use EnliteMail\Service;
use Zend\ServiceManager\ServiceLocatorAwareInterface;
use Zend\ServiceManager\ServiceLocatorAwareTrait;

class MyService implements ServiceLocatorAwareInterface
{
    use ServiceLocatorAwareTrait,
        MailServiceTrait;

    public function test()
    {
        $mailService = $this->getMailService();

        // send any mail
        $message = $mailService->factoryMessage(); // this is Zend\Mail\Message
        // configure message
        // ...
        // send
        $mailService->sendMessage($message);

        // create mail from template and variables
        // variable will be pass to template
        $template = $mailService->createTemplate('my-module\controller\view', ['foo' => 'bar']);
        $mailService->sendTemplate($template, 'qwerty@example.com');
        // or, if you want operate message object before send
        $message = $mailService->createMessageFromTemplate($template);
        $mailService->sendMessage($message);
    }
}

模板

基于您的渲染器的模板系统,具有所有优点。只有一个新功能,headTitle 插件设置消息主题,而不仅是页面标题。

layout/mail.twig 的示例

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    {% block title %}
        {% do headTitle('My Application').setSeparator(' - ') %}
    {% endblock %}
</head>
<body marginheight="0" topmargin="0" marginwidth="0" leftmargin="0" style="">
<div class="padded" style="padding:20px 20px 20px 20px">
    <table style="">
        <tr>
            <td style="">
{% block content %}{{ content|raw }}{% endblock content %}
            </td>
        </tr>
    </table>
</div>
</body>
</html>

application/mail/invite.twig 的示例

{% extends "layout/mail" %}

{% block content %}
    {% do headTitle("You're invited to MyApplication") %}
    
    Some variable: {{some_variable}}
{% endblock %}