diff --git a/README.md b/README.md index 17e25d7..5db60b9 100644 --- a/README.md +++ b/README.md @@ -163,6 +163,18 @@ Example: * Param $email * Param $text +### grabAttachmentsFromLastEmail + +Grab Attachments From Email + +Returns array with the format [ [filename1 => bytes1], [filename2 => bytes2], ...] + +Example: + + grabAttachmentsFromLastEmail(); + ?> + ### grabMatchesFromLastEmail Extracts an array of matches and sub-matches from the last email based on diff --git a/src/Module/MailCatcher.php b/src/Module/MailCatcher.php index f6a2041..71c622c 100644 --- a/src/Module/MailCatcher.php +++ b/src/Module/MailCatcher.php @@ -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 + */ + 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 * diff --git a/tests/acceptance/MailcatcherCest.php b/tests/acceptance/MailcatcherCest.php index 1d60ba9..c93bbd3 100644 --- a/tests/acceptance/MailcatcherCest.php +++ b/tests/acceptance/MailcatcherCest.php @@ -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 = "user@example.com"; + + $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 */