soflomo/mail

模块,简化在Zend Framework 2中使用发送电子邮件消息的过程

dev-master 2017-03-09 08:23 UTC

This package is not auto-updated.

Last update: 2024-09-14 13:45:35 UTC


README

Latest Stable Version Build Status Scrutinizer Code Quality Code Coverage Dependency Status

Soflomo\Mail是一个门面模块,它集成了多种组件以在Zend Framework 2中发送电子邮件。它允许用户通过一个调用来编写消息、渲染模板和配置传输。每个组件都松散耦合,可以在运行时替换。

Soflomo-Mail提供以下功能

  1. 单个send()方法来配置消息、渲染模板并发送它
  2. 带有预先填充变量的默认消息对象,例如“发件人”字段
  3. 默认传输,可以在您的配置文件内部进行配置
  4. 所有部分都可以由您自己的任何部分替换

安装

Soflomo\Mail与Composer一起使用。请确保您已下载composer.phar,并且您的项目根目录中有一个composer.json文件。要安装它,请将以下行添加到您的composer.json文件中

"require": {
    "soflomo/mail": "~0.3"
}

安装包后,您需要完成以下步骤才能使用Soflomo-Mail

  1. 通过将Soflomo\Mail添加到您的application.config.php文件中来启用模块。
  2. soflomo_mail.global.php.dist(您可以在Soflomo-Mail的config文件夹中找到此文件)复制到您的config/autoload文件夹,并应用您想要的任何设置。

要求

  1. Zend Framework 2:的Zend\Mail组件
  2. Zend Framework 2:的Zend\ServiceManager组件

用法

Soflomo\Mail使用中央MailService门面。此服务公开单个send()方法,根据一些变量发送电子邮件。

// $serviceLocator is an instance of Zend\Service\ServiceManager

$service = $serviceLocator->get('Soflomo\Mail\Service\MailService');
$service->send(array(
  'to'       => 'bob@acme.com',
  'subject'  => 'Just want to say hi',
  'template' => 'email/test',
));

三个键是发送电子邮件所必需的。在上面的示例中,template是模板的名称,由PhpRenderer解析并设置为消息正文。

send()配置消息,渲染email/test模板,并使用配置的传输发送消息。

对于控制器,存在一个控制器插件可以代理到电子邮件服务

public function sendAction()
{
    $this->email(array(
        'to'       => 'bob@acme.com',
        'subject'  => 'Just want to say hi',
        'template' => 'email/test',
    ));
}

附加选项

除了to之外,您还可以使用fromccbccreply_to。对于每个收件人,您可以在选项后附加_name来设置地址部分的名字。

// $serviceLocator is an instance of Zend\Service\ServiceManager

$service = $serviceLocator->get('Soflomo\Mail\Service\MailService');
$service->send(array(
  'to'            => 'bob@acme.com',
  'subject'       => 'Just want to say hi',
  'template'      => 'email/test',

  'to_name'       => 'Bob',
  'from'          => 'alice@acme.com',
  'from_name'     => 'Alice',

  'cc'            => 'mike@acme.com',
  'cc_name'       => 'Mike',

  'bcc'           => 'john@acme.com'
  'bcc_name'      => 'John',

  'reply_to'      => 'internals@acme.com',
  'reply_to_name' => 'ACME Corp internals mailing list'
));

向多个收件人发送

您可以将所有收件人制作为一个键/值数组。这允许您在一个send()调用中向多个人发送邮件。

// $serviceLocator is an instance of Zend\Service\ServiceManager

$service = $serviceLocator->get('Soflomo\Mail\Service\MailService');
$service->send(array(
  'to'       => array(
    'bob@acme.com' => 'Bob', 'alice@acme.com' => 'Alice'
  ),
  'subject'  => 'Just want to say hi',
  'template' => 'email/test',
));

此时,数组必须是键/值对。非关联数组不会被识别。如果您想留空名字,请使用null

array('bob@acme.com' => null, 'alice@acme.com' => null)

发送自定义头信息

您可以使用headers键向电子邮件消息对象添加额外的头信息。

// $serviceLocator is an instance of Zend\Service\ServiceManager

$service = $serviceLocator->get('Soflomo\Mail\Service\MailService');
$service->send(array(
  'to'       => 'bob@acme.com',
  'subject'  => 'Just want to say hi',
  'template' => 'email/test',
  'headers'  => array(
    'X-Send-By' => 'MyCustomApp'
  ),
));

