ripaclub/zf2-mailman

ZF2 模块用于管理邮件投递

v0.3.2 2016-03-18 12:41 UTC

This package is not auto-updated.

Last update: 2024-09-14 15:27:14 UTC


README

Packagist Travis branch Coveralls branch

这是什么?

这是一个 ZF2 模块,它提供了简单配置一个或多个邮件服务的方式。

它支持 ZF2 中包含的所有传输方式,例如实现 Zend\Mail\Transport\TransportInterface 的任何传输。

它还提供了一个用于 Mandrill 的传输。如果您想使用它,还需要安装 mandrill/mandrill(版本 1.0.*)库。

安装

ripaclub/zf2-mailman 添加到您的 composer.json 文件中。

{
   "require": {
       "ripaclub/zf2-mailman": "~0.3.2"
   }
}

使用方法

在配置文件中配置一个传输。

'mailman' => [
    'MailMan\Gmail' => [
        'default_sender' => 'my-name-is-methos@gmail.com',
        'transport' => [
            'type' => 'smtp',
            'options' => [
                 'host' => 'smtp.gmail.com',
                 'port' => '587',
                 'connection_class' => 'login',
                 'connection_config' => [
                     'ssl' => 'tls',
                     'username' => 'my-name-is-methos@gmail.com',
                     'password' => 'MYSECRETPASSWORD',
                 ]
             ]
        ],
    ],
],

别忘了将 MailMan 模块添加到您的 application.config.php 文件中。

'modules' => [
        // ...
        'MailMan',
        'Application',
],

纯文本消息

然后我们发送一个纯文本消息。

$message = new \MailMan\Message();
$message->addTextPart('Test email');
$message->setSubject('My name is methos');
$message->addFrom('my-name-is-methos@gmail.com', 'Methos');
$message->addTo('ripaclub@gmail.com', 'RipaClub');
/** @var \MailMan\Service\MailService $mailService */
$mailService = $this->getServiceLocator()->get('MailMan\Gmail');
$mailService->send($message);

带附件的消息

您想从文件系统发送带有附件的电子邮件消息吗?

$message = new \MailMan\Message();
$message->addAttachment('/path/to/an/attachment.png');
$message->setBody('Test email');
$message->setSubject('My name is methos');
$message->addFrom('my-name-is-methos@gmail.com', 'Methos');
$message->addTo('ripaclub@gmail.com', 'RipaClub');
/** @var \MailMan\Service\MailService $mailService */
$mailService = $this->getServiceLocator()->get('MailMan\Gmail');
$mailService->send($message);

使用模板的消息

$content = new ViewModel();
$content->setTemplate('email/example.phtml');
$content->setVariable('name', 'RipaClub');
$message = new \MailMan\Message();
$message->setSubject('Example email');
$message->addHtmlPart($this->getServiceLocator()->get('ViewRenderer')->render($content));
$message->addTo('ripaclub@gmail.com', 'RipaClub');
/** @var $mailService \MailMan\Service\MailService */
$mailService = $this->getServiceLocator()->get('MailMan\Gmail');
$mailService->send($message);

email/example.phtml 文件的内容将是

<h2>Hi <?= $name; ?>,</h2>
This is an example email with template.

传输配置示例

Mandrill

要使用 Mandrill 传输,请将 "mandrill/mandrill" 添加到您的 composer.json 中。

然后进行配置。

'mailman' => [
    'MailMan\Mandrill' => [
        'default_sender' => 'test@mail.com',
        'transport' => [
            'type' => 'mandrill',
            'options' => [
                'api_key' => 'MYSECRETMANDRILLKEY',
                'sub_account' => 'my-optional-subaccount-if-any'
            ],
        ],
    ],
]

SMTP

在这个示例中,我们使用 SMTP 传输(由 ZF2 提供)。

'mailman' => [
    'MailMan\SMTP' => [
        'default_sender' => 'my-name-is-methos@gmail.com',
        'transport' => [
            'type' => 'smtp',
            'options' => [
                 'host' => 'smtp.gmail.com',
                 'port' => '587',
                 'connection_class' => 'login',
                 'connection_config' => [
                     'ssl' => 'tls',
                     'username' => 'my-name-is-methos@gmail.com',
                     'password' => 'MYSECRETPASSWORD',
                 ]
             ]
        ],
    ],
],

Sendmail

在这个示例中,我们使用 Sendmail 传输(由 ZF2 提供)。

'mailman' => [
    'MailMan\Sendmail' => [
        'default_sender' => 'my-name-is-methos@yourdomain.com',
        'transport' => [
            'type' => 'sendmail'
        ],
    ],
],

Analytics