-
Notifications
You must be signed in to change notification settings - Fork 48
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added ability to extract text and html from multipart messages #157
Merged
roelvanduijnhoven
merged 4 commits into
Webador:master
from
kynx:extract-from-multipart-messages
Nov 17, 2023
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
2df9632
Added ability to extract text and html from multipart messages
mattkynaston 18ad01a
Require laminas/laminas-mime:^2.8 so we have access to Mime class con…
mattkynaston 3ea43bd
Update README with notes on setting boundary on multipart/alternative…
mattkynaston 394177f
Link to Laminas documentation for multipart/alternative emails now ht…
mattkynaston File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,179 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SlmMailTest\Service; | ||
|
||
use Laminas\Mail\Message; | ||
use Laminas\Mime\Message as MimeMessage; | ||
use Laminas\Mime\Mime; | ||
use Laminas\Mime\Part; | ||
use SlmMail\Service\AbstractMailService; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
/** | ||
* @covers \SlmMail\Service\AbstractMailService | ||
*/ | ||
final class AbstractMailServiceTest extends TestCase | ||
{ | ||
private AbstractMailService $service; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->service = new class () extends AbstractMailService { | ||
public ?string $text = null; | ||
public ?string $html = null; | ||
public array $attachments = []; | ||
|
||
public function send(Message $message) | ||
{ | ||
$this->text = $this->extractText($message); | ||
$this->html = $this->extractHtml($message); | ||
$this->attachments = $this->extractAttachments($message); | ||
} | ||
}; | ||
} | ||
|
||
public function testExtractTextFromStringBodyReturnsString(): void | ||
{ | ||
$expected = 'Foo'; | ||
$message = new Message(); | ||
$message->setBody($expected); | ||
|
||
$this->service->send($message); | ||
self::assertSame($expected, $this->service->text); | ||
} | ||
|
||
public function testExtractTextFromEmptyBodyReturnsNull(): void | ||
{ | ||
$message = new Message(); | ||
|
||
$this->service->send($message); | ||
self::assertNull($this->service->text); | ||
} | ||
|
||
public function testExtractTextFromTwoPartMessageReturnsString(): void | ||
{ | ||
$expected = 'Foo'; | ||
$message = new Message(); | ||
$body = new MimeMessage(); | ||
$body->addPart(new Part('')); | ||
$body->addPart( | ||
(new Part($expected)) | ||
->setType(Mime::TYPE_TEXT) | ||
); | ||
$message->setBody($body); | ||
|
||
$this->service->send($message); | ||
self::assertSame($expected, $this->service->text); | ||
} | ||
|
||
public function testExtractTextFromTextAttachmentReturnsNull(): void | ||
{ | ||
$message = new Message(); | ||
$body = new MimeMessage(); | ||
$body->addPart( | ||
(new Part('Foo')) | ||
->setType(Mime::TYPE_TEXT) | ||
->setDisposition(Mime::DISPOSITION_ATTACHMENT) | ||
); | ||
$message->setBody($body); | ||
|
||
$this->service->send($message); | ||
self::assertNull($this->service->text); | ||
} | ||
|
||
public function testExtractTextFromMultipartMessageReturnsString(): void | ||
{ | ||
$expected = 'Foo'; | ||
$message = new Message(); | ||
$body = new MimeMessage(); | ||
$contentPart = new MimeMessage(); | ||
$contentPart->addPart(new Part()); | ||
$contentPart->addPart( | ||
(new Part($expected)) | ||
->setType(Mime::TYPE_TEXT) | ||
); | ||
$body->addPart( | ||
(new Part($contentPart->generateMessage())) | ||
->setType(Mime::MULTIPART_ALTERNATIVE) | ||
->setBoundary($contentPart->getMime()->boundary()) | ||
); | ||
$message->setBody($body); | ||
|
||
$this->service->send($message); | ||
self::assertSame($expected, trim($this->service->text)); | ||
} | ||
|
||
public function testExtractHtmlFromStringBodyReturnsNull(): void | ||
{ | ||
$message = new Message(); | ||
$message->setBody('Foo'); | ||
|
||
$this->service->send($message); | ||
self::assertNull($this->service->html); | ||
} | ||
|
||
public function testExtractHtmlFromEmptyBodyReturnsNull(): void | ||
{ | ||
$message = new Message(); | ||
|
||
$this->service->send($message); | ||
self::assertNull($this->service->html); | ||
} | ||
|
||
public function testExtractHtmlFromTwoPartMessageReturnsString(): void | ||
{ | ||
$expected = 'Foo'; | ||
$message = new Message(); | ||
$body = new MimeMessage(); | ||
$body->addPart(new Part('')); | ||
$body->addPart( | ||
(new Part($expected)) | ||
->setType(Mime::TYPE_HTML) | ||
); | ||
$message->setBody($body); | ||
|
||
$this->service->send($message); | ||
self::assertSame($expected, $this->service->html); | ||
} | ||
|
||
public function testExtractHtmlFromHtmlAttachmentReturnsNull(): void | ||
{ | ||
$message = new Message(); | ||
$body = new MimeMessage(); | ||
$body->addPart( | ||
(new Part('Foo')) | ||
->setType(Mime::TYPE_HTML) | ||
->setDisposition(Mime::DISPOSITION_ATTACHMENT) | ||
); | ||
$message->setBody($body); | ||
|
||
$this->service->send($message); | ||
self::assertNull($this->service->html); | ||
} | ||
|
||
public function testExtractHtmlFromMultipartMessageReturnsString(): void | ||
{ | ||
$expected = 'Foo'; | ||
$message = new Message(); | ||
$body = new MimeMessage(); | ||
$contentPart = new MimeMessage(); | ||
$contentPart->addPart(new Part()); | ||
$contentPart->addPart( | ||
(new Part($expected)) | ||
->setType(Mime::TYPE_HTML) | ||
); | ||
$body->addPart( | ||
(new Part($contentPart->generateMessage())) | ||
->setType(Mime::MULTIPART_ALTERNATIVE) | ||
->setBoundary($contentPart->getMime()->boundary()) | ||
); | ||
$message->setBody($body); | ||
|
||
$this->service->send($message); | ||
self::assertSame($expected, trim($this->service->html)); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've raised a PR against
laminas/laminas-mail
to fix what I think are issues with their documentation. If that's merged this section may not be needed.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kynx It seems they are merging your work :). So .. what do you think makes most sense here? Drop this PR, or merge it neverthless?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This PR is still required so the text and html can be extracted from the
multipart/alternative
part. But I'll just link to the laminas docs for a (now correct) example of how to create the email in the first place.