├── LICENSE.md ├── README.md ├── composer.json └── src ├── ElasticTransport.php ├── MailServiceProvider.php └── TransportManager.php /LICENSE.md: -------------------------------------------------------------------------------- 1 | 2 | The MIT License (MIT) 3 | 4 | Copyright (c) 2017 Obura Tongoi 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in all 14 | copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | SOFTWARE. 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Laravel Elastic Email # 2 | 3 | A Laravel wrapper for Elastic Email 4 | 5 | ### Installation ### 6 | 7 | Add Laravel Elastic Email as a dependency using the composer CLI: 8 | 9 | ```bash 10 | composer require chocoholics/laravel-elastic-email 11 | ``` 12 | 13 | Next, add the following to your config/services.php and add the correct values to your .env file 14 | ```php 15 | 'elastic_email' => [ 16 | 'key' => env('ELASTIC_KEY'), 17 | 'account' => env('ELASTIC_ACCOUNT') 18 | ] 19 | ``` 20 | 21 | Next, in config/app.php, comment out Laravel's default MailServiceProvider. If using < Laravel 5.5, add the MailServiceProvider to the providers array 22 | ```php 23 | 'providers' => [ 24 | /* 25 | * Laravel Framework Service Providers... 26 | */ 27 | ... 28 | // Illuminate\Mail\MailServiceProvider::class, 29 | Chocoholics\LaravelElasticEmail\MailServiceProvider::class, 30 | ... 31 | ], 32 | ``` 33 | 34 | Finally switch your default mail provider to elastic email in your .env file by setting MAIL_DRIVER=elastic_email 35 | 36 | ### Usage ### 37 | 38 | This package works exactly like Laravel's native mailers. Refer to Laravel's Mail documentation. 39 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "chocoholics/laravel-elastic-email", 3 | "description": "A Laravel wrapper for Elastic Email", 4 | "keywords": ["laravel", "email", "elastic email"], 5 | "license": "MIT", 6 | "authors": [ 7 | { 8 | "company": "Artisan Pixelworks", 9 | "email": "obura@tongoi.com", 10 | "url": "https://www.tongoi.com", 11 | "role": "Developer" 12 | } 13 | ], 14 | "support": { 15 | "email": "obura@tongoi.com" 16 | }, 17 | "require": { 18 | "laravel/framework": "5.1.x|5.2.x|5.3.x|5.4.x|5.5.x|5.6.x|5.7.x|5.8.x|6.*", 19 | "guzzlehttp/guzzle": ">=6.3" 20 | }, 21 | "autoload": { 22 | "psr-4": { 23 | "Chocoholics\\LaravelElasticEmail\\": "src/" 24 | } 25 | }, 26 | "extra": { 27 | "laravel": { 28 | "providers": [ 29 | "Chocoholics\\LaravelElasticEmail\\MailServiceProvider" 30 | ] 31 | } 32 | }, 33 | "minimum-stability": "stable" 34 | } 35 | -------------------------------------------------------------------------------- /src/ElasticTransport.php: -------------------------------------------------------------------------------- 1 | client = $client; 52 | $this->key = $key; 53 | $this->account = $account; 54 | } 55 | 56 | /** 57 | * {@inheritdoc} 58 | */ 59 | public function send(Swift_Mime_SimpleMessage $message, &$failedRecipients = null) 60 | { 61 | $this->beforeSendPerformed($message); 62 | 63 | $data = [ 64 | 'api_key' => $this->key, 65 | 'account' => $this->account, 66 | 'msgTo' => $this->getEmailAddresses($message), 67 | 'msgCC' => $this->getEmailAddresses($message, 'getCc'), 68 | 'msgBcc' => $this->getEmailAddresses($message, 'getBcc'), 69 | 'msgFrom' => $this->getFromAddress($message)['email'], 70 | 'msgFromName' => $this->getFromAddress($message)['name'], 71 | 'from' => $this->getFromAddress($message)['email'], 72 | 'fromName' => $this->getFromAddress($message)['name'], 73 | 'to' => $this->getEmailAddresses($message), 74 | 'subject' => $message->getSubject(), 75 | 'body_html' => $message->getBody(), 76 | 'body_text' => $this->getText($message) 77 | ]; 78 | 79 | $result = $this->client->post($this->url, [ 80 | 'form_params' => $data 81 | ]); 82 | 83 | return $result; 84 | } 85 | 86 | /** 87 | * Get the plain text part. 88 | * 89 | * @param \Swift_Mime_SimpleMessage $message 90 | * @return text|null 91 | */ 92 | protected function getText(Swift_Mime_SimpleMessage $message) 93 | { 94 | $text = null; 95 | 96 | foreach($message->getChildren() as $child) 97 | { 98 | if($child->getContentType() == 'text/plain') 99 | { 100 | $text = $child->getBody(); 101 | } 102 | } 103 | 104 | return $text; 105 | } 106 | 107 | /** 108 | * @param \Swift_Mime_SimpleMessage $message 109 | * 110 | * @return array 111 | */ 112 | protected function getFromAddress(Swift_Mime_SimpleMessage $message) 113 | { 114 | return [ 115 | 'email' => array_keys($message->getFrom())[0], 116 | 'name' => array_values($message->getFrom())[0], 117 | ]; 118 | } 119 | 120 | protected function getEmailAddresses(Swift_Mime_SimpleMessage $message, $method = 'getTo') 121 | { 122 | $data = call_user_func([$message, $method]); 123 | 124 | if(is_array($data)) 125 | { 126 | return implode(',', array_keys($data)); 127 | } 128 | return ''; 129 | } 130 | } 131 | -------------------------------------------------------------------------------- /src/MailServiceProvider.php: -------------------------------------------------------------------------------- 1 | app->singleton('swift.transport', function ($app) { 17 | return new TransportManager($app); 18 | }); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/TransportManager.php: -------------------------------------------------------------------------------- 1 | app['config']->get('services.elastic_email', []); 12 | 13 | return new ElasticTransport( 14 | $this->guzzle($config), 15 | $config['key'], 16 | $config['account'] 17 | ); 18 | } 19 | } 20 | --------------------------------------------------------------------------------