Skip to content
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

Accept a replyTo param #36

Merged
merged 4 commits into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration {
public function up(): void
{
Schema::table('logs', function (Blueprint $table) {
$table->json('replyTo')->nullable();
});
}

public function down(): void
{
Schema::table('logs', function (Blueprint $table) {
$table->dropColumn('replyTo');
});
}
};
1 change: 1 addition & 0 deletions src/Actions/Logs/CreateFromGenericMail.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public function run(GenericMailDto $genericMailDto): Log
email: Config::get('mail.from.address')
),
'recipient' => $genericMailDto->recipient,
'replyTo' => $genericMailDto->replyTo,
'cc' => $genericMailDto->cc,
'bcc' => $genericMailDto->bcc,
'error' => $genericMailDto->error,
Expand Down
1 change: 1 addition & 0 deletions src/Actions/SendMail.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ protected function send(RecipientDto $recipient): void
trigger: $this->params->trigger,
sender: $this->params->sender,
recipient: $recipient->email,
replyTo: $this->params->replyTo,
cc: $recipient->cc,
bcc: $recipient->bcc,
subject: $this->params->subject,
Expand Down
2 changes: 2 additions & 0 deletions src/Dto/GenericMailDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class GenericMailDto extends DataTransferObject

public ?ContactDto $sender;

public ?ContactDto $replyTo;

/** @var \MailCarrier\Dto\ContactDto[]|null */
public ?array $cc;

Expand Down
3 changes: 3 additions & 0 deletions src/Dto/SendMailDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ class SendMailDto extends DataTransferObject
#[CastWith(ContactStringCaster::class)]
public ?ContactDto $sender;

#[CastWith(ContactStringCaster::class)]
public ?ContactDto $replyTo;

public ?string $recipient;

/** @var \MailCarrier\Dto\ContactDto[]|null */
Expand Down
1 change: 1 addition & 0 deletions src/Http/Requests/SendMailRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public function rules(): array
'trigger' => 'sometimes|string|max:255',
'subject' => 'required|string|max:255',
'sender' => ['sometimes', new ContactRule],
'replyTo' => ['sometimes', new ContactRule],
'cc' => 'sometimes|array',
'bcc' => 'sometimes|array',
'cc.*' => [new ContactRule],
Expand Down
4 changes: 4 additions & 0 deletions src/Mail/GenericMail.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ public function build(): self
$this->params->subject,
fn (GenericMail $mail) => $mail->subject($this->params->subject)
)
->when(
$this->params->replyTo,
fn (GenericMail $mail) => $mail->replyTo($this->params->replyTo->email, $this->params->replyTo->name)
)
->when(
$this->params->cc,
fn (GenericMail $mail) => $mail->cc($this->params->cc)
Expand Down
19 changes: 10 additions & 9 deletions src/MailCarrierServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,16 @@ public function configurePackage(Package $package): void
RetryCommand::class,
])
->hasMigrations([
'1_create_users_table',
'2_create_layouts_table',
'3_create_templates_table',
'4_create_logs_table',
'5_create_attachments_table',
'6_transform_logs_cc_bcc_array',
'7_add_tries_to_logs_table',
'8_add_tags_metadata_to_logs_table',
'9_add_tags_to_templates_table',
'2024_01_01_00000_create_users_table',
'2024_01_01_00001_create_layouts_table',
'2024_01_01_00002_create_templates_table',
'2024_01_01_00003_create_logs_table',
'2024_01_01_00004_create_attachments_table',
'2024_01_01_00005_transform_logs_cc_bcc_array',
'2024_01_01_00006_add_tries_to_logs_table',
'2024_01_01_00007_add_tags_metadata_to_logs_table',
'2024_01_01_00008_add_tags_to_templates_table',
'2024_08_27_074130_add_replyto_to_logs_table',
])
->runsMigrations();
}
Expand Down
3 changes: 3 additions & 0 deletions src/Models/Log.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
* @property string|null $trigger
* @property string|null $subject
* @property \MailCarrier\Dto\ContactDto $sender
* @property \MailCarrier\Dto\ContactDto|null $replyTo
* @property \Illuminate\Support\Collection<MailCarrier\Dto\ContactDto>|null $cc
* @property \Illuminate\Support\Collection<MailCarrier\Dto\ContactDto>|null $bcc
* @property string $recipient
Expand Down Expand Up @@ -67,6 +68,7 @@ class Log extends Model
'bcc',
'sender',
'recipient',
'replyTo',
'template_frozen',
'variables',
'error',
Expand All @@ -89,6 +91,7 @@ class Log extends Model
protected $casts = [
'status' => LogStatus::class,
'sender' => ContactDto::class,
'replyTo' => ContactDto::class,
'cc' => CollectionOfContacts::class,
'bcc' => CollectionOfContacts::class,
'template_frozen' => LogTemplateDto::class,
Expand Down
5 changes: 4 additions & 1 deletion tests/Feature/Logs/GetTriggersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,8 @@
'trigger' => 'bar',
]);

expect(GetTriggers::resolve()->run())->toEqualCanonicalizing(['foo', 'bar']);
expect(GetTriggers::resolve()->run())->toEqualCanonicalizing([
'foo' => 'foo',
'bar' => 'bar',
]);
});
49 changes: 49 additions & 0 deletions tests/Feature/SendMailTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1324,3 +1324,52 @@
&& $mail->hasMetadata('meta2', 'value2');
});
});

it('accepts a replyTo as string', function () {
Template::factory()->create([
'slug' => 'welcome',
]);

postJson(route('mailcarrier.send'), [
'enqueue' => false,
'template' => 'welcome',
'subject' => 'Welcome!',
'recipient' => '[email protected]',
'replyTo' => '[email protected]',
])->assertOk();

Mail::assertSent(GenericMail::class, 1);
Mail::assertSent(GenericMail::class, function (GenericMail $mail) {
$mail->build();

return $mail->hasTo('[email protected]')
&& $mail->hasSubject('Welcome!')
&& $mail->hasReplyTo('[email protected]');
});
});

it('accepts a replyTo as object', function () {
Template::factory()->create([
'slug' => 'welcome',
]);

postJson(route('mailcarrier.send'), [
'enqueue' => false,
'template' => 'welcome',
'subject' => 'Welcome!',
'recipient' => '[email protected]',
'replyTo' => [
'email' => '[email protected]',
'name' => 'Reply',
],
])->assertOk();

Mail::assertSent(GenericMail::class, 1);
Mail::assertSent(GenericMail::class, function (GenericMail $mail) {
$mail->build();

return $mail->hasTo('[email protected]')
&& $mail->hasSubject('Welcome!')
&& $mail->hasReplyTo('[email protected]', 'Reply');
});
});