Skip to content

Commit

Permalink
Merge pull request #33 from kuldeepkhichar/patch-1
Browse files Browse the repository at this point in the history
Updated plugin to support replyTo
  • Loading branch information
challgren authored Mar 31, 2020
2 parents 2c2c6bf + 4a4bcaa commit ce3dc0f
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
8 changes: 8 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -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.#'
3 changes: 3 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
<php>
<ini name="memory_limit" value="-1"/>
<ini name="apc.enable_cli" value="1"/>
<!-- E_ALL => 32767 -->
<!-- E_ALL & ~E_USER_DEPRECATED => 16383 -->
<ini name="error_reporting" value="16383"/>
</php>

<!-- Add any additional test suites you want to run here -->
Expand Down
16 changes: 10 additions & 6 deletions src/Mailer/Transport/MailgunTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class MailgunTransport extends AbstractTransport
protected $_defaultConfig = [
'apiEndpoint' => 'https://api.mailgun.net/v3',
'domain' => '',
'apiKey' => ''
'apiKey' => '',
];

/**
Expand All @@ -54,7 +54,7 @@ class MailgunTransport extends AbstractTransport
'tracking-clicks',
'tracking-opens',
'require-tls',
'skip-verification'
'skip-verification',
];

/**
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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()
{
Expand Down Expand Up @@ -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));
}
}

/**
Expand All @@ -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();
Expand Down
26 changes: 25 additions & 1 deletion tests/TestCase/Mailer/Transport/MailgunTransportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ public function testAdditionalEmailAddresses()
->setTo('[email protected]')
->addCC(['[email protected]', '[email protected]' => 'John'])
->addBcc(['[email protected]', '[email protected]' => 'John'])
->setReplyTo(['[email protected]' => 'John'])
->setEmailFormat('both')
->setSubject('Email from CakePHP Mailgun plugin')
->send('Hello there, <br> This is an email from CakePHP Mailgun Email plugin.');
Expand All @@ -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('[email protected] <[email protected]>', $reqDataString);
$this->assertTextContains('[email protected] <[email protected]>', $reqDataString);
$this->assertTextContains('[email protected] <[email protected]>', $reqDataString);
$this->assertTextContains('John <[email protected]>', $reqDataString);
$this->assertTextContains('[email protected] <[email protected]>', $reqDataString);
$this->assertTextContains('John <[email protected]>', $reqDataString);
$this->assertTextContains('John <[email protected]>', $reqDataString);
}

public function testCustomHeaders()
{
$this->_setEmailConfig();
$email = new Email();
$email->setProfile(['transport' => 'mailgun']);
$res = $email->setFrom('[email protected]')
->setTo('[email protected]')
->setHeaders(['h:X-MyHeader' => 'YouGotIt'])
->setSubject('Email from CakePHP Mailgun plugin')
->send('Hello there, <br> 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()
Expand All @@ -116,7 +140,7 @@ public function testAttachments()
->setTo('[email protected]')
->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, <br> This is an email from CakePHP Mailgun Email plugin.');
Expand Down

0 comments on commit ce3dc0f

Please sign in to comment.