|
1 | | -# cakephp-sendgrid |
| 1 | +# SendGrid Plugin for CakePHP 3 |
| 2 | + |
| 3 | +[](https://travis-ci.org/sprintcube/cakephp-sendgrid) |
| 4 | +[](https://codecov.io/gh/sprintcube/cakephp-sendgrid) |
| 5 | +[](LICENSE) |
| 6 | +[](https://packagist.org/packages/sprintcube/cakephp-sendgrid) |
| 7 | +[](https://packagist.org/packages/sprintcube/cakephp-sendgrid) |
| 8 | + |
| 9 | +This plugin provides email delivery using [SendGrid](https://sendgrid.com/). |
| 10 | + |
| 11 | +## Requirements |
| 12 | + |
| 13 | +This plugin has the following requirements: |
| 14 | + |
| 15 | +* CakePHP 3.4.0 or greater. |
| 16 | +* PHP 5.6 or greater. |
| 17 | + |
| 18 | +## Installation |
| 19 | + |
| 20 | +You can install this plugin into your CakePHP application using [composer](http://getcomposer.org). |
| 21 | + |
| 22 | +``` |
| 23 | +composer require sprintcube/cakephp-sendgrid |
| 24 | +``` |
| 25 | + |
| 26 | +After installation, [Load the plugin](http://book.cakephp.org/3.0/en/plugins.html#loading-a-plugin) |
| 27 | +```php |
| 28 | +Plugin::load('SendGrid'); |
| 29 | +``` |
| 30 | +Or, you can load the plugin using the shell command |
| 31 | +```sh |
| 32 | +$ bin/cake plugin load SendGrid |
| 33 | +``` |
| 34 | + |
| 35 | +## Setup |
| 36 | + |
| 37 | +Set your SendGrid Api key in `EmailTransport` settings in app.php |
| 38 | + |
| 39 | +```php |
| 40 | +'EmailTransport' => [ |
| 41 | +... |
| 42 | + 'sendgrid' => [ |
| 43 | + 'className' => 'SendGrid.SendGrid', |
| 44 | + 'apiKey' => 'your-api-key' // your api key |
| 45 | + ] |
| 46 | +] |
| 47 | +``` |
| 48 | +And create new delivery profile in `Email` settings. |
| 49 | + |
| 50 | +```php |
| 51 | +'Email' => [ |
| 52 | + 'default' => [ |
| 53 | + 'transport' => 'default', |
| 54 | + 'from' => 'you@localhost', |
| 55 | + //'charset' => 'utf-8', |
| 56 | + //'headerCharset' => 'utf-8', |
| 57 | + ], |
| 58 | + 'sendgrid' => [ |
| 59 | + 'transport' => 'sendgrid' |
| 60 | + ] |
| 61 | +] |
| 62 | +``` |
| 63 | + |
| 64 | +## Usage |
| 65 | + |
| 66 | +You can now simply use the CakePHP `Email` to send an email via SendGrid. |
| 67 | + |
| 68 | +```php |
| 69 | +$email = new Email('sendgrid'); |
| 70 | + |
| 71 | +$email->setFrom(['you@yourdomain.com' => 'CakePHP SendGrid']) |
| 72 | + ->setTo('foo@example.com.com') |
| 73 | + ->addTo('bar@example.com') |
| 74 | + ->addCc('john@example.com') |
| 75 | + ->setHeaders(['X-Custom' => 'headervalue']) |
| 76 | + ->setSubject('Email from CakePHP SendGrid plugin') |
| 77 | + ->send('Message from CakePHP SendGrid plugin'); |
| 78 | +``` |
| 79 | + |
| 80 | +That is it. |
| 81 | + |
| 82 | +## Advance Use |
| 83 | +You can also use few more options to send email via SendGrid APIs. To do so, get the transport instance and call the appropriate methods before sending the email. |
| 84 | + |
| 85 | +### Custom Headers |
| 86 | +You can pass your own headers. It must be prefixed with "X-". Use the default `Email::setHeaders` method like, |
| 87 | + |
| 88 | +```php |
| 89 | +$email = new Email('sendgrid'); |
| 90 | + |
| 91 | +$email->setFrom(['you@yourdomain.com' => 'CakePHP SendGrid']) |
| 92 | + ->setSender('someone@example.com', 'Someone') |
| 93 | + ->setTo('foo@example.com.com') |
| 94 | + ->addTo('bar@example.com') |
| 95 | + ->setHeaders([ |
| 96 | + 'X-Custom' => 'headervalue', |
| 97 | + 'X-MyHeader' => 'myvalue' |
| 98 | + ]) |
| 99 | + ->setSubject('Email from CakePHP SendGrid plugin') |
| 100 | + ->send('Message from CakePHP SendGrid plugin'); |
| 101 | +``` |
| 102 | + |
| 103 | +> When sending request, `X-` will be removed from header name e.g. X-MyHeader will become MyHeader |
| 104 | +
|
| 105 | +### Attachments |
| 106 | +Set your attachments using `Email::setAttachments` method. |
| 107 | + |
| 108 | +```php |
| 109 | +$email = new Email('sendgrid'); |
| 110 | + |
| 111 | +$email->setFrom(['you@yourdomain.com' => 'CakePHP SendGrid']) |
| 112 | + ->setSender('someone@example.com', 'Someone') |
| 113 | + ->setTo('foo@example.com.com') |
| 114 | + ->addTo('bar@example.com') |
| 115 | + ->setAttachments([ |
| 116 | + 'cake_icon1.png' => Configure::read('App.imageBaseUrl') . 'cake.icon.png', |
| 117 | + 'cake_icon2.png' => ['file' => Configure::read('App.imageBaseUrl') . 'cake.icon.png'], |
| 118 | + WWW_ROOT . 'favicon.ico' |
| 119 | + ]) |
| 120 | + ->setSubject('Email from CakePHP SendGrid plugin') |
| 121 | + ->send('Message from CakePHP SendGrid plugin'); |
| 122 | +``` |
| 123 | + |
| 124 | +> To send inline attachment, use `contentId` parameter while setting attachment. |
| 125 | +
|
| 126 | +### Template |
| 127 | +You can use the template created in SendGrid backend. Get the template id by either using their API or from the URL. |
| 128 | +Set the template id using `setTemplate` method. |
| 129 | + |
| 130 | +```php |
| 131 | +$email = new Email('sendgrid'); |
| 132 | +$emailInstance = $email->getTransport(); |
| 133 | +$emailInstance->setTemplte(123); |
| 134 | +$email->send(); |
| 135 | +``` |
| 136 | + |
| 137 | +### Schedule |
| 138 | +You can schedule the email to be sent in future date. You can set upto 72 hours in future as per SendGrid documentation. |
| 139 | + |
| 140 | +```php |
| 141 | +$email = new Email('sendgrid'); |
| 142 | +$emailInstance = $email->getTransport(); |
| 143 | +$emailInstance->setScheduleTime(1537958411); |
| 144 | +$email->send(); |
| 145 | +``` |
| 146 | + |
| 147 | +## Reporting Issues |
| 148 | + |
| 149 | +If you have a problem with this plugin or any bug, please open an issue on [GitHub](https://github.com/sprintcube/cakephp-sendgrid/issues). |
0 commit comments