Skip to content

Commit c999361

Browse files
committed
inital commit
0 parents  commit c999361

File tree

5 files changed

+256
-0
lines changed

5 files changed

+256
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*.DS_store
2+
.idea/*

README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
### Install
2+
3+
Require this package with composer using the following command:
4+
```bash
5+
composer require clarification/sendgrid-laravel-driver
6+
```
7+
8+
After updating composer, add the service provider to the `providers` array in `config/app.php`
9+
```php
10+
Clarification\MailDrivers\Sendgrid\SendgridServiceProvider::class,
11+
```
12+
13+
You will also need to add the sendgrid API Key settings to the array in `config/services.php` and set up the environment key
14+
```php
15+
'sendgrid' => [
16+
'api_key' => env('SENDGRID_API_KEY'),
17+
],
18+
```
19+
```bash
20+
SENDGRID_API_KEY=__Your_key_here__
21+
```
22+
23+
Finally you need to set your mail driver to `sendgrid`. You can do this by changing the driver in `config/mail.php`
24+
```php
25+
'driver' => env('MAIL_DRIVER', 'sendgrid'),
26+
```
27+
28+
Or by setting the environment variable `MAIL_DRIVER` in your .env file
29+
```bash
30+
MAIL_DRIVER=sendgrid
31+
```

composer.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "clarification/sendgrid-laravel-driver",
3+
"type": "library",
4+
"description" : "Sendgrid mail driver for Laravel",
5+
"keywords": ["laravel", "mail", "driver", "sendgrid"],
6+
"license": "MIT",
7+
"homepage" : "http://clarification.io",
8+
"authors": [
9+
{
10+
"name" : "Robin Lidbetter",
11+
"role" : "Developer"
12+
}
13+
],
14+
"require": {
15+
"illuminate/container": ">=4.2",
16+
"swiftmailer/swiftmailer": "~5.1"
17+
},
18+
"autoload": {
19+
"psr-4": {
20+
"Clarification\\MailDrivers\\Sendgrid\\": "src"
21+
}
22+
}
23+
}

src/SendgridServiceProvider.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace Clarification\MailDrivers\Sendgrid;
4+
5+
use GuzzleHttp\Client;
6+
use Illuminate\Support\Arr;
7+
use Illuminate\Mail\TransportManager;
8+
use Illuminate\Support\ServiceProvider;
9+
use Clarification\MailDrivers\Sendgrid\Transport\SendgridTransport;
10+
11+
class SendgridServiceProvider extends ServiceProvider
12+
{
13+
/**
14+
* After register is called on all service providers, then boot is called
15+
*/
16+
public function boot()
17+
{
18+
//
19+
}
20+
21+
/**
22+
* Register is called on all service providers first.
23+
*
24+
* We must register the extension before anything tries to use the mailing functionality.
25+
* None of the closures are executed until someone tries to send an email.
26+
*
27+
* This will register a closure which will be run when 'swift.transport' (the transport manager) is first resolved.
28+
* Then we extend the transport manager, by adding the spark post transport object as the 'sparkpost' driver.
29+
*/
30+
public function register()
31+
{
32+
$this->app->extend('swift.transport', function(TransportManager $manager) {
33+
$manager->extend('sendgrid', function() {
34+
$config = $this->app['config']->get('services.sendgrid', []);
35+
$client = new Client(Arr::get($config, 'guzzle', []));
36+
return new SendgridTransport($client, $config['api_key']);
37+
});
38+
return $manager;
39+
});
40+
}
41+
}

