Skip to content

Commit

Permalink
Add grab attachments from last mail
Browse files Browse the repository at this point in the history
  • Loading branch information
margori committed Jul 20, 2020
1 parent 5feb2c3 commit 86f2444
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,18 @@ Example:
* Param $email
* Param $text

### grabAttachmentsFromLastEmail

Grab Attachments From Email

Returns array with the format [ [filename1 => bytes1], [filename2 => bytes2], ...]

Example:

<?php
$attachments = $I->grabAttachmentsFromLastEmail();
?>

### grabMatchesFromLastEmail

Extracts an array of matches and sub-matches from the last email based on
Expand Down
25 changes: 25 additions & 0 deletions src/Module/MailCatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,31 @@ public function grabUrlsFromLastEmail()
return $results;
}

/**
* Grab Attachments From Email
*
* Returns array with the format [ [filename1 => bytes1], [filename2 => bytes2], ...]
*
* @return array
* @author Marcelo Briones <[email protected]>
*/
public function grabAttachmentsFromLastEmail()
{
$email = $this->lastMessage();

$message = Message::from($email->getSource());

$attachments = [];

foreach ($message->getAllAttachmentParts() as $attachmentPart) {
$filename = $attachmentPart->getFilename();
$content = $attachmentPart->getContent();
$attachments[$filename] = $content;
}

return $attachments;
}

/**
* See Attachment In Last Email
*
Expand Down
19 changes: 19 additions & 0 deletions tests/acceptance/MailcatcherCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,25 @@ public function test_grab_urls_from_last_email_with_encoding(
$I->assertEquals($example[0], $urls[0]);
}

/**
* @param AcceptanceTester $I
*/
public function test_grab_attachments_from_last(AcceptanceTester $I)
{
$user = "[email protected]";

$attachments = [
"image.jpg" => codecept_data_dir('image.jpg'),
"lorem.txt" => codecept_data_dir('lorem.txt'),
"compressed.zip" => codecept_data_dir('compressed.zip'),
];

$I->sendEmail($user, 'Email with attachments', "I have attachments.", null, $attachments);
$grabbedAttachments = $I->grabAttachmentsFromLastEmail();

$I->assertEquals(3, count($grabbedAttachments));
}

/**
* @param AcceptanceTester $I
*/
Expand Down

0 comments on commit 86f2444

Please sign in to comment.