@@ -33,6 +33,11 @@ class DirectMailTransport extends Transport
3333 */
3434 protected $ key ;
3535
36+ /**
37+ * @var string
38+ */
39+ protected $ secret ;
40+
3641 /**
3742 * @var array
3843 */
@@ -41,18 +46,36 @@ class DirectMailTransport extends Transport
4146 /**
4247 * @var string
4348 */
44- protected $ url = 'https://dm.aliyuncs.com/?Action=SingleSendMail ' ;
49+ protected $ regons = [
50+ 'cn-hangzhou ' => [
51+ 'id ' => 'cn-hangzhou ' ,
52+ 'url ' => 'https://dm.aliyuncs.com ' ,
53+ 'version ' => '2015-11-23 ' ,
54+ ],
55+ 'ap-southeast-1 ' => [
56+ 'id ' => 'ap-southeast-1 ' ,
57+ 'url ' => 'https://dm.ap-southeast-1.aliyuncs.com ' ,
58+ 'version ' => '2017-06-22 ' ,
59+ ],
60+ 'ap-southeast-2 ' => [
61+ 'id ' => 'ap-southeast-2 ' ,
62+ 'url ' => 'https://dm.ap-southeast-2.aliyuncs.com ' ,
63+ 'version ' => '2017-06-22 ' ,
64+ ],
65+ ];
4566
4667 /**
47- * Create a new SparkPost transport instance .
68+ * DirectMailTransport constructor .
4869 *
4970 * @param \GuzzleHttp\ClientInterface $client
5071 * @param string $key
72+ * @param string $secret
5173 * @param array $options
5274 */
53- public function __construct (ClientInterface $ client , $ key , $ options = [])
75+ public function __construct (ClientInterface $ client , string $ key , string $ secret , array $ options = [])
5476 {
5577 $ this ->key = $ key ;
78+ $ this ->secret = $ secret ;
5679 $ this ->client = $ client ;
5780 $ this ->options = $ options ;
5881 }
@@ -72,47 +95,47 @@ public function send(Swift_Mime_SimpleMessage $message, &$failedRecipients = nul
7295 {
7396 $ this ->beforeSendPerformed ($ message );
7497
75- $ to = $ this ->getTo ($ message );
76-
7798 $ message ->setBcc ([]);
7899
79- $ this ->client ->post ($ this ->url , $ this ->payload ($ message , $ to ));
100+ $ regionId = \array_get ($ this ->options , 'region_id ' , 'cn-hangzhou ' );
101+ $ region = $ this ->regons [$ regionId ];
102+
103+ $ this ->client ->post ($ region ['url ' ], ['form_params ' => $ this ->payload ($ message , $ region )]);
80104
81105 $ this ->sendPerformed ($ message );
82106
83107 return $ this ->numberOfRecipients ($ message );
84108 }
85109
86110 /**
87- * Get the HTTP payload for sending the Mailgun message.
111+ * Get the HTTP payload for sending the message.
88112 *
89113 * @param \Swift_Mime_SimpleMessage $message
90- * @param string $to
114+ * @param array $region
91115 *
92116 * @return array
93117 */
94- protected function payload (Swift_Mime_SimpleMessage $ message , $ to )
118+ protected function payload (Swift_Mime_SimpleMessage $ message , array $ region )
95119 {
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- ];
120+ $ parameters = array_filter ([
121+ 'AccountName ' => array_get ($ this ->options , 'from_address ' , \config ('mail.from.address ' , key ($ message ->getFrom ()))),
122+ 'ReplyToAddress ' => 'true ' ,
123+ 'AddressType ' => array_get ($ this ->options , 'address_type ' , 1 ),
124+ 'ToAddress ' => $ this ->getTo ($ message ),
125+ 'FromAlias ' => array_get ($ this ->options , 'from_alias ' ),
126+ 'Subject ' => $ message ->getSubject (),
127+ 'HtmlBody ' => $ message ->getBody (),
128+ 'ClickTrace ' => array_get ($ this ->options , 'click_trace ' , 0 ),
129+ 'Format ' => 'json ' ,
130+ 'Action ' => 'SingleSendMail ' ,
131+ 'Version ' => $ region ['version ' ],
132+ 'AccessKeyId ' => $ this ->getKey (),
133+ 'Timestamp ' => date ('Y-m-d\TH:i:s\Z ' ),
134+ 'SignatureMethod ' => 'HMAC-SHA1 ' ,
135+ 'SignatureVersion ' => '1.0 ' ,
136+ 'SignatureNonce ' => \uniqid (),
137+ 'RegionId ' => $ region ['id ' ],
138+ ]);
116139
117140 $ parameters ['Signature ' ] = $ this ->makeSignature ($ parameters );
118141
@@ -128,9 +151,15 @@ protected function makeSignature(array $parameters)
128151 {
129152 \ksort ($ parameters );
130153
131- $ signString = rawurlencode ( ' POST&/& ' . http_build_query ( $ parameters , null , ' & ' , PHP_QUERY_RFC3986 )) ;
154+ $ encoded = [] ;
132155
133- return base64_encode (hash_hmac ('sha1 ' , $ signString , $ this ->getKey (), true ));
156+ foreach ($ parameters as $ key => $ value ) {
157+ $ encoded [] = \sprintf ('%s=%s ' , rawurlencode ($ key ), rawurlencode ($ value ));
158+ }
159+
160+ $ signString = 'POST&%2F& ' .rawurlencode (\join ('& ' , $ encoded ));
161+
162+ return base64_encode (hash_hmac ('sha1 ' , $ signString , $ this ->getSecret ().'& ' , true ));
134163 }
135164
136165 /**
@@ -185,15 +214,35 @@ public function getKey()
185214 return $ this ->key ;
186215 }
187216
217+ /**
218+ * Get the API key being used by the transport.
219+ *
220+ * @return string
221+ */
222+ public function getSecret ()
223+ {
224+ return $ this ->secret ;
225+ }
226+
188227 /**
189228 * Set the API key being used by the transport.
190229 *
191230 * @param string $key
192231 *
193232 * @return string
194233 */
195- public function setKey ($ key )
234+ public function setKey (string $ key )
196235 {
197236 return $ this ->key = $ key ;
198237 }
238+
239+ /**
240+ * Get the API key being used by the transport.
241+ *
242+ * @return string
243+ */
244+ public function setSecret (string $ secret )
245+ {
246+ return $ this ->secret = $ secret ;
247+ }
199248}
0 commit comments