定制 maba/mail-renderer-bundle 二次开发

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

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

maba/mail-renderer-bundle

Composer 安装命令:

composer require maba/mail-renderer-bundle

包简介

Symfony bundle for easier email sending

README 文档

README

Symfony bundle that collects some libraries together to make sending emails easier:

  1. Renders twig template to get subject and content of an email
  2. Converts HTML content to plain text if it's not available in template
  3. Sets default From email and name to message
  4. Gets CSS for mail messages using Assetic, if configured
  5. Inlines CSS inside HTML style attributes and/or adds <styles> tag
  6. Sends message using swift mailer

Installation

Add via composer:

composer require maba/mail-renderer-bundle jimbojsb/emogrify:@dev

Register bundle:

new Maba\Bundle\MailRendererBundle\MabaMailRendererBundle(),

Configure in config.yml:

maba_mail_renderer:
    css_assets:
        inline: mail
        imports: mail_imports
    from:
        email: info@example.com
        name: My Awesome Company
        
assetic:
    assets:
        mail:
            inputs:
                - @bootstrap_css
                - @ApplicationBundle/Resources/less/styles.less
        mail_imports:
            inputs:
                - %kernel.root_dir%/Resources/public/css/mail-imports.css

css_assets node is optional, as is usage of Assetic.

Usage

Code:

/** @var FormattedMessageMailer $mailer */
$mailer = $this->get('maba_mail_renderer.formatted_message_mailer');

$mailer->sendMessage('customer@example.com', 'ApplicationBundle:Email:welcome.html.twig', array(
    'username' => 'customer123',
    'activationLink' => 'http://www.example.com/activate/ABC123',
));

Twig template:

{# ApplicationBundle:Email:welcome.html.twig #}

{% block subject %}Hello {{ username }}, welcome to example.com!{% endblock %}

{% block body_html %}
    <div class="header">Hello {{ username }},</div>
    
    <div class="content">
        You've registered on example.com. Here you can make lots of stuff:
        <ul>
            <li>Stuff</li>
            <li>More stuff</li>
        </ul>
    </div>
    
    <div class="important">
        To activate your account <a class="btn btn-primary" href="{{ activationLink }}">click here</a>
    </div>
    
    <div class="footer">See ya!</div>
{% endblock %}

That's it!

All CSS rules assigned to classes will be inlined into HTML as style attributes.

Extended usage

Templates

Only these blocks are parsed from template: subject, body_html and body_text. subject and at least one of other two are required. Any other blocks or content in template is ignored.

If body_text is provided, it will be used as plain text version of mailer message.

Additionally toEmail variable is passed to the twig template, which equals to the given recipient.

I'd suggest to use single layout twig file, if there's defined structure in your mails. For example:

{# :Email:layout.html.twig #}

{% block body_html %}
    <div class="header">Good afternoon,</div>
    
    {% block content %}{% endblock %}
    
    <div class="footer">See ya!</div>
    
    <a href="{{ get_unsubscribe_link(toEmail) }}">Unsubscribe</a>
{% endblock %}
{# ApplicationBundle:Email:welcome.html.twig #}
{% extends ':Email:layout.html.twig' %}

{% block subject %}...{% endblock %}
{% block content %}
    ...
{% endblock %}

Styles

inline and imports values in css_assets configuration node take Assetic asset name, which you should configure using Assetic configuration.

inline is used when applying CSS rules to message HTML result - style attributes are added to HTML nodes, which would trigger appropriate CSS rules. This is needed as some email readers support neither <styles> nor <link> (for example GMail).

imports is used to make <styles> tag with CSS contents in the mail message itself. This should be as short as possible - usually only for rules that cannot be inlined, like this one:

@import url("//fonts.googleapis.com/css?family=Ubuntu:400,400italic,700,700italic&subset=latin,latin-ext");

That's why it's called imports in configuration.

Why <styles> instead of <link>? To avoid remote resource loading, which is also most commonly disabled by default.

Changing created message

Use createMessage instead of sendMessage with same parameters in renderer, change or add any required fields and send with swift mailer manually.

maba/mail-renderer-bundle 适用场景与选型建议

maba/mail-renderer-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 94 次下载、GitHub Stars 达 0, 最近一次更新时间为 2015 年 06 月 23 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 maba/mail-renderer-bundle 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 94
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 21
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-06-23