This module provide SendGrid as a transport for transactional e-mail in Zend Framework 2.
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',
],
]
];
By default, when the SendGridTransportModule is loaded a service is registered and ready to use.
// 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.
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.
You can contribute with this module suggesting improvements, making tests and reporting bugs. Use issues for that.
Report errors opening Issues.