kachar / zend-mjml
MJML 客户端
1.0.0
2016-10-11 17:18 UTC
Requires
- php: >=5.4
- guzzlehttp/cache-subscriber: *
- guzzlehttp/guzzle: ~5
- zendframework/zendframework: 2.*
This package is not auto-updated.
Last update: 2024-09-28 20:19:58 UTC
README
本模块基于:MJML - 唯一开源的响应式电子邮件框架,让响应式电子邮件变得简单 https://mjml.io
使用本模块,您可以定义电子邮件模板为 MJML 文件,并使用 Zend ViewModel
进行渲染。MJML 标记将被发送到 https://mjml.io/try-it-live 以转换为 HTML。
该软件包支持
- 在转换后的标记中进行简单的变量替换。(
*.mjml
文件) - 使用 mjml 标记内的 PHP 的复杂模板系统。(
*.pmjml
文件)
您可以使用预定义的电子邮件传输方法发送完整的电子邮件。
要求
- PHP 版本 >= 5.4
- Zend Framework 2 (2.*)
- Guzzle Http 客户端 (~5)
- Guzzle 缓存订阅者 (*)
安装
-
将
"kachar/zend-mjml": "dev-master"
添加到您的composer.json
文件中,并运行php composer.phar update
。"require": { "kachar/zend-mjml": "dev-master" }
$ php composer.phar update
-
将
ZendMjml
添加到您的config/application.config.php
文件中的modules
键下。'modules' => [ 'ZendMjml', ],
配置
在配置中,您可以在 mjml
键下设置以下选项
mjmlServiceUrl
: 设置 MJML 端点(默认 https://mjml.io
)
timeout
: 描述请求超时的浮点数,以秒为单位。使用 0 以无限期等待(默认值 10
)。
connectTimeout
: 描述尝试连接到服务器时等待的秒数的浮点数。使用 0 以无限期等待(默认值 1.5
)。
transportAdapter
: 将用于发送实际电子邮件的适配器。两种可能的配置
- 作为字符串 - 实现
Zend\Mail\Transport\TransportInterface
的任何服务,并且可以从 ServiceLocator 中获取。
'mjml' => [ 'transportAdapter' => [ 'type' => 'sendmail', 'options' => [ // see http://www.sendmail.org/~ca/email/man/sendmail.html ], ], ],
- 作为工厂数组 - 基于
\Zend\Mail\Transport\Factory
的配置选项。有关高级工厂选项,请参阅(官方文档)[http://framework.zend.com/manual/current/en/modules/zend.mail.transport.html#zend-mail-transport]。
'mjml' => [ 'transportAdapter' => 'Zend\Mail\Transport\Sendmail', ], 'service_manager' => [ 'invokables' => [ 'Zend\Mail\Transport\Sendmail' => 'Zend\Mail\Transport\Sendmail', ], ],
使用方法
内联 MJML 标记
$service = $this->getServiceLocator()->get('Service\Mjml'); $mjml = ' <mj-body> <mj-section> <mj-column> <mj-image width="100" src="https://mjml.io/assets/img/logo-small.png"></mj-image> <mj-divider border-color="#F45E43"></mj-divider> <mj-text font-size="20px" color="#F45E43" font-family="helvetica">Hello {{ name }}</mj-text> </mj-column> </mj-section> </mj-body> '; // This is the final html to be sent to the recipients $body = $service->renderMjml($mjml); // You can replace simple variables inside the final output $body = $service->renderMjml($mjml, [ 'name' => 'direct example', ]); // Sending the email $email = $service->composeEmail($body); $email->setTo('email@example.com'); $email->setSubject('Sample responsive MJML email'); $service->sendEmail($email); // or echo $body;
使用 ViewModel 加载 MJML 标记文件
$service = $this->getServiceLocator()->get('Service\Mjml'); $view = new ViewModel(); $view->name = 'John Doe'; $view->setTemplate('mjml/plain.mjml'); // This is the final html to be sent to the recipients $body = $service->renderView($view); // Sending the email $email = $service->composeEmail($body); $email->setTo('email@example.com'); $email->setSubject('Sample responsive MJML email using ViewModel'); $service->sendEmail($email);
使用 ViewModel 加载 PHP-MJML 标记文件
$view = new ViewModel(); // This will be replaced using str_replace in the final template $view->name = 'PHP MJML Template'; // This will be replaced using php in the initial mjml template $view->products = [ [ 'name' => 'Ham', 'price' => '1.50', 'quantity' => 5, ], [ 'name' => 'Cheese', 'price' => '3.75', 'quantity' => 3, ], [ 'name' => 'Bread', 'price' => '5.00', 'quantity' => 10, ], ]; $view->setTemplate('mjml/php-mjml.pmjml'); // This is the final html to be sent to the recipients $body = $service->renderView($view); // Sending the email $email = $service->composeEmail($body); $email->setTo('email@example.com'); $email->setSubject('Sample responsive MJML email using PhpRenderer'); $service->sendEmail($email);