andrebian / sendgrid-transport-module
Zend Framework 2 Module that provides SendGrid as a Transport for Mail.
Requires
- php: >=5.3.3
- sendgrid/sendgrid: 4.0.4
- zendframework/zend-modulemanager: 2.* || ^3.0
Requires (Dev)
- codacy/coverage: dev-master
- mockery/mockery: dev-master
- phpunit/phpunit: 4.4.*
This package is auto-updated.
Last update: 2024-10-13 06:35:12 UTC
README
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:
-
Copy the contents file
vendor/andrebian/sendgrid-transport-module/mail.global.php.dist
and put it in yourconfig/autoload/mail.global.php
. -
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
ERRORS
Report errors opening Issues.