Skip to content

Commit

Permalink
Fixed #2
Browse files Browse the repository at this point in the history
  • Loading branch information
overtrue committed Jan 24, 2019
1 parent 8f968c2 commit 5f5293a
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 44 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,26 @@ $ composer require overtrue/laravel-mail-aliyun -vvv

> API documention: https://help.aliyun.com/document_detail/29435.html
*config/services.php*
```php
// config/services.php

'directmail' => [
'key' => env('ALIYUN_ACCESS_KEY_ID'),
'address_type' => 1,
'from_alias' => null,
'click_trace' => 0,
'version' => '2015-11-23',
'region_id' => null,
'secret' => env('ALIYUN_ACCESS_KEY_SECRET'),
],
```

AccessKeyID 和 AccessKeySecret 由阿里云官方颁发给用户的 AccessKey 信息(可以通过阿里云控制台[用户信息管理](https://usercenter.console.aliyun.com/?spm=a2c4g.11186623.2.17.12f2461dHSyXbw#/manage/ak)中查看和管理).

## Usage

Set default mail driver:

```env
//.env
MAIL_DRIVER=directmail
ALIYUN_ACCESS_KEY_ID= #AccessKeyID
ALIYUN_ACCESS_KEY_SECRET= #AccessKeySecret
```

Please reference the official doc: [Laravel Sending mail](https://laravel.com/docs/5.6/mail#sending-mail)
Expand Down
7 changes: 2 additions & 5 deletions src/DirectMailServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,12 @@
*/
class DirectMailServiceProvider extends ServiceProvider
{
/**
* Register the service provider.
*/
public function register()
public function boot()
{
$this->app['swift.transport']->extend('directmail', function () {
$config = $this->app['config']->get('services.directmail', []);

return new DirectMailTransport(new Client($config), $config['key'], $config);
return new DirectMailTransport(new Client($config), $config['key'], $config['secret'], $config);
});
}
}
113 changes: 81 additions & 32 deletions src/DirectMailTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ class DirectMailTransport extends Transport
*/
protected $key;

/**
* @var string
*/
protected $secret;

/**
* @var array
*/
Expand All @@ -41,18 +46,36 @@ class DirectMailTransport extends Transport
/**
* @var string
*/
protected $url = 'https://dm.aliyuncs.com/?Action=SingleSendMail';
protected $regons = [
'cn-hangzhou' => [
'id' => 'cn-hangzhou',
'url' => 'https://dm.aliyuncs.com',
'version' => '2015-11-23',
],
'ap-southeast-1' => [
'id' => 'ap-southeast-1',
'url' => 'https://dm.ap-southeast-1.aliyuncs.com',
'version' => '2017-06-22',
],
'ap-southeast-2' => [
'id' => 'ap-southeast-2',
'url' => 'https://dm.ap-southeast-2.aliyuncs.com',
'version' => '2017-06-22',
],
];

/**
* Create a new SparkPost transport instance.
* DirectMailTransport constructor.
*
* @param \GuzzleHttp\ClientInterface $client
* @param string $key
* @param string $secret
* @param array $options
*/
public function __construct(ClientInterface $client, $key, $options = [])
public function __construct(ClientInterface $client, string $key, string $secret, array $options = [])
{
$this->key = $key;
$this->secret = $secret;
$this->client = $client;
$this->options = $options;
}
Expand All @@ -72,47 +95,47 @@ public function send(Swift_Mime_SimpleMessage $message, &$failedRecipients = nul
{
$this->beforeSendPerformed($message);

$to = $this->getTo($message);

$message->setBcc([]);

$this->client->post($this->url, $this->payload($message, $to));
$regionId = \array_get($this->options, 'region_id', 'cn-hangzhou');
$region = $this->regons[$regionId];

$this->client->post($region['url'], ['form_params' => $this->payload($message, $region)]);

$this->sendPerformed($message);

return $this->numberOfRecipients($message);
}

/**
* Get the HTTP payload for sending the Mailgun message.
* Get the HTTP payload for sending the message.
*
* @param \Swift_Mime_SimpleMessage $message
* @param string $to
* @param array $region
*
* @return array
*/
protected function payload(Swift_Mime_SimpleMessage $message, $to)
protected function payload(Swift_Mime_SimpleMessage $message, array $region)
{
$parameters = [
'form_params' => [
'AccountName' => $message->getFrom(),
'ReplyToAddress' => true,
'AddressType' => array_get($this->options, 'address_type', 1),
'ToAddress' => $this->getTo(),
'FromAlias' => array_get($this->options, 'from_alias'),
'Subject' => $message->getSubject(),
'HtmlBody' => $message->getBody(),
'ClickTrace' => array_get($this->options, 'click_trace', 0),
'Format' => 'json',
'Version' => array_get($this->options, 'version', '2015-11-23'),
'AccessKeyId' => $this->getKey(),
'Timestamp' => date('Y-m-d\TH:i:s\Z'),
'SignatureMethod' => 'HMAC-SHA1',
'SignatureVersion' => '1.0',
'SignatureNonce' => \uniqid(),
'RegionId' => \array_get($this->options, 'region_id'),
],
];
$parameters = array_filter([
'AccountName' => array_get($this->options, 'from_address', \config('mail.from.address', key($message->getFrom()))),
'ReplyToAddress' => 'true',
'AddressType' => array_get($this->options, 'address_type', 1),
'ToAddress' => $this->getTo($message),
'FromAlias' => array_get($this->options, 'from_alias'),
'Subject' => $message->getSubject(),
'HtmlBody' => $message->getBody(),
'ClickTrace' => array_get($this->options, 'click_trace', 0),
'Format' => 'json',
'Action' => 'SingleSendMail',
'Version' => $region['version'],
'AccessKeyId' => $this->getKey(),
'Timestamp' => date('Y-m-d\TH:i:s\Z'),
'SignatureMethod' => 'HMAC-SHA1',
'SignatureVersion' => '1.0',
'SignatureNonce' => \uniqid(),
'RegionId' => $region['id'],
]);

$parameters['Signature'] = $this->makeSignature($parameters);

Expand All @@ -128,9 +151,15 @@ protected function makeSignature(array $parameters)
{
\ksort($parameters);

$signString = rawurlencode('POST&/&'.http_build_query($parameters, null, '&', PHP_QUERY_RFC3986));
$encoded = [];

return base64_encode(hash_hmac('sha1', $signString, $this->getKey(), true));
foreach ($parameters as $key => $value) {
$encoded[] = \sprintf('%s=%s', rawurlencode($key), rawurlencode($value));
}

$signString = 'POST&%2F&'.rawurlencode(\join('&', $encoded));

return base64_encode(hash_hmac('sha1', $signString, $this->getSecret().'&', true));
}

/**
Expand Down Expand Up @@ -185,15 +214,35 @@ public function getKey()
return $this->key;
}

/**
* Get the API key being used by the transport.
*
* @return string
*/
public function getSecret()
{
return $this->secret;
}

/**
* Set the API key being used by the transport.
*
* @param string $key
*
* @return string
*/
public function setKey($key)
public function setKey(string $key)
{
return $this->key = $key;
}

/**
* Get the API key being used by the transport.
*
* @return string
*/
public function setSecret(string $secret)
{
return $this->secret = $secret;
}
}

0 comments on commit 5f5293a

Please sign in to comment.