email-zf2/emailzf2

EmailZF2 是一个 ZF2 模块,用于管理使用模板发送电子邮件的过程。

安装次数: 2,451

依赖项: 0

建议者: 0

安全: 0

星标: 4

关注者: 3

分支: 4

开放问题: 2

类型:zf2-module

dev-master 2013-12-26 11:24 UTC

This package is not auto-updated.

Last update: 2024-09-23 14:52:23 UTC


README

Total Downloads EmailZF2

版本 1.1

模块,允许在 Zend Framework 2 中使用或不用模板发送电子邮件。

安装

使用 composer composer 进行安装。

php composer.phar require email-zf2/emailzf2:dev-master

如果你已经安装了 composer

composer require email-zf2/emailzf2:dev-master

安装后

使用 composer 安装成功后,转到配置。

  • config/application.config.php 下的 modules 数组中添加模块,插入 EmailZF2
  • config/autoload/ 下创建一个名为 email.local.php 的文件。
  • 将以下行添加到刚才创建的文件中
<?php
return array(
    'email' => array(
        'credentials' => array(
            'from_mail' => 'support@domain.com',
            'from_name' => 'yourname',
        ),
        //use smtp or sendmail
        'transport' => 'sendmail',
        'smtp' => array(
            'host' => 'smtp.domain.com',
            'port' => 587,
            'connection_class' => 'login',
            'connection_config' => array(
                'ssl'      => 'tls',
                'username' => 'youremail',
                'password' => 'yourpassword'
            ),
        ),
    ),
);

这样我们就创建了配置文件。在这个文件中,我们可以选择是否使用已安装在服务器上的 sendmail 发送电子邮件或使用 smtp 发送电子邮件。如果你想保留 sendmail 配置文件的原样。

使用方法

只需在任何文件夹中创建任何模块以保存用于电子邮件的模板。按照惯例,我们将文件夹放在应用程序模块中。

  • Application/view/ 下创建一个名为 emails 的文件夹。
  • 创建一个名为 hello_world.phtml 的模板。
  • 在任何控制器中创建测试消息,在此例中,使用模块 Application 下的 IndexController 进行测试。

控制器

  • 普通版本
$view = new ViewModel(array(
    			'fullname' => 'Vincenzo Provenza',
            ));
$view->setTerminal(true);
$view->setTemplate('Application/view/emails/hello_world');
$this->mailerZF2()->send(array(
	'to' => 'email@domain.it',
	'subject' => 'This is subject'
), $view);
  • 带有 Cc 版本
$view = new ViewModel(array(
        		'fullname' => 'Vincenzo Provenza',
            ));
$view->setTerminal(true);
$view->setTemplate('Application/view/emails/hello_world');
$this->mailerZF2()->send(array(
	'to' => 'email@domain.com',
    	'cc' => 'email2@domain.com',
	'subject' => 'This is subject'
), $view);
  • 带有 Cc & Bcc 版本
$view = new ViewModel(array(
            	'fullname' => 'Vincenzo Provenza',
            ));
$view->setTerminal(true);
$view->setTemplate('Application/view/emails/hello_world');
$this->mailerZF2()->send(array(
	'to' => 'email@domain.com',
    'cc' => 'email2@domain.com',
    'bcc' => 'email3@domain.com',
	'subject' => 'This is subject'
), $view);
  • 带有 addReplyTo 版本
$view = new ViewModel(array(
                'fullname' => 'Vincenzo Provenza',
            ));
$view->setTerminal(true);
$view->setTemplate('Application/view/emails/hello_world');
$this->mailerZF2()->send(array(
    'to' => 'email@domain.com',
    'replyTo' => 'mariorossi@domain.com',
    'replyNameTo' => 'Mario Rossi',
    'subject' => 'This is subject'
), $view);

模板

<h4>Hi, <?= $fullname; ?></h4>