diff --git a/.travis.yml b/.travis.yml index 2f3f780..aa3aaac 100644 --- a/.travis.yml +++ b/.travis.yml @@ -31,7 +31,7 @@ before_script: script: - sh -c "if [ '$DEFAULT' = '1' ]; then composer test; fi" - - sh -c "if [ '$PHPSTAN' = '1' ]; then composer require --dev "phpstan/phpstan:^0.10" && composer phpstan; fi" + - sh -c "if [ '$PHPSTAN' = '1' ]; then composer require --dev "phpstan/phpstan:^0.12" && composer phpstan; fi" - sh -c "if [ '$PHPCS' = '1' ]; then composer cs-check; fi" - sh -c "if [ '$CODECOVERAGE' = '1' ]; then phpdbg -qrr vendor/bin/phpunit --coverage-clover=clover.xml || true; fi" diff --git a/composer.json b/composer.json index 23a3430..05d11f5 100644 --- a/composer.json +++ b/composer.json @@ -44,7 +44,7 @@ ], "cs-check": "phpcs --colors -p --standard=vendor/cakephp/cakephp-codesniffer/CakePHP src/ tests/", "cs-fix": "phpcbf --colors --standard=vendor/cakephp/cakephp-codesniffer/CakePHP src/ tests/", - "phpstan": "phpstan analyze -l 1 src/", + "phpstan": "phpstan analyze src/", "test": "phpunit --colors=always" } } diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 0000000..d39df6a --- /dev/null +++ b/phpstan.neon @@ -0,0 +1,8 @@ +parameters: + level: 7 + checkMissingIterableValueType: false + autoload_files: + - tests/bootstrap.php + ignoreErrors: + - '#Parameter \#1 \$str of function trim expects string, array\|string given.#' + - '#Result of \|\| is always true.#' diff --git a/phpunit.xml.dist b/phpunit.xml.dist index cf3edac..e9f2bb2 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -9,6 +9,9 @@ + + + diff --git a/src/Mailer/Transport/MailgunTransport.php b/src/Mailer/Transport/MailgunTransport.php index 41333b6..7a0eac6 100644 --- a/src/Mailer/Transport/MailgunTransport.php +++ b/src/Mailer/Transport/MailgunTransport.php @@ -37,7 +37,7 @@ class MailgunTransport extends AbstractTransport protected $_defaultConfig = [ 'apiEndpoint' => 'https://api.mailgun.net/v3', 'domain' => '', - 'apiKey' => '' + 'apiKey' => '', ]; /** @@ -54,7 +54,7 @@ class MailgunTransport extends AbstractTransport 'tracking-clicks', 'tracking-opens', 'require-tls', - 'skip-verification' + 'skip-verification', ]; /** @@ -135,9 +135,9 @@ public function send(Email $email) if (!empty($attachments)) { foreach ($attachments as $fileName => $attachment) { if (empty($attachment['contentId'])) { - $file = $this->_addFile('attachment', $attachment, $fileName); + $file = $this->_addFile('attachment', $attachment, (string)$fileName); } else { - $file = $this->_addFile('inline', $attachment, $fileName); + $file = $this->_addFile('inline', $attachment, (string)$fileName); $file->contentId($attachment['contentId']); } $file->disposition('attachment'); @@ -181,7 +181,7 @@ protected function _addFile($partName, $attachment, $fileName = '') /** * Returns the parameters for API request. * - * @return array + * @return \Cake\Http\Client\FormData */ public function getRequestData() { @@ -344,6 +344,10 @@ protected function _prepareEmailAddresses(Email $email) foreach ($email->getBcc() as $bccEmail => $bccName) { $this->_formData->add('bcc', sprintf("%s <%s>", $bccName, $bccEmail)); } + + foreach ($email->getReplyTo() as $replyToEmail => $replyToName) { + $this->_formData->add('h:Reply-To', sprintf("%s <%s>", $replyToName, $replyToEmail)); + } } /** @@ -356,7 +360,7 @@ protected function _sendEmail() $http = new Client(); $response = $http->post("{$this->getConfig('apiEndpoint')}/{$this->getConfig('domain')}/messages", (string)$this->_formData, [ 'auth' => ['username' => 'api', 'password' => $this->getConfig('apiKey')], - 'headers' => ['Content-Type' => $this->_formData->contentType()] + 'headers' => ['Content-Type' => $this->_formData->contentType()], ]); return $response->getJson(); diff --git a/tests/TestCase/Mailer/Transport/MailgunTransportTest.php b/tests/TestCase/Mailer/Transport/MailgunTransportTest.php index b527dc9..5b49a4f 100644 --- a/tests/TestCase/Mailer/Transport/MailgunTransportTest.php +++ b/tests/TestCase/Mailer/Transport/MailgunTransportTest.php @@ -85,6 +85,7 @@ public function testAdditionalEmailAddresses() ->setTo('to@example.com') ->addCC(['ccbar@example.com', 'ccjohn@example.com' => 'John']) ->addBcc(['bccbar@example.com', 'bccjohn@example.com' => 'John']) + ->setReplyTo(['replyto@example.com' => 'John']) ->setEmailFormat('both') ->setSubject('Email from CakePHP Mailgun plugin') ->send('Hello there,
This is an email from CakePHP Mailgun Email plugin.'); @@ -99,12 +100,35 @@ public function testAdditionalEmailAddresses() $this->assertTextContains('Content-Disposition: form-data; name="to"', $reqDataString); $this->assertTextContains('Content-Disposition: form-data; name="cc"', $reqDataString); $this->assertTextContains('Content-Disposition: form-data; name="bcc"', $reqDataString); + $this->assertTextContains('Content-Disposition: form-data; name="h:Reply-To"', $reqDataString); $this->assertTextContains('from@example.com ', $reqDataString); $this->assertTextContains('to@example.com ', $reqDataString); $this->assertTextContains('ccbar@example.com ', $reqDataString); $this->assertTextContains('John ', $reqDataString); $this->assertTextContains('bccbar@example.com ', $reqDataString); $this->assertTextContains('John ', $reqDataString); + $this->assertTextContains('John ', $reqDataString); + } + + public function testCustomHeaders() + { + $this->_setEmailConfig(); + $email = new Email(); + $email->setProfile(['transport' => 'mailgun']); + $res = $email->setFrom('from@example.com') + ->setTo('to@example.com') + ->setHeaders(['h:X-MyHeader' => 'YouGotIt']) + ->setSubject('Email from CakePHP Mailgun plugin') + ->send('Hello there,
This is an email from CakePHP Mailgun Email plugin.'); + + $reqData = $res['reqData']; + $boundary = $reqData->boundary(); + $reqDataString = (string)$reqData; + + $this->assertNotEmpty($reqDataString); + $this->assertStringStartsWith("--$boundary", $reqDataString); + $this->assertTextContains('Content-Disposition: form-data; name="h:X-MyHeader"', $reqDataString); + $this->assertTextContains('YouGotIt', $reqDataString); } public function testAttachments() @@ -116,7 +140,7 @@ public function testAttachments() ->setTo('to@example.com') ->setAttachments([ 'logo.png' => ['file' => TESTS . DS . 'assets' . DS . 'logo.png', 'contentId' => 'logo.png'], - 'cake.power.gif' => ['file' => TESTS . DS . 'assets' . DS . 'cake.power.gif'] + 'cake.power.gif' => ['file' => TESTS . DS . 'assets' . DS . 'cake.power.gif'], ]) ->setSubject('Email from CakePHP Mailgun plugin') ->send('Hello there,
This is an email from CakePHP Mailgun Email plugin.');