Skip to content

Commit c4fcdd5

Browse files
authored
Merge pull request #5 from eodin/master
grabBodyFromEmail(): allow grab email MIME-part with Content-part alternatives
2 parents bb7b669 + 0730c01 commit c4fcdd5

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,14 @@ Returns the string containing all of the recipients, such as To, CC and if provi
231231
grabBodyFromEmail
232232
```
233233
Returns the email body as string
234+
```
235+
$email_body = $I->grabBodyFromEmail('text/html');
236+
$doc = new DOMDocument();
237+
$doc->loadHTML($email_body);
238+
$login = $doc->getElementById('login')->textContent;
239+
$password = $doc->getElementById('password')->textContent;
240+
```
241+
Fetch login & password from 'text/html' email MIME-part.
234242

235243
### To Do
236244
While this framework should be sufficient for most email testing scenarios, the remaining features need to be implemented
@@ -265,5 +273,5 @@ THE SOFTWARE.
265273
[MailHogModule]: http://github.com/ericmartel/codeception-email-mailhog
266274
[MailCatcherModule]: http://github.com/ericmartel/codeception-email-mailcatcher
267275
[MailtrapModule]: http://github.com/ericmartel/codeception-email-mailtrap
268-
276+
269277

src/TestsEmails.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -563,11 +563,23 @@ public function dontSeeInEmailPriority($email, $expected)
563563
}
564564

565565
/**
566+
567+
* @param string $content_type_alternative MIME-part Content-Type
566568
* @return string Body
567569
*/
568-
public function grabBodyFromEmail()
570+
public function grabBodyFromEmail($content_type_alternative = null)
569571
{
570572
$email = $this->getOpenedEmail();
571-
return $this->getDecodedEmailProperty($email, $email->Content->Body);
573+
574+
if( isset($content_type_alternative) ) {
575+
foreach ($email->MIME->Parts as $part) {
576+
if (!empty($part->Headers->{'Content-Type'}[0]) &&
577+
strpos($part->Headers->{'Content-Type'}[0], $content_type_alternative) !== false) {
578+
return $this->getDecodedEmailProperty($part, $part->Body); //return first entry
579+
}
580+
}
581+
return null; //not found
582+
}
583+
return $this->getDecodedEmailProperty($email, $email->Content->Body); //return full email
572584
}
573585
};

0 commit comments

Comments
 (0)