mechanik / zf3mail
Zend Framework 3 SMTP 邮件模块
1.0.0
2019-11-09 11:11 UTC
Requires
- php: ^7.0
- zendframework/zend-mvc: ^3.1
Conflicts
- zendframework/zendframework: <3.0.0
This package is auto-updated.
Last update: 2024-09-12 19:15:28 UTC
README
描述
电子邮件模块,使发送HTML电子邮件的开发更快、更简单。
当前功能
- 使用 "Zend 3" 方法的 Service (e-mail SMTP 传输)
- 能够使用来自
phtml
文件的自定义模板 - 能够使用来自
phtml
文件的自定义布局,每个电子邮件一个,自定义布局 - 能够添加自定义标题
- 适用于所有控制器的插件
- 适用于视图模板的插件(如有需要)
- PHP 7 兼容(且必需)
- 没有其他依赖(除了 Zend Framework 3)
安装
通过 Composer 进行安装
composer require mecanik/zf3mail
SMTP 配置
创建 config/autoload/zf3mail.global.php,内容如下
<?php return [ 'zf3mail_config' => [ 'smtp_login' => [ 'name' => 'smtp.hostname.net', 'host' => 'smtp.hostname.net', 'connection_class' => 'login', 'connection_config' => [ 'username' => '', 'password' => '', 'ssl' => 'tls', ], ], // By default, the Message class assumes ASCII encoding for your email. I've set UTF-8, but change as neccessary. 'encoding' => 'UTF-8', // Default layout used for all HTML emails. This can be replaced per email basis by specifying 'layout' array parameter when composing emails. 'default_layout' => 'application/emails/default_layout.phtml', ], ];
模块配置/使用
加载模块(任意顺序)
'Mecanik\ZF3Mail'
使用此模块的方式有三种
- 将服务注入到任何控制器中(通过工厂)
$mailService = $container->get(\Mecanik\ZF3Mail\Service\ZF3Mail::class);
- 仅作为插件在控制器中使用
$this->zf3mail()
- 仅作为插件在视图中使用
$this->zf3mail()
然而,由于模块已自动注册为插件,我建议在您想要的任何控制器中使用插件。
编写电子邮件
编写电子邮件时,您必须指定一些内容,如 'from'、'to'、'subject'、模板等,所有这些操作都是通过最简单的方式:数组完成的。
您还可以添加额外的标题,并在必要时覆盖每个电子邮件的“默认”布局。
<?php $headers = [ 'from' => 'My Name <noreply@mywebsite.com>', 'subject' => 'Hello there', 'to' => 'Neo <neo@matrix.com>', // Additional headers can be specified like: // 'X-API-Key', 'FOO-BAR-BAZ-BAT' ]; $template = [ 'content_template' => 'application/emails/order-received.phtml', //'layout' => 'application/emails/layout.phtml', ]; $email = $this->zf3mail()->compose($headers, $template, $parameters);
编写完成后,只需简单地发送即可
$this->zf3mail()->send($email);
布局说明
如您在配置中注意到的,所有发送的HTML电子邮件都将使用一个“默认”布局
'default_layout' => 'application/emails/default_layout.phtml',
对于您发送的每个电子邮件,都可以像上面那样覆盖它,但它必须包含输出内容的变量
<?php echo $this->email_content ?>
现在就到这里,祝您享受!