|
| 1 | +# Laravel Elastic Email # |
| 2 | + |
| 3 | +[](https://www.paypal.com/donate/?hosted_button_id=6CYVR8U4VDMAA) |
| 4 | + |
| 5 | +A Laravel wrapper for sending emails via Elastic Email service and API capabilities that allows you to check the status of every email sent. |
| 6 | +It provides a basic email log table to store all outbound emails where you can link to a model. |
| 7 | + |
| 8 | +## Installation |
| 9 | + |
| 10 | +Add Laravel Elastic Email as a dependency using the composer CLI: |
| 11 | + |
| 12 | +```bash |
| 13 | +composer require seinoxygen/laravel-elastic-email |
| 14 | +``` |
| 15 | + |
| 16 | +## Mail Service Usage |
| 17 | + |
| 18 | +This package works exactly like Laravel's native mailers. Refer to Laravel's Mail documentation. |
| 19 | + |
| 20 | +Add the following to your config/services.php and add the correct values to your .env file |
| 21 | + |
| 22 | +```php |
| 23 | +'elastic_email' => [ |
| 24 | + 'key' => env('ELASTIC_KEY'), |
| 25 | + 'account' => env('ELASTIC_ACCOUNT') |
| 26 | +] |
| 27 | +``` |
| 28 | + |
| 29 | +Add the following to your config/mail.php |
| 30 | + |
| 31 | +```php |
| 32 | +'elastic_email' => [ |
| 33 | + 'transport' => 'elasticemail' |
| 34 | +] |
| 35 | +``` |
| 36 | + |
| 37 | +Next, in config/app.php, comment out Laravel's default MailServiceProvider. If using < Laravel 5.5, add the MailServiceProvider and ApiServiceProvider to the providers array |
| 38 | + |
| 39 | +```php |
| 40 | +'providers' => [ |
| 41 | + ... |
| 42 | + SeinOxygen\ElasticEmail\MailServiceProvider::class, |
| 43 | + SeinOxygen\ElasticEmail\ApiServiceProvider::class, |
| 44 | + ... |
| 45 | +], |
| 46 | +``` |
| 47 | + |
| 48 | +Next, in config/app.php, add the ElasticEmail to the aliases array |
| 49 | + |
| 50 | +```php |
| 51 | +'aliases' => [ |
| 52 | + ... |
| 53 | + 'ElasticEmail' => SeinOxygen\ElasticEmail\Facades\ElasticEmail::class, |
| 54 | + ... |
| 55 | +], |
| 56 | +``` |
| 57 | + |
| 58 | +Finally switch your default mail provider to elastic email in your .env file by setting **MAIL_DRIVER=elastic_email** |
| 59 | + |
| 60 | +## Outbound Email Tracking |
| 61 | + |
| 62 | +To keep track of all emails sent by the driver you'll need to publish the migrations and the configuration files: |
| 63 | + |
| 64 | +```bash |
| 65 | +php artisan vendor:publish --provider="SeinOxygen\ElasticEmail\ApiServiceProvider" --tag="migrations" |
| 66 | +``` |
| 67 | + |
| 68 | +```bash |
| 69 | +php artisan migrate |
| 70 | +``` |
| 71 | + |
| 72 | +```bash |
| 73 | +php artisan vendor:publish --provider="SeinOxygen\ElasticEmail\ApiServiceProvider" --tag="config" |
| 74 | +``` |
| 75 | + |
| 76 | +By default all outgoing emails will be stored with the Elastic Email **message_id** and **transaction_id**. |
| 77 | + |
| 78 | +Check **config/elasticemail.php** for more options. |
| 79 | + |
| 80 | +### Linking Outgoing Emails To Your Models ### |
| 81 | + |
| 82 | +In your mailable be sure to set the with array the following way. |
| 83 | + |
| 84 | +```php |
| 85 | +public function build() |
| 86 | +{ |
| 87 | + // You can set ad many models you want to relate with the outgoing email |
| 88 | + $models = [ |
| 89 | + [$yourmodel->id, get_class($yourmodel)], |
| 90 | + ]; |
| 91 | + |
| 92 | + return $this |
| 93 | + ->subject("My Subject") |
| 94 | + ->view('my-view') |
| 95 | + ->with([ |
| 96 | + 'models' => $models |
| 97 | + ]); |
| 98 | +} |
| 99 | +``` |
| 100 | + |
| 101 | +Sorry if it looks ugly. I haven't found a better way to do this...yet. |
| 102 | + |
| 103 | +### Capturing Webhook Events ### |
| 104 | + |
| 105 | +You will need to set a webhook in Elastic Email service pointing to **yourappurl.com/webhook/elasticemail** |
| 106 | + |
| 107 | +There is an event being fired when data is sent to the webhook url. |
| 108 | + |
| 109 | +```php |
| 110 | +<?php |
| 111 | + |
| 112 | +namespace app\Listeners; |
| 113 | + |
| 114 | +use SeinOxygen\ElasticEmail\Events\WebhookCallReceived; |
| 115 | + |
| 116 | +class WebhookCallListerner |
| 117 | +{ |
| 118 | + public function handle(WebhookCallReceived $event) |
| 119 | + { |
| 120 | + $request = $event->request; |
| 121 | + } |
| 122 | +} |
| 123 | +``` |
| 124 | + |
| 125 | +## Api Usage |
| 126 | + |
| 127 | +For documentation visit https://api.elasticemail.com/public/help |
| 128 | + |
| 129 | +```php |
| 130 | + |
| 131 | + //For contact |
| 132 | + ElasticEmail::Contact() |
| 133 | + |
| 134 | + //For emails |
| 135 | + ElasticEmail::Email() |
| 136 | + |
| 137 | +``` |
| 138 | + |
| 139 | +## Credits |
| 140 | + |
| 141 | +This package is based on [ZanySoft](https://github.com/zanysoft/laravel-elastic-email) |
| 142 | + |
| 143 | +## License |
| 144 | + |
| 145 | +The MIT License (MIT). Please see [License File](LICENSE.md) for more information. |
0 commit comments