src/Transport/SendgridTransport.php

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
<?php
2+
3+
namespace Clarification\MailDrivers\Sendgrid\Transport;
4+
5+
use Swift_Image;
6+
use Swift_MimePart;
7+
use Swift_Attachment;
8+
use Swift_Mime_Message;
9+
use GuzzleHttp\ClientInterface;
10+
use Illuminate\Mail\Transport\Transport;
11+
12+
class SendgridTransport extends Transport
13+
{
14+
const MAXIMUM_FILE_SIZE = 7340032;
15+
const SMTP_API_NAME = 'sendgrid/x-smtpapi';
16+
17+
protected $client;
18+
protected $options;
19+
20+
public function __construct(ClientInterface $client, $api_key)
21+
{
22+
$this->client = $client;
23+
$this->options = [
24+
'headers' => ['Authorization' => 'Bearer ' . $api_key]
25+
];
26+
}
27+
28+
/**
29+
* Send the given Message.
30+
*
31+
* Recipient/sender data will be retrieved from the Message API.
32+
* The return value is the number of recipients who were accepted for delivery.
33+
*
34+
* @param Swift_Mime_Message $message
35+
* @param string[] $failedRecipients An array of failures by-reference
36+
*
37+
* @return int
38+
*/
39+
public function send(Swift_Mime_Message $message, &$failedRecipients = null)
40+
{
41+
list($from, $fromName) = $this->getFromAddresses($message);
42+
$payload = $this->options;
43+
$data = [
44+
'from' => $from,
45+
'fromname' => isset($fromName) ? $fromName : null,
46+
'subject' => $message->getSubject(),
47+
'html' => $message->getBody()
48+
];
49+
$this->setTo($data, $message);
50+
$this->setCc($data, $message);
51+
$this->setBcc($data, $message);
52+
$this->setText($data, $message);
53+
$this->setAttachment($data, $message);
54+
$this->setSmtpApi($data, $message);
55+
if (version_compare(ClientInterface::VERSION, '6') === 1) {
56+
$payload += ['form_params' => $data];
57+
} else {
58+
$payload += ['body' => $data];
59+
}
60+
return $this->client->post('https://api.sendgrid.com/api/mail.send.json', $payload);
61+
}
62+
/**
63+
* @param $data
64+
* @param Swift_Mime_Message $message
65+
*/
66+
protected function setTo(&$data, Swift_Mime_Message $message)
67+
{
68+
if ($to = $message->getTo()) {
69+
$data['to'] = array_keys($to);
70+
$data['toname'] = array_values($to);
71+
}
72+
}
73+
/**
74+
* @param $data
75+
* @param Swift_Mime_Message $message
76+
*/
77+
protected function setCc(&$data, Swift_Mime_Message $message)
78+
{
79+
if ($cc = $message->getCc()) {
80+
$data['cc'] = array_keys($cc);
81+
$data['ccname'] = array_values($cc);
82+
}
83+
}
84+
/**
85+
* @param $data
86+
* @param Swift_Mime_Message $message
87+
*/
88+
protected function setBcc(&$data, Swift_Mime_Message $message)
89+
{
90+
if ($bcc = $message->getBcc()) {
91+
$data['bcc'] = array_keys($bcc);
92+
$data['bccname'] = array_values($bcc);
93+
}
94+
}
95+
/**
96+
* Get From Addresses.
97+
*
98+
* @param Swift_Mime_Message $message
99+
* @return array
100+
*/
101+
protected function getFromAddresses(Swift_Mime_Message $message)
102+
{
103+
if ($message->getFrom()) {
104+
foreach ($message->getFrom() as $address => $name) {
105+
return [$address, $name];
106+
}
107+
}
108+
return [];
109+
}
110+
/**
111+
* Set text contents.
112+
*
113+
* @param $data
114+
* @param Swift_Mime_Message $message
115+
*/
116+
protected function setText(&$data, Swift_Mime_Message $message)
117+
{
118+
foreach ($message->getChildren() as $attachment) {
119+
if (!$attachment instanceof Swift_MimePart) {
120+
continue;
121+
}
122+
$data['text'] = $attachment->getBody();
123+
}
124+
}
125+
/**
126+
* Set Attachment Files.
127+
*
128+
* @param $data
129+
* @param Swift_Mime_Message $message
130+
*/
131+
protected function setAttachment(&$data, Swift_Mime_Message $message)
132+
{
133+
foreach ($message->getChildren() as $attachment) {
134+
if (!$attachment instanceof Swift_Attachment || !strlen($attachment->getBody()) > self::MAXIMUM_FILE_SIZE) {
135+
continue;
136+
}
137+
$handler = tmpfile();
138+
fwrite($handler, $attachment->getBody());
139+
$data['files[' . $attachment->getFilename() . ']'] = $handler;
140+
}
141+
}
142+
/**
143+
* Set Sendgrid SMTP API
144+
*
145+
* @param $data
146+
* @param Swift_Mime_Message $message
147+
*/
148+
protected function setSmtpApi(&$data, Swift_Mime_Message $message)
149+
{
150+
foreach ($message->getChildren() as $attachment) {
151+
if (!$attachment instanceof Swift_Image
152+
|| !in_array(self::SMTP_API_NAME, [$attachment->getFilename(), $attachment->getContentType()])
153+
) {
154+
continue;
155+
}
156+
$data['x-smtpapi'] = json_encode($attachment->getBody());
157+
}
158+
}
159+
}

0 commit comments

Comments
 (0)