Skip to content

Commit ea2dde4

Browse files
committed
init commit.
0 parents  commit ea2dde4

File tree

8 files changed

+355
-0
lines changed

8 files changed

+355
-0
lines changed

.editorconfig

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
root = true
2+
3+
[*]
4+
indent_style = space
5+
indent_size = 4
6+
end_of_line = lf
7+
charset = utf-8
8+
trim_trailing_whitespace = true
9+
insert_final_newline = false
10+
11+
[*.{vue,js,scss}]
12+
charset = utf-8
13+
indent_style = space
14+
indent_size = 2
15+
end_of_line = lf
16+
insert_final_newline = true
17+
trim_trailing_whitespace = true
18+
19+
[*.md]
20+
trim_trailing_whitespace = false

.gitattributes

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
* text=auto
2+
3+
/tests export-ignore
4+
.gitattributes export-ignore
5+
.gitignore export-ignore
6+
.scrutinizer.yml export-ignore
7+
.travis.yml export-ignore
8+
phpunit.php export-ignore
9+
phpunit.xml.dist export-ignore
10+
phpunit.xml export-ignore
11+
.php_cs export-ignore

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
*.DS_Store
2+
/vendor
3+
/coverage
4+
sftp-config.json
5+
composer.lock
6+
.subsplit
7+
.php_cs.cache

.php_cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
$header = <<<EOF
3+
This file is part of the overtrue/laravel-mail-aliyun.
4+
5+
(c) overtrue <anzhengchao@gmail.com>
6+
7+
This source file is subject to the MIT license that is bundled.
8+
EOF;
9+
10+
return PhpCsFixer\Config::create()
11+
->setRiskyAllowed(true)
12+
->setRules(array(
13+
'@Symfony' => true,
14+
'header_comment' => array('header' => $header),
15+
'array_syntax' => array('syntax' => 'short'),
16+
'ordered_imports' => true,
17+
'no_useless_else' => true,
18+
'no_useless_return' => true,
19+
'php_unit_construct' => true,
20+
'php_unit_strict' => true,
21+
))
22+
->setFinder(
23+
PhpCsFixer\Finder::create()
24+
->exclude('vendor')
25+
->in(__DIR__)
26+
)
27+
;

