Skip to content

Commit

Permalink
Merge pull request #265 from php-mime-mail-parser/issue-263
Browse files Browse the repository at this point in the history
Fix issue #263
  • Loading branch information
eXorus committed Sep 23, 2019
2 parents 7d4be05 + 2457931 commit 2798343
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/Attachment.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace PhpMimeMailParser;

use function var_dump;

/**
* Attachment of php-mime-mail-parser
*
Expand Down Expand Up @@ -237,7 +239,9 @@ public function save(
// Determine filename
switch ($filenameStrategy) {
case Parser::ATTACHMENT_RANDOM_FILENAME:
$attachment_path = tempnam($attach_dir, '');
$fileInfo = pathinfo($this->getFilename());
$extension = empty($fileInfo['extension']) ? '' : '.'.$fileInfo['extension'];
$attachment_path = $attach_dir.uniqid().$extension;
break;
case Parser::ATTACHMENT_DUPLICATE_THROW:
case Parser::ATTACHMENT_DUPLICATE_SUFFIX:
Expand Down
22 changes: 22 additions & 0 deletions tests/AttachmentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,26 @@ public function testGeneratingDuplicateSuffix()
array_map('unlink', $attachmentFiles);
rmdir($attachDir);
}

public function testSavingWithRandomFilenameKeepExtension()
{
$file = __DIR__ . '/mails/m0025';
$Parser = new Parser();
$Parser->setPath($file);

$attachDir = __DIR__ . '/mails/m0025_attachments/';
$Parser->saveAttachments($attachDir, true, $Parser::ATTACHMENT_RANDOM_FILENAME);

$attachmentFiles = glob($attachDir . '*');
$attachmentJpgFiles = glob($attachDir . '*.jpg');
$attachmentTxtFiles = glob($attachDir . '*.txt');

// Clean up attachments dir
array_map('unlink', $attachmentFiles);
rmdir($attachDir);

$this->assertCount(3, $attachmentFiles);
$this->assertCount(2, $attachmentJpgFiles);
$this->assertCount(1, $attachmentTxtFiles);
}
}

0 comments on commit 2798343

Please sign in to comment.