├── Contributors.md ├── src ├── TransportManager.php ├── LaravelElasticEmailServiceProvider.php └── ElasticTransport.php ├── composer.json └── README.md /Contributors.md: -------------------------------------------------------------------------------- 1 | 2 | Thierry Bellevue -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /src/LaravelElasticEmailServiceProvider.php: -------------------------------------------------------------------------------- 1 | app->singleton('swift.transport', function ($app) { 17 | return new TransportManager($app); 18 | }); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "rdanusha/laravel-elastic-email", 3 | "description": "Package for send emails with attachments via elastic email driver", 4 | "keywords": [ 5 | "laravel", 6 | "email", 7 | "elastic email" 8 | ], 9 | "license": "MIT", 10 | "authors": [ 11 | { 12 | "name": "Anusha Priyamal", 13 | "email": "rdanusha@gmail.com" 14 | } 15 | ], 16 | "support": { 17 | "email": "rdanusha@gmail.com" 18 | }, 19 | "require": { 20 | "laravel/framework": "~5.4" 21 | }, 22 | "autoload": { 23 | "psr-4": { 24 | "Rdanusha\\LaravelElasticEmail\\": "src/" 25 | } 26 | }, 27 | "minimum-stability": "stable" 28 | } 29 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Laravel Elastic Email # 2 | 3 | A Laravel wrapper for Elastic Email 4 | 5 | Can send emails with multiple attachments 6 | 7 | ## IMPORTANT 8 | ### Laravel version 9 | **5.5** or older - Use Version 1.1.1 10 | 11 | **5.6** and forwards - Use version 1.2 12 | 13 | ### Installation ### 14 | 15 | * Step 1 16 | 17 | Install package via composer 18 | 19 | ```bash 20 | composer require rdanusha/laravel-elastic-email 21 | ``` 22 | * Step 2 23 | 24 | Add this code to **.env file** 25 | ``` 26 | ELASTIC_ACCOUNT= 27 | ELASTIC_KEY= 28 | ``` 29 | * Step 3 30 | 31 | Update **MAIL_DRIVER** value as 'elastic_email' in your **.env file** 32 | ``` 33 | MAIL_DRIVER=elastic_email 34 | ``` 35 | 36 | * Step 4 37 | 38 | Add this code to your **config/services.php** file 39 | ``` 40 | 'elastic_email' => [ 41 | 'key' => env('ELASTIC_KEY'), 42 | 'account' => env('ELASTIC_ACCOUNT') 43 | ] 44 | ``` 45 | * Step 5 46 | 47 | Open **config/app.php** file and go to providers array, Then comment out Laravel's default MailServiceProvider and add the following 48 | ```php 49 | 'providers' => [ 50 | /* 51 | * Laravel Framework Service Providers... 52 | */ 53 | ... 54 | // Illuminate\Mail\MailServiceProvider::class, 55 | Rdanusha\LaravelElasticEmail\LaravelElasticEmailServiceProvider::class, 56 | ... 57 | ], 58 | ``` 59 | 60 | ### Usage ### 61 | 62 | This package works exactly like Laravel's native mailers. Refer to Laravel's Mail documentation. 63 | 64 | https://laravel.com/docs/5.5/mail 65 | 66 | ### Code Example ### 67 | ```php 68 | Mail::to($request->user())->send(new OrderShipped($order)); 69 | ``` 70 | -------------------------------------------------------------------------------- /src/ElasticTransport.php: -------------------------------------------------------------------------------- 1 | client = $client; 54 | $this->key = $key; 55 | $this->account = $account; 56 | } 57 | 58 | /** 59 | * {@inheritdoc} 60 | */ 61 | public function send(Swift_Mime_SimpleMessage $message, &$failedRecipients = null) 62 | { 63 | 64 | $this->beforeSendPerformed($message); 65 | 66 | $data = [ 67 | 'apikey' => $this->key, 68 | 'account' => $this->account, 69 | 'msgTo' => $this->getEmailAddresses($message), 70 | 'msgCC' => $this->getEmailAddresses($message, 'getCc'), 71 | 'msgBcc' => $this->getEmailAddresses($message, 'getBcc'), 72 | 'msgFrom' => $this->getFromAddress($message)['email'], 73 | 'msgFromName' => $this->getFromAddress($message)['name'], 74 | 'from' => $this->getFromAddress($message)['email'], 75 | 'fromName' => $this->getFromAddress($message)['name'], 76 | 'to' => $this->getEmailAddresses($message), 77 | 'subject' => $message->getSubject(), 78 | 'bodyHtml' => $message->getBody(), 79 | 'bodyText' => $this->getText($message), 80 | 81 | ]; 82 | 83 | 84 | $attachments = $message->getChildren(); 85 | $attachmentCount = $this->checkAttachmentCount($attachments); 86 | if ($attachmentCount > 0) { 87 | $data = $this->attach($attachments, $data); 88 | } 89 | $ch = curl_init(); 90 | 91 | curl_setopt_array($ch, array( 92 | CURLOPT_URL => $this->url, 93 | CURLOPT_POST => true, 94 | CURLOPT_POSTFIELDS => $data, 95 | CURLOPT_RETURNTRANSFER => true, 96 | CURLOPT_HEADER => false, 97 | CURLOPT_SSL_VERIFYPEER => false 98 | )); 99 | 100 | $result = curl_exec($ch); 101 | curl_close($ch); 102 | 103 | if ($attachmentCount > 0) { 104 | $this->deleteTempAttachmentFiles($data, $attachmentCount); 105 | } 106 | 107 | return $result; 108 | } 109 | 110 | 111 | /** 112 | * Add attachments to post data array 113 | * @param $attachments 114 | * @param $data 115 | * @return mixed 116 | */ 117 | public function attach($attachments, $data) 118 | { 119 | if (is_array($attachments) && count($attachments) > 0) { 120 | $i = 1; 121 | foreach ($attachments AS $attachment) { 122 | if ($attachment instanceof \Swift_Attachment) { 123 | $attachedFile = $attachment->getBody(); 124 | $fileName = $attachment->getFilename(); 125 | $ext = pathinfo($fileName, PATHINFO_EXTENSION); 126 | $tempName = uniqid() . '.' . $ext; 127 | Storage::put($tempName, $attachedFile); 128 | $type = $attachment->getContentType(); 129 | $attachedFilePath = storage_path($tempName); 130 | $data['file_' . $i] = new \CurlFile($attachedFilePath, $type, $fileName); 131 | $i++; 132 | } 133 | } 134 | } 135 | 136 | return $data; 137 | } 138 | 139 | 140 | /** 141 | * Check Swift_Attachment count 142 | * @param $attachments 143 | * @return bool 144 | */ 145 | public function checkAttachmentCount($attachments) 146 | { 147 | $count = 0; 148 | foreach ($attachments AS $attachment) { 149 | if ($attachment instanceof \Swift_Attachment) { 150 | $count++; 151 | } 152 | } 153 | return $count; 154 | } 155 | 156 | 157 | /** 158 | * Get the plain text part. 159 | * 160 | * @param \Swift_Mime_Message $message 161 | * @return text|null 162 | */ 163 | protected function getText(Swift_Mime_SimpleMessage $message) 164 | { 165 | $text = null; 166 | 167 | foreach ($message->getChildren() as $child) { 168 | if ($child->getContentType() == 'text/plain') { 169 | $text = $child->getBody(); 170 | } 171 | } 172 | 173 | return $text; 174 | } 175 | 176 | /** 177 | * @param \Swift_Mime_Message $message 178 | * 179 | * @return array 180 | */ 181 | protected function getFromAddress(Swift_Mime_SimpleMessage $message) 182 | { 183 | return [ 184 | 'email' => array_keys($message->getFrom())[0], 185 | 'name' => array_values($message->getFrom())[0], 186 | ]; 187 | } 188 | 189 | protected function getEmailAddresses(Swift_Mime_SimpleMessage $message, $method = 'getTo') 190 | { 191 | $data = call_user_func([$message, $method]); 192 | 193 | if (is_array($data)) { 194 | return implode(',', array_keys($data)); 195 | } 196 | return ''; 197 | } 198 | 199 | /** 200 | * delete temp attachment files 201 | * @param $data 202 | * @param $count 203 | */ 204 | protected function deleteTempAttachmentFiles($data, $count) 205 | { 206 | for ($i = 1; $i <= $count; $i++) { 207 | $file = $data['file_' . $i]->name; 208 | if (file_exists($file)) { 209 | unlink($file); 210 | } 211 | } 212 | } 213 | } 214 | --------------------------------------------------------------------------------