README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<p align="center"><h1>Laravel mail aliyun</h1></p>
2+
3+
:e-mail: [Aliyun DrirectMail](https://help.aliyun.com/product/29412.html) Transport for Laravel Application.
4+
5+
## Installing
6+
7+
```shell
8+
$ composer require overtrue/laravel-mail-aliyun -vvv
9+
```
10+
11+
## Configuration
12+
13+
> API documention: https://help.aliyun.com/document_detail/29435.html
14+
15+
```php
16+
// config/services.php
17+
18+
'directmail' => [
19+
'key' => env('ALIYUN_ACCESS_KEY_ID'),
20+
'address_type' => 1,
21+
'from_alias' => null,
22+
'click_trace' => 0',
23+
'version' => '2015-11-23',
24+
'region_id' => null',
25+
],
26+
```
27+
28+
## Usage
29+
30+
Set default mail driver:
31+
32+
```env
33+
MAIL_DRIVER=directmail
34+
```
35+
36+
Please reference the official doc: [Laravel Sending mail](https://laravel.com/docs/5.6/mail#sending-mail)
37+
38+
## License
39+
40+
MIT

composer.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "overtrue/laravel-mail-aliyun",
3+
"description": "Aliyun DrirectMail Transport for Laravel Application.",
4+
"type": "library",
5+
"require": {
6+
"guzzlehttp/guzzle": "^6.3",
7+
"laravel/framework": "5.5",
8+
"swiftmailer/swiftmailer": "^6.1"
9+
},
10+
"license": "MIT",
11+
"authors": [
12+
{
13+
"name": "overtrue",
14+
"email": "anzhengchao@gmail.com"
15+
}
16+
]
17+
}

src/DirectMailServiceProvider.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the overtrue/laravel-mail-aliyun.
5+
*
6+
* (c) overtrue <anzhengchao@gmail.com>
7+
*
8+
* This source file is subject to the MIT license that is bundled.
9+
*/
10+
11+
namespace Overtrue\LaravelMailAliyun;
12+
13+
use GuzzleHttp\Client;
14+
use Illuminate\Support\ServiceProvider;
15+
16+
/**
17+
* Class DirectMailServiceProvider.
18+
*
19+
* @author overtrue <i@overtrue.me>
20+
*/
21+
class DirectMailServiceProvider extends ServiceProvider
22+
{
23+
/**
24+
* Register the service provider.
25+
*/
26+
public function register()
27+
{
28+
$this->app['swift.transport']->extend('directmail', function () {
29+
$config = $this->app['config']->get('services.directmail', []);
30+
31+
return new DirectMailTransport(new Client($config), $config['key'], $config);
32+
});
33+
}
34+
}

src/DirectMailTransport.php

Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the overtrue/laravel-mail-aliyun.
5+
*
6+
* (c) overtrue <anzhengchao@gmail.com>
7+
*
8+
* This source file is subject to the MIT license that is bundled.
9+
*/
10+
11+
namespace Overtrue\LaravelMailAliyun;
12+
13+
use GuzzleHttp\ClientInterface;
14+
use Illuminate\Mail\Transport\Transport;
15+
use Swift_Mime_SimpleMessage;
16+
17+
/**
18+
* Class Transport.
19+
*
20+
* @author overtrue <i@overtrue.me>
21+
*/
22+
class DirectMailTransport extends Transport
23+
{
24+
/**
25+
* Guzzle client instance.
26+
*
27+
* @var \GuzzleHttp\ClientInterface
28+
*/
29+
protected $client;
30+
31+
/**
32+
* @var string
33+
*/
34+
protected $key;
35+
36+
/**
37+
* @var array
38+
*/
39+
protected $options = [];
40+
41+
/**
42+
* @var string
43+
*/
44+
protected $url = 'https://dm.aliyuncs.com/?Action=SingleSendMail';
45+
46+
/**
47+
* Create a new SparkPost transport instance.
48+
*
49+
* @param \GuzzleHttp\ClientInterface $client
50+
* @param string $key
51+
* @param array $options
52+
*/
53+
public function __construct(ClientInterface $client, $key, $options = [])
54+
{
55+
$this->key = $key;
56+
$this->client = $client;
57+
$this->options = $options;
58+
}
59+
60+
/**
61+
* Send the given Message.
62+
*
63+
* Recipient/sender data will be retrieved from the Message API.
64+
* The return value is the number of recipients who were accepted for delivery.
65+
*
66+
* @param Swift_Mime_SimpleMessage $message
67+
* @param string[] $failedRecipients An array of failures by-reference
68+
*
69+
* @return int
70+
*/
71+
public function send(Swift_Mime_SimpleMessage $message, &$failedRecipients = null)
72+
{
73+
$this->beforeSendPerformed($message);
74+
75+
$to = $this->getTo($message);
76+
77+
$message->setBcc([]);
78+
79+
$this->client->post($this->url, $this->payload($message, $to));
80+
81+
$this->sendPerformed($message);
82+
83+
return $this->numberOfRecipients($message);
84+
}
85+
86+
/**
87+
* Get the HTTP payload for sending the Mailgun message.
88+
*
89+
* @param \Swift_Mime_SimpleMessage $message
90+
* @param string $to
91+
*
92+
* @return array
93+
*/
94+
protected function payload(Swift_Mime_SimpleMessage $message, $to)
95+
{
96+
$parameters = [
97+
'form_params' => [
98+
'AccountName' => $message->getFrom(),
99+
'ReplyToAddress' => true,
100+
'AddressType' => array_get($this->options, 'address_type', 1),
101+
'ToAddress' => $this->getTo(),
102+
'FromAlias' => array_get($this->options, 'from_alias'),
103+
'Subject' => $message->getSubject(),
104+
'HtmlBody' => $message->getBody(),
105+
'ClickTrace' => array_get($this->options, 'click_trace', 0),
106+
'Format' => 'json',
107+
'Version' => array_get($this->options, 'version', '2015-11-23'),
108+
'AccessKeyId' => $this->getKey(),
109+
'Timestamp' => date('Y-m-d\TH:i:s\Z'),
110+
'SignatureMethod' => 'HMAC-SHA1',
111+
'SignatureVersion' => '1.0',
112+
'SignatureNonce' => \uniqid(),
113+
'RegionId' => \array_get($this->options, 'region_id'),
114+
],
115+
];
116+
117+
$parameters['Signature'] = $this->makeSignature($parameters);
118+
119+
return $parameters;
120+
}
121+
122+
/**
123+
* @param array $parameters
124+
*
125+
* @return string
126+
*/
127+
protected function makeSignature(array $parameters)
128+
{
129+
\ksort($parameters);
130+
131+
$signString = rawurlencode('POST&/&'.http_build_query($parameters, null, '&', PHP_QUERY_RFC3986));
132+
133+
return base64_encode(hash_hmac('sha1', $signString, $this->getKey(), true));
134+
}
135+
136+
/**
137+
* Get the "to" payload field for the API request.
138+
*
139+
* @param \Swift_Mime_SimpleMessage $message
140+
*
141+
* @return string
142+
*/
143+
protected function getTo(Swift_Mime_SimpleMessage $message)
144+
{
145+
return collect($this->allContacts($message))->map(function ($display, $address) {
146+
return $display ? $display." <{$address}>" : $address;
147+
})->values()->implode(',');
148+
}
149+
150+
/**
151+
* Get the transmission ID from the response.
152+
*
153+
* @param \GuzzleHttp\Psr7\Response $response
154+
*
155+
* @return string
156+
*/
157+
protected function getTransmissionId($response)
158+
{
159+
return object_get(
160+
json_decode($response->getBody()->getContents()), 'RequestId'
161+
);
162+
}
163+
164+
/**
165+
* Get all of the contacts for the message.
166+
*
167+
* @param \Swift_Mime_SimpleMessage $message
168+
*
169+
* @return array
170+
*/
171+
protected function allContacts(Swift_Mime_SimpleMessage $message)
172+
{
173+
return array_merge(
174+
(array) $message->getTo(), (array) $message->getCc(), (array) $message->getBcc()
175+
);
176+
}
177+
178+
/**
179+
* Get the API key being used by the transport.
180+
*
181+
* @return string
182+
*/
183+
public function getKey()
184+
{
185+
return $this->key;
186+
}
187+
188+
/**
189+
* Set the API key being used by the transport.
190+
*
191+
* @param string $key
192+
*
193+
* @return string
194+
*/
195+
public function setKey($key)
196+
{
197+
return $this->key = $key;
198+
}
199+
}

0 commit comments

Comments
 (0)