Skip to content

Commit

Permalink
Multipart messages needed to be handlede differently than non-multipa…
Browse files Browse the repository at this point in the history
…rt messages
  • Loading branch information
jonb-sf committed Mar 31, 2022
1 parent e675695 commit 629a44f
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/MailTracker.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace jdavidbakr\MailTracker;

use Exception;
use Illuminate\Mail\Events\MessageSending;
use Illuminate\Mail\Events\MessageSent;
use Illuminate\Mail\SentMessage;
Expand All @@ -11,6 +12,7 @@
use jdavidbakr\MailTracker\Model\SentEmail;
use jdavidbakr\MailTracker\Model\SentEmailUrlClicked;
use Symfony\Component\Mime\Email;
use Symfony\Component\Mime\Part\Multipart\AlternativePart;
use Symfony\Component\Mime\Part\TextPart;

class MailTracker
Expand Down Expand Up @@ -184,7 +186,16 @@ protected function createTrackers($message)
$subject = $message->getSubject();

$original_content = $message->getBody();
$original_html = $message->getHtmlbody();
if(get_class($original_content) == AlternativePart::class) {
$messageBody = $message->getBody() ?: [];
foreach($messageBody->getParts() as $part) {
if($part->getMediaSubtype() == 'html') {
$original_html = $part->getBody();
}
}
} else {
$original_html = $original_content->getBody();
}

$mime = $original_content->getMediaType().'/'.$original_content->getMediaSubtype();

Expand All @@ -194,7 +205,7 @@ protected function createTrackers($message)
) {
$message->setBody(new TextPart(
$this->addTrackers($original_html, $hash),
null,
$message->getHtmlCharset(),
$original_content->getMediaSubtype(),
null
)
Expand Down
38 changes: 38 additions & 0 deletions tests/MailTrackerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Mail\Events\MessageSending;
use Illuminate\Mail\Events\MessageSent;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\MailServiceProvider;
use Illuminate\Mail\SentMessage;
use Illuminate\Support\Carbon;
Expand Down Expand Up @@ -56,6 +57,14 @@ public function render($request, Throwable $e)
}
}

class TestMailable extends Mailable
{
public function build()
{
return $this->markdown('email.markdown');
}
}

class MailTrackerTest extends SetUpTest
{
protected function disableExceptionHandling()
Expand Down Expand Up @@ -166,6 +175,32 @@ public function testSendMessageWithMailRaw()
]);
}

public function testSendMessageWithMultiPart()
{
$faker = Factory::create();
$email = $faker->email;
$name = $faker->firstName . ' ' .$faker->lastName;
View::addLocation(__DIR__);
$str = Mockery::mock(Str::class);
app()->instance(Str::class, $str);
$str->shouldReceive('random')
->once()
->andReturn('random-hash');
$mailable = new TestMailable();
$mailable->subject('this is the message subject.');

try {
Mail::to($email)->send($mailable);
} catch (Exception $e) {
// dd($e);
}

$this->assertDatabaseHas('sent_emails', [
'hash' => 'random-hash',
'recipient_email' => $email,
]);
}

/**
* @test
*/
Expand Down Expand Up @@ -425,6 +460,7 @@ public function it_retrieves_the_mesage_id_from_laravel_mailer()
'setBody' => Mockery::Mock(Email::class),
'getChildren' => [],
'getId' => 'message-id',
'getHtmlCharset' => 'utf-8',
]);
$sentEvent = Mockery::mock(MessageSent::class);
$sentEvent->sent = Mockery::mock(SentMessage::class, [
Expand Down Expand Up @@ -482,6 +518,7 @@ public function it_retrieves_the_mesage_id_from_ses_mail_default()
'setBody' => null,
'getChildren' => [],
'getId' => 'message-id',
'getHtmlCharset' => 'utf-8',
]);
$sentEvent = Mockery::mock(MessageSent::class);
$sentEvent->sent = Mockery::mock(SentMessage::class, [
Expand Down Expand Up @@ -538,6 +575,7 @@ public function it_retrieves_the_mesage_id_from_ses_mail_driver()
'setBody' => null,
'getChildren' => [],
'getId' => 'message-id',
'getHtmlCharset' => 'utf-8',
]);
$sentEvent = Mockery::mock(MessageSent::class);
$sentEvent->sent = Mockery::mock(SentMessage::class, [
Expand Down
5 changes: 5 additions & 0 deletions tests/email/markdown.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@component('mail::message')
# This is a test message

THank you for reaing it.
@endcomponent

0 comments on commit 629a44f

Please sign in to comment.