定制 jinexus/zend-notification 二次开发

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

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

jinexus/zend-notification

Composer 安装命令:

composer require jinexus/zend-notification

包简介

JiNexus/Zend-Notification provides a simple, chainable wrapper for creating and sending responsive email templates using some zend components (such as zend-mail and zend-view).

README 文档

README

Build Status Minimum PHP Version Latest Stable Version Total Downloads License Donate

JiNexus/Zend-Notification is a component that extends and utilized the components of Zend-Mail, Zend-View, Zend-Config, Zend-Servicemanager and Zend-Filter to generate and send a well layout emails. This component also uses Cerberus-Responsive as a sample base email template.

Installation

It's recommended that you use Composer to install JiNexus/Zend-Notification.

$ composer require jinexus/zend-notification

This will install JiNexus/Zend-Notification and all required dependencies. JiNexus/Zend-Notification requires PHP 5.6 or latest.

Basic Usage

A basic usage consists of one or more recipients, a subject, a body/content and a sender. To send such a mail using JiNexus/Zend-Notification, do the following:

<?php 
use JiNexus\Zend\Notification\Notification;

$notification = new Notification();
$notification->setFrom('sender@example.com', 'Sender Name')
    ->setTo('recipient@example.com', 'Recipient Name')
    ->setSubject('Sample Subject')
    ->setContent(
        'This is the body/content of the email, you can write here your thoughts.' .
        'I\'ve got everything yet nothing to write, for my thoughts are in a constant fight.'
    )
    ->send();

By default JiNexus/Zend-Notification is using Zend\Mail\Transport\Sendmail to send an email.

Adding Multiple Recipients

Method 1: Method chaining the addTo() method

<?php 
use JiNexus\Zend\Notification\Notification;

$notification = new Notification();
$notification->setFrom('sender@example.com', 'Sender Name')
    ->setTo('recipientOne@example.com', 'Recipient One')
    ->addTo('recipientTwo@example.com', 'Recipient Two')
    ->addTo('recipientThree@example.com', 'Recipient Three')
    ->setSubject('Your character defines you')
    ->setContent(
        'There are two things that defines you. Your patience to learn when you don\'t know anything, 
        and your attitude to share when you know everything.'
    )
    ->send();

Method 2: Passing array to setTo() method

<?php 
use JiNexus\Zend\Notification\Notification;

$notification = new Notification();
$notification->setFrom('sender@example.com', 'Sender Name')
    ->setTo(['recipientOne@example.com', 'recipientTwo@example.com'], 'Common Name')
    ->setSubject('What makes me superior?')
    ->setContent(
        'The fact that I don\'t believe that I\'m better than anyone else 
        gives me an inevitable sense of superiority.'
    )
    ->send();

Method 3: Passing array to addTo() method

<?php 
use JiNexus\Zend\Notification\Notification;

$notification = new Notification();
$notification->setFrom('sender@example.com', 'Sender Name')
    ->addTo(['recipientOne@example.com', 'recipientTwo@example.com'], 'Common Name')
    ->setSubject('I\'m from 90\'s')
    ->setContent(
        'Let me tell you about a magical time. The rock stars were suicidal, 
        the rappers were criminals, and wrestling was real. It was the 90\'s'
    )
    ->send();

Note: The difference between setTo() and addTo() method is that setTo() overwrites the existing data while addTo() appends to the existing data. In short all methods that are prefix with set's and add's behave in the same manner.

You can also add recipients to carbon-copy ("Cc:") or blind carbon-copy ("Bcc:").
<?php 

$notification->setCc('recipientCc@example.com', 'Recipient Cc');
$notification->setBcc('recipientBcc@example.com', 'Recipient Bcc');

Or

<?php 
$notification->addCc('recipientCc@example.com', 'Recipient Cc');
$notification->addBcc('recipientBcc@example.com', 'Recipient Bcc');

Note: setCc(), setBcc, addCc() and addBcc methods also accepts array of recipients and can also be use in method chaining.

If you want to specify an alternate address to which replies may be sent, that can be done, too.
<?php 
$notification->setReplyTo('jimvirle@example.com', 'Jimvirle');

Or

<?php 
$notification->addReplyTo('jimvirle@example.com', 'Jimvirle');
Interestingly, RFC-822 allows for multiple "From:" addresses. When you do this, the first one will be used as the sender, unless you specify a "Sender:" header. The Notification class allows for this by utilizing the Zend-Mail.
<?php
/*
 * Mail headers created:
 * From: Kheven Bitoon <kheven@example.com>, Rogelio Carrillo <rogelio@example.com>
 * Sender: Jimvirle Calago <jimvirle@example.com>
 */
$notification->addFrom('kheven@example.com', 'Kheven Bitoon');
$notification->addFrom('rogelio@example.com', 'Rogelio Carrillo');
$notification->setSender('jimvirle@example.com', 'Jimvirle Calago');
By default JiNexus/Zend-Notification provides an HTML content upon sending the email. And there are instances that you may want to choose a Text content. To do that you have to set the type of your email by:
<?php 
/*
 * This will set your email to a text content.
 */
$notification->setType('text');

Or

<?php 
/*
 * This will set your email to an html content.
 */
$notification->setType('html');

Adding Attachments To Email

JiNexus/Zend-Notification directly provides you the ability to create and use mail attachments.

<?php 
/*
 * You can also set multiple attachments.
 */
$notification->setAttachments([
    'absolute-path-of-the-file.jpg',
    'this-is-a-multiple-attachment.pdf',
]);

Advance Usage

Advance usage allows you to assemble your own layout and set the right template to be your email's content. JiNexus/Zend-Notification uses Cerberus-Responsive as a sample base email template (You can replace it by your own choice, later when we tackle about setConfig() method). Refer to the src/view/layout and src/view/template

