├── .gitignore ├── README.md ├── composer.json └── src ├── SparkpostServiceProvider.php └── Transport ├── SparkPostTransport.php ├── SparkPostTransportFiveFive.php ├── SparkPostTransportFiveZero.php └── SparkPostTransportTrait.php /.gitignore: -------------------------------------------------------------------------------- 1 | *.DS_store 2 | .idea/* -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ### No Longer Maintained 2 | 3 | Please use: 4 | https://github.com/vemcogroup/laravel-sparkpost-driver 5 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "clarification/sparkpost-laravel-driver", 3 | "type": "library", 4 | "description" : "Sparkpost mail driver for Laravel", 5 | "keywords": ["laravel", "mail", "driver", "sparkpost"], 6 | "license": "MIT", 7 | "homepage" : "http://clarification.io", 8 | "authors": [ 9 | { 10 | "name" : "Robin Lidbetter", 11 | "role" : "Developer" 12 | } 13 | ], 14 | "abandoned": "vemcogroup/laravel-sparkpost-driver", 15 | "require": { 16 | "guzzlehttp/guzzle": ">=5.0", 17 | "illuminate/container": ">=5.0", 18 | "swiftmailer/swiftmailer": "~5.1||~6.0" 19 | }, 20 | "extra": { 21 | "laravel": { 22 | "providers": [ 23 | "Clarification\\MailDrivers\\Sparkpost\\SparkpostServiceProvider" 24 | ] 25 | } 26 | }, 27 | "autoload": { 28 | "psr-4": { 29 | "Clarification\\MailDrivers\\Sparkpost\\": "src" 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/SparkpostServiceProvider.php: -------------------------------------------------------------------------------- 1 | app->extend('swift.transport', function(TransportManager $manager) { 42 | 43 | $manager->extend('sparkpost', function() { 44 | 45 | $config = $this->app['config']->get('services.sparkpost', []); 46 | $sparkpostOptions = isset($config['options']) ? $config['options'] : []; 47 | $guzzleOptions = isset($config['guzzle']) ? $config['guzzle'] : []; 48 | $client = new Client($guzzleOptions); 49 | 50 | if(class_exists(AbstractTransport::class)) { 51 | // version 6 of swiftmailer removed this class in favor of Swift_Mime_SimpleMessage 52 | // https://github.com/swiftmailer/swiftmailer/blob/5a90a13527e955f2d6e8fcc8876d087a89d37da3/CHANGES#L64-L78 53 | // larvel version 5.5 requires version 6 of swiftmailer and has updated method signatures 54 | if(!interface_exists(Swift_Mime_Message::class)) { 55 | return new SparkPostTransportFiveFive($client, $config['secret'], $sparkpostOptions); 56 | } 57 | 58 | return new SparkPostTransport($client, $config['secret'], $sparkpostOptions); 59 | } 60 | 61 | // Fallback to implementation which only depends on Swift_Transport 62 | return new SparkPostTransportFiveZero($client, $config['secret'], $sparkpostOptions); 63 | }); 64 | 65 | return $manager; 66 | }); 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /src/Transport/SparkPostTransport.php: -------------------------------------------------------------------------------- 1 | beforeSendPerformed($message); 15 | 16 | $recipients = $this->getRecipients($message); 17 | 18 | $message->setBcc([]); 19 | 20 | $options = [ 21 | 'headers' => [ 22 | 'Authorization' => $this->key, 23 | ], 24 | 'json' => [ 25 | 'recipients' => $recipients, 26 | 'content' => [ 27 | 'email_rfc822' => $message->toString(), 28 | ], 29 | ], 30 | ]; 31 | 32 | if ($this->options) { 33 | $options['json']['options'] = $this->options; 34 | } 35 | 36 | return $this->client->post($this->getEndpoint(), $options); 37 | } 38 | 39 | /** 40 | * Get all the addresses this message should be sent to. 41 | * 42 | * Note that SparkPost still respects CC, BCC headers in raw message itself. 43 | * 44 | * @param Swift_Mime_SimpleMessage $message 45 | * @return array 46 | */ 47 | protected function getRecipients(Swift_Mime_SimpleMessage $message) 48 | { 49 | $to = []; 50 | if ($getTo = $message->getTo()) { 51 | $to = array_merge($to, array_keys($getTo)); 52 | } 53 | 54 | if ($getCc = $message->getCc()) { 55 | $to = array_merge($to, array_keys($getCc)); 56 | } 57 | 58 | if ($getBcc = $message->getBcc()) { 59 | $to = array_merge($to, array_keys($getBcc)); 60 | } 61 | 62 | $recipients = array_map(function ($address) { 63 | return compact('address'); 64 | }, $to); 65 | 66 | return $recipients; 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /src/Transport/SparkPostTransportFiveZero.php: -------------------------------------------------------------------------------- 1 | plugins, $plugin); 51 | } 52 | /** 53 | * Iterate through registered plugins and execute plugins' methods. 54 | * 55 | * @param \Swift_Mime_Message $message 56 | * @return void 57 | */ 58 | protected function beforeSendPerformed(Swift_Mime_Message $message) 59 | { 60 | $event = new Swift_Events_SendEvent($this, $message); 61 | foreach ($this->plugins as $plugin) { 62 | if (method_exists($plugin, 'beforeSendPerformed')) { 63 | $plugin->beforeSendPerformed($event); 64 | } 65 | } 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /src/Transport/SparkPostTransportTrait.php: -------------------------------------------------------------------------------- 1 | client = $client; 34 | $this->key = $key; 35 | $this->options = $options; 36 | } 37 | 38 | /** 39 | * {@inheritdoc} 40 | */ 41 | public function send(Swift_Mime_Message $message, &$failedRecipients = null) 42 | { 43 | $this->beforeSendPerformed($message); 44 | 45 | $recipients = $this->getRecipients($message); 46 | 47 | $message->setBcc([]); 48 | 49 | $options = [ 50 | 'headers' => [ 51 | 'Authorization' => $this->key, 52 | ], 53 | 'json' => [ 54 | 'recipients' => $recipients, 55 | 'content' => [ 56 | 'email_rfc822' => $message->toString(), 57 | ], 58 | ], 59 | ]; 60 | 61 | if ($this->options) { 62 | $options['json']['options'] = $this->options; 63 | } 64 | 65 | return $this->client->post($this->getEndpoint(), $options); 66 | } 67 | 68 | /** 69 | * Get all the addresses this message should be sent to. 70 | * 71 | * Note that SparkPost still respects CC, BCC headers in raw message itself. 72 | * 73 | * @param Swift_Mime_Message $message 74 | * @return array 75 | */ 76 | protected function getRecipients(Swift_Mime_Message $message) 77 | { 78 | $to = []; 79 | if ($getTo = $message->getTo()) { 80 | $to = array_merge($to, array_keys($getTo)); 81 | } 82 | 83 | if ($getCc = $message->getCc()) { 84 | $to = array_merge($to, array_keys($getCc)); 85 | } 86 | 87 | if ($getBcc = $message->getBcc()) { 88 | $to = array_merge($to, array_keys($getBcc)); 89 | } 90 | 91 | $recipients = array_map(function ($address) { 92 | return compact('address'); 93 | }, $to); 94 | 95 | return $recipients; 96 | } 97 | 98 | /** 99 | * Get the endpoint used by transport, depends if option endpoint is specified 100 | * Maybe https://api.sparkpost.com/api/v1/transmissions (default) or https://api.eu.sparkpost.com/api/v1/transmissions 101 | * 102 | * @return string 103 | */ 104 | public function getEndpoint() 105 | { 106 | $endpoint = 'https://api.sparkpost.com/api/v1/transmissions'; 107 | if ($this->options && !empty($this->options['endpoint'])) { 108 | $endpoint = $this->options['endpoint']; 109 | } 110 | return $endpoint; 111 | } 112 | 113 | /** 114 | * Get the API key being used by the transport. 115 | * 116 | * @return string 117 | */ 118 | public function getKey() 119 | { 120 | return $this->key; 121 | } 122 | 123 | /** 124 | * Set the API key being used by the transport. 125 | * 126 | * @param string $key 127 | * @return string 128 | */ 129 | public function setKey($key) 130 | { 131 | return $this->key = $key; 132 | } 133 | } 134 | --------------------------------------------------------------------------------