andrebian/sendgrid-transport-module

Zend Framework 2 模块,提供 SendGrid 作为邮件传输方式。

dev-master 2017-02-25 22:21 UTC

This package is auto-updated.

Last update: 2024-09-13 06:29:13 UTC


README

Latest Stable Version Total Downloads Latest Unstable Version License Codacy Badge Codacy Badge

本模块为 Zend Framework 2 提供了 SendGrid 作为事务性电子邮件的传输方式。

安装

composer require andrebian/sendgrid-transport-module

安装后,按照以下步骤之一操作

  1. 复制文件 vendor/andrebian/sendgrid-transport-module/mail.global.php.dist 并将其放入您的 config/autoload/mail.global.php 中。

  2. 如果此文件不在您的应用程序中,只需复制整个文件,并将其放置于您的 config/autoload 中,并移除 .dist 扩展名。

然后放入您的 SendGrid API 密钥。要获取您的 API 密钥,请访问 https://sendgrid.com/docs/Classroom/Send/How_Emails_Are_Sent/api_keys.html

// config/autoload/mail.global.php

return array(
    'mail' => array(
        'sendgrid' => array(
            'api_key' => 'YOUR_API_KEY',
        )
    )
);

最后,您必须在 config/application.config.php 中注册 SendGridTransportModule

// config/application.config.php
return [
    'modules' => [
        'YourPreviousModules',
        'SendGridTransportModule'
    ],
    'module_listener_options' => [
        'module_paths' => [
            './module',
            './vendor',
        ],
        'config_glob_paths' => [
            'config/autoload/{{,*.}global,{,*.}local}.php',
            'module/{*}/config/autoload/{{,*.}global,{,*.}local}.php',
        ],
    ]
];

使用方法

通过服务

默认情况下,当 SendGridTransportModule 被加载时,将注册一个服务并准备好使用。

// In a Controller
$sendGridTransport = $this->getServiceLocator()->get('SendGridTransport');

使用服务的完整示例

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Zend\Mail;

class SomeController extends AbstractActionController
{
    public function someAction()
    {
        $mail = new Mail\Message();
        $mail->setBody('This is the text of the email.');
        $mail->setFrom(new Mail\Address('test@example.org', 'Sender\'s name'));
        $mail->addTo(new Mail\Address('some@address.com', 'User Name'));
        $mail->setSubject('TestSubject');

        $sendGridTransport = $this->getServiceLocator()->get('SendGridTransport');
        $sendGridTransport->send($mail);

        return new ViewModel();
    }
}

直接使用

如果您需要更多控制,您可以在任何地方使用此库。

use SendGrid;
use SendGridTransportModule\SendGridTransport;

class SomeControllerOrServiceOrHelper 
{
    public function someMethod()
    {
        $mail = new Mail\Message();
        $mail->setBody('This is the text of the email.');
        $mail->setFrom(new Mail\Address('test@example.org', 'Sender\'s name'));
        $mail->addTo(new Mail\Address('some@address.com', 'User Name'));
        $mail->setSubject('TestSubject');

        $sendGrid = new SendGrid('YOUR_API_KEY');
        $sendGridEmail = new SendGrid\Email();
        $sendGridTransport = new SendGridTransport($sendGrid, $sendGridEmail);

        $sendGridTransport->send($mail);
    }
}

强烈建议使用已注册的服务。

贡献

您可以通过提出改进建议、编写测试和报告错误来为此模块做出贡献。请使用 问题 来进行。

许可证

MIT

错误

问题 中报告错误。