andrebian/sendgrid-transport-module

Zend Framework 2 Module that provides SendGrid as a Transport for Mail.

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

This package is auto-updated.

Last update: 2024-10-13 06:35:12 UTC


README

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

This module provide SendGrid as a transport for transactional e-mail in Zend Framework 2.

INSTALLING

composer require andrebian/sendgrid-transport-module

After install follow one of these steps:

  1. Copy the contents file vendor/andrebian/sendgrid-transport-module/mail.global.php.dist and put it in your config/autoload/mail.global.php.

  2. If this file does not exists in your application, just copy the entire file and place into your config/autoload removing the .dist extension.

Then put your SendGrid API Key. To get your API Key, please visit 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',
        )
    )
);

After all, you must register SendGridTransportModule in your config/application.config.php.

// 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',
        ],
    ]
];

USAGE

Via Service

By default, when the SendGridTransportModule is loaded a service is registered and ready to use.

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

Full example with service

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

Directly

If you need more control, you can use the library anywhere you want.

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);
    }
}

Is strongly recommended to use the already registered service.

CONTRIBUTING

You can contribute with this module suggesting improvements, making tests and reporting bugs. Use issues for that.

LICENSE

MIT

ERRORS

Report errors opening Issues.