承接 lm-commons/lmc-mail 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

lm-commons/lmc-mail

Composer 安装命令:

composer require lm-commons/lmc-mail

包简介

Wrapper for Laminas Mail that uses View Model to compose the email body

README 文档

README

Continuous Integration Latest Stable Version Total Downloads License PHP Version Require

LmcMail

An email service module that provides the ability to use the View renderer of a Laminas MVC application and the installed View Helper plugins to render HTML emails.

Requirements

  • PHP 8.1 or higher
  • Laminas MVC

Installation

Install the module:

$ composer require lm-commons/lmc-mail

Composer will inject the module into the modules configuration, or you can add it manually to the modules.config.php or application.config.php.

Customize the module by copying and renaming the sample configuration file lm-commons/lmc-mail/config/lmcmail.local.php.dist to the application's config/autoload.

Configuration

LmcMail supports with the Laminas SMTP Mail Transport or the Laminas File Mail Transport and this is configured by the transport config key in the lmcmail.local.php file:

<?php
return [
    'lmc_mail' => [
        'from' => [
            'email' => 'user@example.com',
            'name' => 'User',
        ],

        // For SMTP
        'transport' => [
            'type' => 'smtp',
            'options' => [
                'host' => 'example.com',
                'connection_class' => 'plain',
                'connection_config' => [
                    'ssl' => 'tls',
                    'username' => 'user@example.com',
                    'password' => 'somepassword',
                ],
                'port' => 587,
            ],
        ]    
         // OR
            
        'transport' => [
            'type' => 'file',
            'options' => [
                'path' => '/path/to/email/folder',
            ],
        ],
    ],
];

In a development environment, it is typical to use a File Mail Transport. In a production environment, an SMTP Mail Transport will more likely be used.

The 'transport' configuration must comply with the Laminas\Mail\Transport\Factory\Factory::create method.

The 'from' configuration defines a default from address. The from address can also be specified at message creation.

Usage

The Mail service can be retrieved from the service manager:

$messageService = $serviceManager->get(LmcMail\Service\MessageService::class);

Basic example to send an HTML email:

$viewModel = new \Laminas\View\Model\ViewModel();
$viewModel->setTemplate('mail/html');
$message = $messageService->createHtmlMessage(
    ['email' => 'john@example.com', 'name' => 'John'], //from 
    ['email' => 'jane@example.com', 'name' => 'Jane'] //to
    "This is the subject line", //subject
    $viewModel); // View model

$messageService->send($message);

The 'mail/html' template must exist in the application's view template map. The HTML mail renderer will use a layout template aliased as 'mail/layout' in the view template map. This is defined in the module.config.php file.

Available methods

createHtmlMessage

 /**
  * Create an HTML message
  * @param string|Address|AddressInterface|array|AddressList|Traversable $from
  * @param string|Address|AddressInterface|array|AddressList|Traversable $to
  * @param string $subject
  * @param string|ModelInterface $nameOrModel
  * @return Message
  */
createHtmlMessage(string|Address|AddressInterface|array|AddressList|Traversable $from, 
                  string|Address|AddressInterface|array|AddressList|Traversable $to, 
                  string $subject, 
                  string|ModelInterface $nameOrModel): \Laminas\Mime\Message::class

If $nameorModel is a string, it must correspond to the view template to use.

createTextMessage

/**
 * Create a text message
 * @param string|Address|AddressInterface|array|AddressList|Traversable $from
 * @param string|Address|AddressInterface|array|AddressList|Traversable $to
 * @param string $subject
 * @param string|ModelInterface $nameOrModel
 * @return Message
 */
createTextMessage(string|Address|AddressInterface|array|AddressList|Traversable $from, 
                  string|Address|AddressInterface|array|AddressList|Traversable $to, 
                  string $subject, 
                  ModelInterface $nameOrModel): \Laminas\Mail\Message::class

If $nameorModel is a string, it must correspond to the view template to use.

send

/**
 * Send the message
 * @param Message $message
 */
send(Message $message): void

where $message can be any object of type \Laminas\Mail\Message not necessarily one created by the above methods.

Advanced Customizations

LmcMail can be customized to the applications needs.

Using view templates

LmcMail uses nested view models to render the body of HTML messages.

In a similar fashion to the view model structure of the Laminas MVC Skeleton, the body is rendered using a layout view model to which the view model parameter ($nameOrModel) to the createHtmlMessage method is added a child. The rendered output of the $nameOrModel view model is captured in the variable message which is passed to the layout view model.

A default template mail/layout is supplied is view/layout/layout.phtml. This template can be the starting point for your own layout template. The layout template can be set using the setLayoutTemplate() method. Alternatively, the mail/layout entry in the View Manager template map can be overridden to point to your template. Another alternative is to use a factory delegator to the MessageServiceFactory::class to set the layout template after the Message Service is created.

View Helpers can be used when rendering view models. A common use case is to use $this->url() to render a link to your application.

Use alternate View Resolved and View Helper Manager

LmcMail uses Service Manager aliases to get the View Resolver and View Helper Manager which resolves to the Laminas MVC resolver and manager. This allows to use any view template and helpers already defined in the application.

'aliases' => [
    // These aliases are used by the MailViewRendererFactory
    // by default, they resolve to the Laminas MVC View Helper manager and Resolver
    'lmc_mail_view_helper_manager' => 'ViewHelperManager',
    'lmc_mail_view_resolver' => 'ViewResolver',
],

If you want to use a different resolver and view helper manager, then update the aliases to point to your classes:

'aliases' => [
    'lmc_mail_view_helper_manager' => 'MyHelperManager',
    'lmc_mail_view_resolver' => 'MyViewResolver',
],

If you want to use your own renderer, then you can override the Service Manager factory:

'factories' => [
    // Override the factory with your own
    'lmc_mail_view_renderer' => MyViewRendererFactory::class,
    /* ... */
],

Event Listening

MessageService::send() triggers two events:

  • MessageEvent::SEND is triggered right before the message is sent by the transport service.
  • MessageEvent::SEND_POST is triggered right after the message has been sent by the transport service.

The listener to these events will receive an event of classMessageEvent that extends the Event class with:

  • A $message property containing the message. The message is also stored in an event parameter named 'message'.
  • A getMessage() method to get the $message property.
  • A setMessage(Message $message) method to set the $message property and the corresponding event parameters.

The MessageService::send() method, after triggering the MessageEvent::SEND event, will retrieve the message from the event and pass it to the transport service. This allows for the listener to modify the message if needed.

A typical use case for listening to the send events would be to log that a message was sent.

lm-commons/lmc-mail 适用场景与选型建议

lm-commons/lmc-mail 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.03k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2023 年 02 月 23 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 lm-commons/lmc-mail 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 lm-commons/lmc-mail 我们能提供哪些服务?
定制开发 / 二次开发

基于 lm-commons/lmc-mail 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 1.03k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 15
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 2
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2023-02-23