Skip to content
This repository has been archived by the owner on Nov 21, 2022. It is now read-only.

Commit

Permalink
Merge pull request #32 from spaantje/L9.x-support
Browse files Browse the repository at this point in the history
Laravel 9.x Support
  • Loading branch information
spaantje authored Feb 16, 2022
2 parents 4d6c7e7 + d1c4fd5 commit 8ba15db
Show file tree
Hide file tree
Showing 7 changed files with 177 additions and 59 deletions.
10 changes: 4 additions & 6 deletions .github/workflows/master.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,12 @@ jobs:
fail-fast: true
matrix:
os: [ubuntu-latest, windows-latest]
php: [7.3, 7.4, 8.0]
laravel: [6.*, 8.*]
php: [8.0, 8.1]
laravel: [9.*]
stability: [prefer-lowest, prefer-stable]
include:
- laravel: 6.*
testbench: ^4.14
- laravel: 8.*
testbench: ^6.20
- laravel: 9.*
testbench: ^7.0

name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.stability }} - ${{ matrix.os }}

Expand Down
8 changes: 5 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@
}
],
"require": {
"illuminate/support": "~6.0|~8.0",
"doctrine/dbal": "^2.10"
"php": "^8.0.2",
"illuminate/support": "^9.0",
"doctrine/dbal": "^3.3",
"nesbot/carbon": "^2.0"
},
"require-dev": {
"orchestra/testbench": "^4.0|^6.0",
"orchestra/testbench": "^7.0",
"phpunit/phpunit": "^9.0"
},
"autoload": {
Expand Down
105 changes: 56 additions & 49 deletions src/ShvetsGroup/LaravelEmailDatabaseLog/EmailLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,58 +2,65 @@

namespace ShvetsGroup\LaravelEmailDatabaseLog;

use Carbon\Carbon;
use Symfony\Component\Mime\Email;
use Illuminate\Support\Facades\DB;
use Symfony\Component\Mime\Part\DataPart;
use Illuminate\Mail\Events\MessageSending;

class EmailLogger
{
/**
* Handle the event.
*
* @param MessageSending $event
*/
public function handle(MessageSending $event)
{
$message = $event->message;

DB::table('email_log')->insert([
'date' => date('Y-m-d H:i:s'),
'from' => $this->formatAddressField($message, 'From'),
'to' => $this->formatAddressField($message, 'To'),
'cc' => $this->formatAddressField($message, 'Cc'),
'bcc' => $this->formatAddressField($message, 'Bcc'),
'subject' => $message->getSubject(),
'body' => $message->getBody(),
'headers' => (string)$message->getHeaders(),
'attachments' => $message->getChildren() ? implode("\n\n", $message->getChildren()) : null,
]);
}

/**
* Format address strings for sender, to, cc, bcc.
*
* @param $message
* @param $field
* @return null|string
*/
function formatAddressField($message, $field)
{
$headers = $message->getHeaders();

if (!$headers->has($field)) {
return null;
}

$mailboxes = $headers->get($field)->getFieldBodyModel();

$strings = [];
foreach ($mailboxes as $email => $name) {
$mailboxStr = $email;
if (null !== $name) {
$mailboxStr = $name . ' <' . $mailboxStr . '>';
}
$strings[] = $mailboxStr;
}
return implode(', ', $strings);
}
/**
* Handle the actual logging.
*
* @param MessageSending $event
* @return void
*/
public function handle(MessageSending $event): void
{
$message = $event->message;

DB::table('email_log')->insert([
'date' => Carbon::now()->format('Y-m-d H:i:s'),
'from' => $this->formatAddressField($message, 'From'),
'to' => $this->formatAddressField($message, 'To'),
'cc' => $this->formatAddressField($message, 'Cc'),
'bcc' => $this->formatAddressField($message, 'Bcc'),
'subject' => $message->getSubject(),
'body' => $message->getBody()->bodyToString(),
'headers' => $message->getHeaders()->toString(),
'attachments' => $this->saveAttachments($message),
]);
}

/**
* Format address strings for sender, to, cc, bcc.
*
* @param Email $message
* @param string $field
* @return null|string
*/
function formatAddressField(Email $message, string $field): ?string
{
$headers = $message->getHeaders();

return $headers->get($field)?->getBodyAsString();
}

/**
* Collect all attachments and format them as strings.
*
* @param Email $message
* @return string|null
*/
protected function saveAttachments(Email $message): ?string
{
if (empty($message->getAttachments())) {
return null;
}

return collect($message->getAttachments())
->map(fn(DataPart $part) => $part->toString())
->implode("\n\n");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace ShvetsGroup\LaravelEmailDatabaseLog;

use Illuminate\Support\ServiceProvider;
use Illuminate\Mail\Events\MessageSending;

class LaravelEmailDatabaseLogServiceProvider extends ServiceProvider
{
Expand Down
75 changes: 75 additions & 0 deletions tests/Feature/LaravelEmailDatabaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

namespace ShvetsGroup\LaravelEmailDatabaseLog\Tests\Feature;

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Mail;
use Symfony\Component\Mime\Encoder\Base64Encoder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use ShvetsGroup\LaravelEmailDatabaseLog\Tests\TestCase;
use ShvetsGroup\LaravelEmailDatabaseLog\Tests\Mail\TestMail;
use ShvetsGroup\LaravelEmailDatabaseLog\Tests\Mail\TestMailWithAttachment;

class LaravelEmailDatabaseTest extends TestCase
{
Expand All @@ -28,4 +31,76 @@ public function the_email_is_logged_to_the_database()
'attachments' => null,
]);
}

/** @test */
public function multiple_recipients_are_comma_separated()
{
Mail::to(['email@example.com', 'email2@example.com'])
->send(new TestMail());

$this->assertDatabaseHas('email_log', [
'date' => now()->format('Y-m-d H:i:s'),
'to' => 'email@example.com, email2@example.com',
'cc' => null,
'bcc' => null,
]);
}

/** @test */
public function recipient_with_name_is_correctly_formatted()
{
Mail::to((object)['email' => 'email@example.com', 'name' => 'John Do'])
->send(new TestMail());

$this->assertDatabaseHas('email_log', [
'date' => now()->format('Y-m-d H:i:s'),
'to' => 'John Do <email@example.com>',
'cc' => null,
'bcc' => null,
]);
}

/** @test */
public function cc_recipient_with_name_is_correctly_formatted()
{
Mail::cc((object)['email' => 'email@example.com', 'name' => 'John Do'])
->send(new TestMail());

$this->assertDatabaseHas('email_log', [
'date' => now()->format('Y-m-d H:i:s'),
'to' => null,
'cc' => 'John Do <email@example.com>',
'bcc' => null,
]);
}

/** @test */
public function bcc_recipient_with_name_is_correctly_formatted()
{
Mail::bcc((object)['email' => 'email@example.com', 'name' => 'John Do'])
->send(new TestMail());

$this->assertDatabaseHas('email_log', [
'date' => now()->format('Y-m-d H:i:s'),
'to' => null,
'cc' => null,
'bcc' => 'John Do <email@example.com>',
]);
}

/** @test */
public function attachement_is_saved()
{
Mail::to('email@example.com')->send(new TestMailWithAttachment());

$log = DB::table('email_log')->first();

// TODO: Is there a beter way to tests this ?
$encoded = (new Base64Encoder)->encodeString(file_get_contents(__DIR__ . '/../stubs/demo.txt'));

$this->assertStringContainsString('Content-Type: text/plain; name=demo.txt', $log->attachments);
$this->assertStringContainsString('Content-Transfer-Encoding: base64', $log->attachments);
$this->assertStringContainsString('Content-Disposition: attachment; name=demo.txt; filename=demo.txt', $log->attachments);
$this->assertStringContainsString($encoded, $log->attachments);
}
}
36 changes: 36 additions & 0 deletions tests/Mail/TestMailWithAttachment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace ShvetsGroup\LaravelEmailDatabaseLog\Tests\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;

class TestMailWithAttachment extends Mailable
{
use Queueable, SerializesModels;

/**
* Create a new message instance.
*
* @return void
*/
public function __construct()
{
//
}

/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this
->subject('The e-mail subject')
->attach(__DIR__ . '/../stubs/demo.txt')
->html('<p>Some random string.</p>');
}
}
1 change: 1 addition & 0 deletions tests/stubs/demo.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is a demo attachment.

0 comments on commit 8ba15db

Please sign in to comment.