dev-master
9999999-devZend Framework 2 Module that provides SendGrid as a Transport for Mail.
MIT
The Requires
The Development Requires
zf2 module
Wallogit.com
2017 © Pedro Peláez
Zend Framework 2 Module that provides SendGrid as a Transport for Mail.
This module provide SendGrid as a transport for transactional e-mail in Zend Framework 2., (*2)
composer require andrebian/sendgrid-transport-module, (*3)
After install follow one of these steps:, (*4)
1) Copy the contents file vendor/andrebian/sendgrid-transport-module/mail.global.php.dist and put it in your config/autoload/mail.global.php., (*5)
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., (*6)
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, (*7)
// 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., (*8)
// 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',
],
]
];
By default, when the SendGridTransportModule is loaded a service is registered and ready to use., (*9)
// 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();
}
}
If you need more control, you can use the library anywhere you want., (*10)
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., (*11)
You can contribute with this module suggesting improvements, making tests and reporting bugs. Use issues for that., (*12)
MIT, (*13)
Report errors opening Issues., (*14)
Zend Framework 2 Module that provides SendGrid as a Transport for Mail.
MIT
zf2 module