添加附件

附件通过attachments键发送,应该是一个关联数组,其中键是电子邮件中使用的文件名,值是附件的绝对路径。

// $serviceLocator is an instance of Zend\Service\ServiceManager

$service = $serviceLocator->get('Soflomo\Mail\Service\MailService');
$service->send(array(
  'to'          => 'bob@acme.com',
  'subject'     => 'Just want to say hi',
  'template'    => 'email/test',
  'attachments' => array(
    'filename.ext' => '/absolute/path/to/file'
  ),
));

使用模板变量

send()方法接受第二个参数来在视图模板中注入变量。

// Your template
<p>Welcome <?= $name?></p>
// $serviceLocator is an instance of Zend\Service\ServiceManager

$service = $serviceLocator->get('Soflomo\Mail\Service\MailService');
$service->send(array(
  'to'       => 'bob@acme.com',
  'subject'  => 'Just want to say hi',
  'template' => 'email/test',
), array(
  'name'     => 'Bob',
));

使用您自己的消息对象

如果您已经有了消息对象,可以将它设置为第三个参数。您可以有已经部分配置的消息对象或需要重用已实例化的消息对象。

// $messaga is an instance of Zend\Mail\Message
// $serviceLocator is an instance of Zend\Service\ServiceManager

$message->setFrom('alice@acme.com');
$service = $serviceLocator->get('Soflomo\Mail\Service\MailService');
$service->send(array(
  'to'       => 'bob@acme.com',
  'subject'  => 'Just want to say hi',
  'template' => 'email/test',
), array(), $message);

布局

在您的配置文件中,您可以设置布局文件的路径,您的邮件将使用该布局发送。要回显布局文件中的内容,您只需使用 <?= $this->content; ?>

您也可以通过在选项数组中使用键 layout 来按邮件更改布局。

配置

Soflomo\Mail 完全可配置以满足您的需求。该模块利用依赖注入。这使得任何用户都可以替换 Soflomo\Mail 的默认服务。在用法部分,为这些情况提供了示例。

使用默认传输发送 SMTP

Soflomo\Mail 使用的传输(称为 Soflomo\Mail\Transport)是默认传输(Soflomo\Mail\DefaultTransport)的别名。您可以使用默认传输配置基于配置的 SMTP 传输。只需将其复制到 config/autoload 目录下的本地配置文件中。

'soflomo_mail' => array(
    'transport' => array(
        'type'    => 'smtp',
        'options' => array(
            'name' => 'myserver.com',
            'host' => 'smtp.myserver.com',
            'connection_class'  => 'login',
            'connection_config' => array(
                'ssl'      => 'tls',
                'username' => 'my-username',
                'password' => 'my-password',
            ),
        ),
    ),
),

'type' 可以是 Zend\Mail\Transport\* 的类(因此可以是 "file"、"smtp" 或 "sendmail")。使用 options 数组来实例化与类型相对应的选项对象(对于 "smtp",使用 SmtpOptions 类)。

soflomo_mail.global.php.dist 包含了不同传输类型和配置的更多示例。

或者,给类型提供一个 FQCN,它将使用该类作为传输。请注意,此 FQCN 是一个简单的解决方案,不能实现依赖注入。对于更高级的使用,请参阅如何配置您的 自定义传输

使用现有的替代传输服务

SlmMail 是一个模块,它实现了 Mailgun、Postmark 和 Amazon SES 等各种第三方电子邮件提供者的 API。

所有 SlmMail 传输都是服务管理器中的服务,您可以将它们注入到任何其他类中。对于 Soflomo\Mail,将别名设置为使用的 SlmMail 传输,它将自动注入。

'service_manager' => array(
    'aliases' => array(
        'Soflomo\Mail\Transport' => 'SlmMail\Mail\Transport\SendGridTransport',
    ),
),

使用您的自定义传输工厂

当您有自定义传输并已注册工厂时,您也可以使用它。取您工厂的服务名称,并将其设置为别名。

'service_manager' => array(
    'factories' => array(
        'MyApp\Mail\Transport\MyTransport' => 'MyApp\Mail\Transport\MyTransportFactory'
    ),
    'aliases' => array(
        'Soflomo\Mail\Transport' => 'MyApp\Mail\Transport\MyTransport',
    ),
),