定制 everlutionsk/email-bundle-2 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

everlutionsk/email-bundle-2

Composer 安装命令:

composer require everlutionsk/email-bundle-2

包简介

Everlution email bundle for Symfony framework

README 文档

README

This Symfony bundle provides mechanism for sending and receiving email messages through various mail systems.

Installation

composer require everlutionsk/email-bundle-2

Enable the bundle

// app/AppKernel.php
public function registerBundles()
{
    return array(
        // ...
        new Everlution\EmailBundle\EverlutionEmailBundle()
    );
}

Configure the bundle

Following configuration snippet describes how to configure the bundle. Configuration requires a names of services, which implements corresponding interfaces. Only exception is doman_name, where you should set something like appdomain.com

# app/config/config.yml

# Doctrine Configuration
doctrine:
    orm:
        entity_managers:
            default:
                mappings:
                    EverlutionEmailBundle: ~

# EmailBundle Configuration
everlution_email:
    domain_name: APP_DOMAIN
    enforced_delivery_address: EMAIL_ADDRESS|NULL
    mail_system: Implementation of Outbound\MailSystem\MailSystem
    async_stream: Implementation of Support\Stream\Stream
    attachment_swappers:
        inbound: Implementation of Inbound\Attachment\AttachmentSwapper
        outbound: Implementation of Outbound\Attachment\AttachmentSwapper
    request_processors:
        inbound: Implementation of Inbound\RequestProcessor
        outbound_message_event: Implementation of Outbound\MessageEvent\RequestProcessor

mail_system - Name of service, which will be used for sending email messages. This service usually comunicate with SMTP server or with some transactional email platform like Mandrill.

enforced_delivery_address - [Optional] Email address, which will be used to override recipient address in every outbound message.

async_stream - Bundle allows to send email messages asynchronously. Email messages is stored in memory unil some value is added into this Stream. Good example is a Stream of Symfony's kernel.terminate events.

attachment_swappers - After sending or receiving a message, bundle try to save the message's attachments by using this attachment swappers. This swappers can save attachments in various ways.

request_processors - Bundle provide common mechanism to handle inbound messages and outbound message events. This events may occur for example when external mail system try to send scheduled messages. However, different mail systems sending data in different format. Request processors transform this data into format, which is known for this bundle.

Routing

Bundle provides controllers for handling inbound messages and outbound message events.

# Handle outbound message events.
everlution.email.outbound_message_event:
    pattern: CUSTOM_PATTERN
    defaults: { _controller: everlution.email.outbound.message_event.controller:handleMessageEvent }
    methods: POST

# Handle inbound messages.
everlution.email.inbound:
    pattern: CUSTOM_PATTERN
    defaults: { _controller: everlution.email.inbound.controller:handleInbound }
    methods: POST

Basic Usage

Create new outbound message

$message = new OutboundMessage();
$message->setSubject('Subject');
$message->setText('Message text.');             // Text for basic insight in email client.
$message->setHtml('<img src="cid:logo">');      // Email body.
$message->setFromEmail('support@example.com');
$message->setFromName('Sender name');
$message->setReplyTo('reply@example.com');
$message->setRecipients([
    new ToRecipient('recipient@mail.com', 'Recipient name'),
    new CcRecipient('cc-recipient@mail.com', 'Cc recipient name'),
    new BccRecipient('bcc-recipient@mail.com', 'Bcc recipient name'),
]);

$image = new BasicAttachment('image/png', 'logo', file_get_contents('logo.png'));
$attachment = new BasicAttachment('application/pdf', 'document.pdf', file_get_contents('document.pdf'));

$message->setImages([$image]);            // Images included into email body. 
$message->setAttachments([$attachment]);

Get mailer

Synchronous mailer - Mail system is called immediately.

$mailer = $this->get('everlution.email.outbound.synchronous_mailer');

Asynchronous mailer - Mail system is called after adding value into corresponding Stream.

$mailer = $this->get('everlution.email.outbound.asynchronous_mailer');

Send / Schedule message

$mailer->sendMessage($message);
$mailer->scheduleMessage($message, new \DateTime('+ 30 minutes'));

Advanced usage

Message transformers

Every message could be modified before it is forwarded into mail system.

Transforming outbound messages
Register service, which implements OutboundMessageTransformer and add following tag:

everlution.email.outbound.message_transformer

Transforming inbound messages
Register service, which implements InboundMessageTransformer and add following tag:

everlution.email.inbound.message_transformer

Message templates

Some mail systems supports message templates, which are defined in this systems. The following code shows how to use this templates.

$parameter = new Parameter('PARAMETER_NAME', 'PARAMETER_VALUE');
$template = new Template('TEMPLATE_NAME', [$parameter]);

$message->setTemplate($template);

Handle Inbound messages

Inbound messages can be handled by listeners, which listening to everlution.email.inbound events. Events are instances of InboundEvent and contains information about Inbound message and about its storable version, which has been saved into database.


Caution: If application doesn't need to create associations with inbound message in database, then storable version of message should be ignored.

Mock a mail system

Use everlution.email.mock.mail_system service as mail system (see Configure the bundle section).

Supported mail systems

Mandrill

everlutionsk/email-bundle-2 适用场景与选型建议

everlutionsk/email-bundle-2 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 12.08k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2015 年 09 月 21 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「symfony」 「email」 「bundle」 「everlution」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

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

围绕 everlutionsk/email-bundle-2 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 12.08k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 11
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 8
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-09-21