jinexus/zend-notification

JiNexus/Zend-Notification提供了一个简单、可链式的包装器,用于使用一些zend组件(如zend-mail和zend-view)创建和发送响应式电子邮件模板。

1.0.1 2017-05-01 11:16 UTC

This package is auto-updated.

Last update: 2024-09-11 02:01:16 UTC


README

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

JiNexus/Zend-Notification是一个组件,它扩展并利用了Zend-MailZend-ViewZend-ConfigZend-ServicemanagerZend-Filter的组件,以生成和发送布局合理的电子邮件。此组件还使用Cerberus-Responsive作为示例基本电子邮件模板。

安装

建议您使用Composer安装JiNexus/Zend-Notification

$ composer require jinexus/zend-notification

这将安装JiNexus/Zend-Notification和所有必需的依赖项。JiNexus/Zend-Notification需要PHP 5.6或更高版本。

基本用法

基本用法包括一个或多个收件人、主题、正文/内容和一个发件人。要使用JiNexus/Zend-Notification发送此类邮件,请执行以下操作

<?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();

默认情况下,JiNexus/Zend-Notification使用Zend\Mail\Transport\Sendmail来发送电子邮件。

添加多个收件人

方法1:链式调用addTo()方法

<?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();

方法2:将数组传递给setTo()方法

<?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();

方法3:将数组传递给addTo()方法

<?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();

注意:setTo()addTo()方法的区别在于setTo()会覆盖现有数据,而addTo()会追加到现有数据。简而言之,所有以set和add为前缀的方法行为相同。

您还可以向抄送("Cc:")或暗抄送("Bcc:")添加收件人。
<?php 

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

或者

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

注意:setCc()setBccaddCc()addBcc方法也接受收件人数组,并且也可以用于链式调用。

如果您想指定一个用于回复的备用地址,也可以这样做。
<?php 
$notification->setReplyTo('jimvirle@example.com', 'Jimvirle');

或者

<?php 
$notification->addReplyTo('jimvirle@example.com', 'Jimvirle');
有趣的是,RFC-822允许存在多个"From:"地址。当您这样做时,第一个将被用作发件人,除非您指定"Sender:"头。通知类通过使用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');
默认情况下,JiNexus/Zend-Notification在发送电子邮件时提供HTML内容。但是,有时您可能想选择文本内容。为此,您必须通过以下方式设置您电子邮件的类型
<?php 
/*
 * This will set your email to a text content.
 */
$notification->setType('text');

或者

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

向电子邮件添加附件

JiNexus/Zend-Notification直接提供您创建和使用邮件附件的能力。

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

高级用法

高级用法允许您组装自己的布局并设置正确的模板作为您的电子邮件内容。JiNexus/Zend-Notification使用Cerberus-Responsive作为示例基本电子邮件模板(您可以在稍后替换它,当我们介绍setConfig()方法时)。请参阅src/view/layoutsrc/view/template

为此,请按照以下示例操作

<?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();

默认情况下,JiNexus/Zend-Notification 将查找内置配置文件 config/notification.global.php 以提供 assemble() 所需的配置。要覆盖此默认配置,您必须调用 setConfig() 方法并传递自己的配置数组。

为此,请按照以下示例操作

<?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();

必须:在可用的方法上使用方法链,特别是 setConfig()assemble()send(),它们有自己的优先级,必须按正确顺序调用。

  • 如果您想调用 setConfig(),它必须在 assemble() 方法之前调用。
  • 如果您想调用 assemble(),它必须在 send() 方法之前调用。
  • 最后,必须调用 send() 方法。

注意:数组键是严格的,必须相应地遵循,只要存在,您可以替换值。

将数据传递和解析到头部 - header.phtml

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

现在您可以通过以下方式解析数据到您的 header.phtml

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

将数据传递和解析到尾部 - footer.phtml

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

现在您可以通过以下方式解析数据到您的 footer.phtml

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

将数据传递和解析到模板 - 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'
]);

现在您可以通过以下方式解析数据到您的 any-email-template.phtml

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

必须:在调用 assemble() 方法之前,必须调用 setHeaderData()setFooterData()setTemplateData()

传输及其参数或选项的设置。

传输负责邮件的实际投递。通常,您只需担心两种可能性:使用 PHP 的本地 mail() 功能,该功能使用系统资源来投递邮件,或使用 SMTP 协议通过远程服务器投递邮件。

您可以使用 setTransport() 方法定义自己的传输。

可用值

- sendmail
- smtp
- inMemory

Sendmail

默认情况下,如果您没有定义传输,JiNexus/Zend-Notification 将自动使用 sendmail。但是,如果您需要向 sendmail 传递参数,可以通过以下方式执行:

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

明智地选择传输

尽管 sendmail 传输是配置最少的传输,但它可能不适合您的生产环境。这是因为使用 sendmail 传输发送的邮件更有可能被投递到垃圾邮件箱。这可以通过使用具有良好声誉的 SMTP 服务器结合 SMTP 传输来部分解决。此外,可以使用 SPF 和 DKIM 等技术来确保更多的电子邮件消息成功投递。

SMTP

以下是 SMTP 传输的示例配置

<?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

InMemory 传输主要用于开发或测试时。

以下是 InMemory 传输的示例配置

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

快照

snapshot-01 snapshot-02

待办事项

  • 创建单元测试
  • 添加获取和设置编码扩展
  • 添加获取和设置标头扩展
  • 添加对文件传输的支持
  • 为 InMemory 传输添加 getLastMessage()
  • 改进文档(这里是最无聊的部分~)#grumble

贡献

在贡献之前,请阅读有关贡献文件的详细信息:贡献文件

安全

如果您发现安全问题,请通过电子邮件 jinexus.zend@gmail.com 而不是使用问题跟踪器。

致谢

依赖项

许可证

JiNexus/Zend-Notification 是一个开源项目,采用 BSD 3-Clause 许可协议 许可。更多信息请参阅 许可文件。JiNexus 保留修改未来版本许可的权利。

变更日志

最新变更日志请访问 发布页面变更日志文件

捐赠

捐赠非常受欢迎!

人需要编程来谋生。一个人必须做自己觉得需要做的事情,即使这样做很危险或不受欢迎。

paypal