To do this all you have is follow the example below:

<?php 
use JiNexus\Zend\Notification\Notification;

$notification = new Notification();
$notification->setFrom('sender@example.com', 'Sender Name')
    ->setTo('recipient@example.com', 'Recipient Name')
    ->setSubject('Sample Subject')
    ->assemble()
    ->send();

By default JiNexus/Zend-Notification will look to the built in configuration under config/notification.global.php to feed the assemble() required configs. To overwrite this default configuration, you have to call the setConfig() method and pass your own array of configs.

To do this all you have is follow the example below:

<?php 
use JiNexus\Zend\Notification\Notification;

$notification = new Notification();
$notification->setFrom('sender@example.com', 'Sender Name')
    ->setTo('recipient@example.com', 'Recipient Name')
    ->setSubject('Sample Subject')
    ->setConfig([
            'notification' => [
                'footer' => __DIR__ . '/src/view/layout/footer.phtml',
                'header' => __DIR__ . '/src/view/layout/header.phtml',
                'layout' => __DIR__ . '/src/view/layout/layout.phtml',
                'template' => __DIR__ . '/src/view/template/confirmation-email.phtml',
            ]
        ])
    ->assemble()
    ->send();

Must: Method chaining on the available methods specifically setConfig(), assemble() and send() has their own precedence to follow, they must be called in the right order:

  • If you want to call setConfig(), it must be called before assemble() method.
  • If you want to call assemble(), it must be called before send() method.
  • And lastly, send() method must be called last.

Note: Array keys are strict and must be followed accordingly, you can replace values as long as it exist.

Passing and Parsing a data to the Header - header.phtml

<?php 
$notification->setHeaderData(['greetings' => 'Hello from the other side!']);

Now you can parse the data to your header.phtml, by:

<?php echo $this->greetings; ?>

Passing and Parsing a data to the Footer - footer.phtml

<?php 
$notification->setFooterData([
    'company' => 'JiNexus Inc.',
    'address' => 'Cebu City, Cebu, 6000, PH',
]);

Now you can parse the data to your footer.phtml, by:

<?php echo $this->company; ?>
<?php echo $this->address; ?>

Passing and Parsing a data to the Template - any-email-template.phtml

<?php 
$notification->setTemplateData([
    'fullName' => 'Jimvirle Calago',
    'message' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.',
    'token' => '$2y$10$2pebvLw6RhmqiybYg6qNr.F.lCIEYf0HzPrKAxNTsrRZLRI5uhh1m'
]);

Now you can parse the data to your any-email-template.phtml, by:

<?php echo $this->fullName; ?>
<?php echo $this->message; ?>
<?php echo $this->token; ?>

Must: setHeaderData(), setFooterData() and setTemplateData() must be called before the assemble() method.

Transports and setting up their own parameters or options.

Transports take care of the actual delivery of mail. Typically, you only need to worry about two possibilities: using PHP's native mail() functionality, which uses system resources to deliver mail, or using the SMTP protocol for delivering mail via a remote server.

You can define your own transport by using setTransport() method.

Available Values:

- sendmail
- smtp
- inMemory

Sendmail

By default if you don't define your transport JiNexus/Zend-Notification will automatically use sendmail. However if by any chance you want to pass a parameter to the sendmail, you can do it by:

<?php 
$notification->setTransport('sendmail');
$notification->setSendmailTransportParameters('-freturn_to_me@example.com');
$notification->send();

Chose your transport wisely

Although the sendmail transport is the transport that requires least configuration, it may not be suitable for your production environment. This is because emails sent using the sendmail transport will be more often delivered to SPAM-boxes. This can partly be remedied by using the SMTP Transport combined with an SMTP server that has an overall good reputation. Additionally, techniques such as SPF and DKIM may be employed to ensure even more email messages are delivered successfully.

SMTP

Below is a sample configuration of SMTP transport:

<?php 
$notification->setTransport('smtp');
$notification->setSmtpTransportOptions([
    'host' => 'smtp.gmail.com',
    'port' => 587,
    'connection_class'  => 'plain',
    'connection_config' => [
        'username' => 'jinexus.zend@gmail.com',
        'password' => 'my-app-password',
        'ssl' => 'tls',
    ],
]);
$notification->send();

InMemory

The InMemory transport is primarily of interest when in development or when testing.

Below is a sample configuration of InMemory transport:

<?php 
$notification->setTransport('inMemory');
$notification->send();

Snapshots

snapshot-01 snapshot-02

To Do's

  • Create a Unit Test
  • Add Get and Set Encoding extensions
  • Add Get and Set Headers extensions
  • Add support for File transport
  • Add getLastMessage() for InMemory transport
  • Improve Documentation (Here's comes the most boring part~) #grumble

Contributing

Before contributing please read the Contributing File for details.

Security

If you discover security related issues, please email jinexus.zend@gmail.com instead of using the issue tracker.

Credits

Dependency

License

The JiNexus/Zend-Notification is an open source project that is licensed under the BSD 3-Clause License. See License File for more information. JiNexus reserves the right to change the license of future releases.

Change log

For the most recent change log, visit the Releases Page or the Changelog File.

Donations

Donations are greatly appreciated!

A man has to code for food. A man must do what he feels needs to be done, even if it is dangerous or undesirable.

paypal

jinexus/zend-notification 适用场景与选型建议

jinexus/zend-notification 是一款 基于 HTML 开发的 Composer 扩展包,目前已累计 13 次下载、GitHub Stars 达 3, 最近一次更新时间为 2017 年 04 月 29 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 jinexus/zend-notification 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 3
  • Watchers: 1
  • Forks: 1
  • 开发语言: HTML

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2017-04-29