diff --git a/src/MailTracker.php b/src/MailTracker.php index c0d5f9a..ed8d701 100644 --- a/src/MailTracker.php +++ b/src/MailTracker.php @@ -2,6 +2,7 @@ namespace jdavidbakr\MailTracker; +use Exception; use Illuminate\Mail\Events\MessageSending; use Illuminate\Mail\Events\MessageSent; use Illuminate\Mail\SentMessage; @@ -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 @@ -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(); @@ -194,7 +205,7 @@ protected function createTrackers($message) ) { $message->setBody(new TextPart( $this->addTrackers($original_html, $hash), - null, + $message->getHtmlCharset(), $original_content->getMediaSubtype(), null ) diff --git a/tests/MailTrackerTest.php b/tests/MailTrackerTest.php index d5d6f9e..e8cb9b4 100644 --- a/tests/MailTrackerTest.php +++ b/tests/MailTrackerTest.php @@ -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; @@ -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() @@ -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 */ @@ -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, [ @@ -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, [ @@ -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, [ diff --git a/tests/email/markdown.blade.php b/tests/email/markdown.blade.php new file mode 100644 index 0000000..ba60236 --- /dev/null +++ b/tests/email/markdown.blade.php @@ -0,0 +1,5 @@ +@component('mail::message') +# This is a test message + +THank you for reaing it. +@endcomponent