|
| 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