-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit c999361
Showing
5 changed files
with
256 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
*.DS_store | ||
.idea/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
### Install | ||
|
||
Require this package with composer using the following command: | ||
```bash | ||
composer require clarification/sendgrid-laravel-driver | ||
``` | ||
|
||
After updating composer, add the service provider to the `providers` array in `config/app.php` | ||
```php | ||
Clarification\MailDrivers\Sendgrid\SendgridServiceProvider::class, | ||
``` | ||
|
||
You will also need to add the sendgrid API Key settings to the array in `config/services.php` and set up the environment key | ||
```php | ||
'sendgrid' => [ | ||
'api_key' => env('SENDGRID_API_KEY'), | ||
], | ||
``` | ||
```bash | ||
SENDGRID_API_KEY=__Your_key_here__ | ||
``` | ||
|
||
Finally you need to set your mail driver to `sendgrid`. You can do this by changing the driver in `config/mail.php` | ||
```php | ||
'driver' => env('MAIL_DRIVER', 'sendgrid'), | ||
``` | ||
|
||
Or by setting the environment variable `MAIL_DRIVER` in your .env file | ||
```bash | ||
MAIL_DRIVER=sendgrid | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{ | ||
"name": "clarification/sendgrid-laravel-driver", | ||
"type": "library", | ||
"description" : "Sendgrid mail driver for Laravel", | ||
"keywords": ["laravel", "mail", "driver", "sendgrid"], | ||
"license": "MIT", | ||
"homepage" : "http://clarification.io", | ||
"authors": [ | ||
{ | ||
"name" : "Robin Lidbetter", | ||
"role" : "Developer" | ||
} | ||
], | ||
"require": { | ||
"illuminate/container": ">=4.2", | ||
"swiftmailer/swiftmailer": "~5.1" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"Clarification\\MailDrivers\\Sendgrid\\": "src" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
namespace Clarification\MailDrivers\Sendgrid; | ||
|
||
use GuzzleHttp\Client; | ||
use Illuminate\Support\Arr; | ||
use Illuminate\Mail\TransportManager; | ||
use Illuminate\Support\ServiceProvider; | ||
use Clarification\MailDrivers\Sendgrid\Transport\SendgridTransport; | ||
|
||
class SendgridServiceProvider extends ServiceProvider | ||
{ | ||
/** | ||
* After register is called on all service providers, then boot is called | ||
*/ | ||
public function boot() | ||
{ | ||
// | ||
} | ||
|
||
/** | ||
* Register is called on all service providers first. | ||
* | ||
* We must register the extension before anything tries to use the mailing functionality. | ||
* None of the closures are executed until someone tries to send an email. | ||
* | ||
* This will register a closure which will be run when 'swift.transport' (the transport manager) is first resolved. | ||
* Then we extend the transport manager, by adding the spark post transport object as the 'sparkpost' driver. | ||
*/ | ||
public function register() | ||
{ | ||
$this->app->extend('swift.transport', function(TransportManager $manager) { | ||
$manager->extend('sendgrid', function() { | ||
$config = $this->app['config']->get('services.sendgrid', []); | ||
$client = new Client(Arr::get($config, 'guzzle', [])); | ||
return new SendgridTransport($client, $config['api_key']); | ||
}); | ||
return $manager; | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
<?php | ||
|
||
namespace Clarification\MailDrivers\Sendgrid\Transport; | ||
|
||
use Swift_Image; | ||
use Swift_MimePart; | ||
use Swift_Attachment; | ||
use Swift_Mime_Message; | ||
use GuzzleHttp\ClientInterface; | ||
use Illuminate\Mail\Transport\Transport; | ||
|
||
class SendgridTransport extends Transport | ||
{ | ||
const MAXIMUM_FILE_SIZE = 7340032; | ||
const SMTP_API_NAME = 'sendgrid/x-smtpapi'; | ||
|
||
protected $client; | ||
protected $options; | ||
|
||
public function __construct(ClientInterface $client, $api_key) | ||
{ | ||
$this->client = $client; | ||
$this->options = [ | ||
'headers' => ['Authorization' => 'Bearer ' . $api_key] | ||
]; | ||
} | ||
|
||
/** | ||
* Send the given Message. | ||
* | ||
* Recipient/sender data will be retrieved from the Message API. | ||
* The return value is the number of recipients who were accepted for delivery. | ||
* | ||
* @param Swift_Mime_Message $message | ||
* @param string[] $failedRecipients An array of failures by-reference | ||
* | ||
* @return int | ||
*/ | ||
public function send(Swift_Mime_Message $message, &$failedRecipients = null) | ||
{ | ||
list($from, $fromName) = $this->getFromAddresses($message); | ||
$payload = $this->options; | ||
$data = [ | ||
'from' => $from, | ||
'fromname' => isset($fromName) ? $fromName : null, | ||
'subject' => $message->getSubject(), | ||
'html' => $message->getBody() | ||
]; | ||
$this->setTo($data, $message); | ||
$this->setCc($data, $message); | ||
$this->setBcc($data, $message); | ||
$this->setText($data, $message); | ||
$this->setAttachment($data, $message); | ||
$this->setSmtpApi($data, $message); | ||
if (version_compare(ClientInterface::VERSION, '6') === 1) { | ||
$payload += ['form_params' => $data]; | ||
} else { | ||
$payload += ['body' => $data]; | ||
} | ||
return $this->client->post('https://api.sendgrid.com/api/mail.send.json', $payload); | ||
} | ||
/** | ||
* @param $data | ||
* @param Swift_Mime_Message $message | ||
*/ | ||
protected function setTo(&$data, Swift_Mime_Message $message) | ||
{ | ||
if ($to = $message->getTo()) { | ||
$data['to'] = array_keys($to); | ||
$data['toname'] = array_values($to); | ||
} | ||
} | ||
/** | ||
* @param $data | ||
* @param Swift_Mime_Message $message | ||
*/ | ||
protected function setCc(&$data, Swift_Mime_Message $message) | ||
{ | ||
if ($cc = $message->getCc()) { | ||
$data['cc'] = array_keys($cc); | ||
$data['ccname'] = array_values($cc); | ||
} | ||
} | ||
/** | ||
* @param $data | ||
* @param Swift_Mime_Message $message | ||
*/ | ||
protected function setBcc(&$data, Swift_Mime_Message $message) | ||
{ | ||
if ($bcc = $message->getBcc()) { | ||
$data['bcc'] = array_keys($bcc); | ||
$data['bccname'] = array_values($bcc); | ||
} | ||
} | ||
/** | ||
* Get From Addresses. | ||
* | ||
* @param Swift_Mime_Message $message | ||
* @return array | ||
*/ | ||
protected function getFromAddresses(Swift_Mime_Message $message) | ||
{ | ||
if ($message->getFrom()) { | ||
foreach ($message->getFrom() as $address => $name) { | ||
return [$address, $name]; | ||
} | ||
} | ||
return []; | ||
} | ||
/** | ||
* Set text contents. | ||
* | ||
* @param $data | ||
* @param Swift_Mime_Message $message | ||
*/ | ||
protected function setText(&$data, Swift_Mime_Message $message) | ||
{ | ||
foreach ($message->getChildren() as $attachment) { | ||
if (!$attachment instanceof Swift_MimePart) { | ||
continue; | ||
} | ||
$data['text'] = $attachment->getBody(); | ||
} | ||
} | ||
/** | ||
* Set Attachment Files. | ||
* | ||
* @param $data | ||
* @param Swift_Mime_Message $message | ||
*/ | ||
protected function setAttachment(&$data, Swift_Mime_Message $message) | ||
{ | ||
foreach ($message->getChildren() as $attachment) { | ||
if (!$attachment instanceof Swift_Attachment || !strlen($attachment->getBody()) > self::MAXIMUM_FILE_SIZE) { | ||
continue; | ||
} | ||
$handler = tmpfile(); | ||
fwrite($handler, $attachment->getBody()); | ||
$data['files[' . $attachment->getFilename() . ']'] = $handler; | ||
} | ||
} | ||
/** | ||
* Set Sendgrid SMTP API | ||
* | ||
* @param $data | ||
* @param Swift_Mime_Message $message | ||
*/ | ||
protected function setSmtpApi(&$data, Swift_Mime_Message $message) | ||
{ | ||
foreach ($message->getChildren() as $attachment) { | ||
if (!$attachment instanceof Swift_Image | ||
|| !in_array(self::SMTP_API_NAME, [$attachment->getFilename(), $attachment->getContentType()]) | ||
) { | ||
continue; | ||
} | ||
$data['x-smtpapi'] = json_encode($attachment->getBody()); | ||
} | ||
} | ||
} |