Skip to content

Commit

Permalink
Merge pull request #34 from narendravaghela/4.x-add-reply
Browse files Browse the repository at this point in the history
Forward porting #33
  • Loading branch information
challgren authored Mar 31, 2020
2 parents 62d1439 + cc46b67 commit 59ea142
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 12 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
/.idea
/.phpunit.result.cache
/coverage
/clover.xml
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 @@ -45,7 +45,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.#'
14 changes: 11 additions & 3 deletions src/Mailer/Transport/MailgunTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,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 @@ -191,7 +191,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 @@ -355,6 +355,10 @@ protected function _prepareEmailAddresses(Email $email)
$this->_formData->add('from', sprintf("%s <%s>", key($from), key($from)));
}

foreach ($email->getSender() as $toEmail => $toName) {
$this->_formData->add('h:Sender', sprintf("%s <%s>", $toName, $toEmail));
}

foreach ($email->getTo() as $toEmail => $toName) {
$this->_formData->add('to', sprintf("%s <%s>", $toName, $toEmail));
}
Expand All @@ -366,6 +370,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 Down
33 changes: 26 additions & 7 deletions tests/TestCase/Mailer/Transport/MailgunTransportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,9 @@ public function testAdditionalEmailAddresses()
->setTo('[email protected]')
->addCC(['[email protected]', '[email protected]' => 'John'])
->addBcc(['[email protected]', '[email protected]' => 'John'])
->setReplyTo(['[email protected]' => 'John'])
->setSender(['[email protected]' => 'John'])
->setEmailFormat('both')
->addHeaders([
'h:Reply-To' => '[email protected]',
'Sent-From' => '[email protected]',
])
->setSubject('Email from CakePHP Mailgun plugin')
->send('Hello there, <br> This is an email from CakePHP Mailgun Email plugin.');

Expand All @@ -111,15 +109,15 @@ public function testAdditionalEmailAddresses()
$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('Content-Disposition: form-data; name="h:Sent-From"', $reqDataString);
$this->assertTextContains('Content-Disposition: form-data; name="h:Sender"', $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('[email protected]', $reqDataString);
$this->assertTextContains('[email protected]', $reqDataString);
$this->assertTextContains('John <[email protected]>', $reqDataString);
$this->assertTextContains('John <[email protected]>', $reqDataString);
}

public function testAttachments()
Expand All @@ -145,6 +143,27 @@ public function testAttachments()
$this->assertStringEndsWith("--$boundary--", rtrim($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 testRawBinaryAttachments()
{
$this->_setEmailConfig();
Expand Down

0 comments on commit 59ea142

Please sign in to comment.