Skip to content

Commit 8dc2d9b

Browse files
committed
send automatically template tags
1 parent 94ec2ad commit 8dc2d9b

File tree

3 files changed

+95
-1
lines changed

3 files changed

+95
-1
lines changed

config/mailcarrier.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,18 @@
140140
'log_strategy' => AttachmentLogStrategy::Inline,
141141
],
142142

143+
'send' => [
144+
/*
145+
|--------------------------------------------------------------------------
146+
| Template tags to provider
147+
|--------------------------------------------------------------------------
148+
|
149+
| Automatically send template tags as tags to email provider.
150+
|
151+
*/
152+
'template_tags' => false,
153+
],
154+
143155
'queue' => [
144156
/*
145157
|--------------------------------------------------------------------------

src/Actions/SendMail.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,10 @@ protected function send(RecipientDto $recipient): void
128128
remoteAttachments: $recipient->remoteAttachments,
129129
template: $this->template,
130130
variables: $recipient->variables,
131-
tags: $this->params->tags,
131+
tags: [
132+
...(Config::get('mailcarrier.send.template_tags', false) ? ($this->template->tags ?: []) : []),
133+
...$this->params->tags,
134+
],
132135
metadata: $this->params->metadata,
133136
content: $templateRender,
134137
error: $exception?->getMessage(),

tests/Feature/SendMailTest.php

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1373,3 +1373,82 @@
13731373
&& $mail->hasReplyTo('[email protected]', 'Reply');
13741374
});
13751375
});
1376+
1377+
it('does not send the template tags to provider if not enabled', function () {
1378+
Config::set('mailcarrier.send.template_tags', false);
1379+
1380+
Template::factory()->create([
1381+
'slug' => 'welcome',
1382+
'tags' => ['tag1'],
1383+
]);
1384+
1385+
postJson(route('mailcarrier.send'), [
1386+
'enqueue' => false,
1387+
'template' => 'welcome',
1388+
'subject' => 'Welcome!',
1389+
'recipient' => '[email protected]',
1390+
'tags' => ['foo'],
1391+
])->assertOk();
1392+
1393+
Mail::assertSent(GenericMail::class, 1);
1394+
Mail::assertSent(GenericMail::class, function (GenericMail $mail) {
1395+
$mail->build();
1396+
1397+
return $mail->hasTo('[email protected]')
1398+
&& $mail->hasSubject('Welcome!')
1399+
&& $mail->hasTag('foo')
1400+
&& !$mail->hasTag('tag1');
1401+
});
1402+
});
1403+
1404+
it('sends the template tags to provider when enabled', function () {
1405+
Config::set('mailcarrier.send.template_tags', true);
1406+
1407+
Template::factory()->create([
1408+
'slug' => 'welcome',
1409+
'tags' => ['tag1'],
1410+
]);
1411+
1412+
postJson(route('mailcarrier.send'), [
1413+
'enqueue' => false,
1414+
'template' => 'welcome',
1415+
'subject' => 'Welcome!',
1416+
'recipient' => '[email protected]',
1417+
])->assertOk();
1418+
1419+
Mail::assertSent(GenericMail::class, 1);
1420+
Mail::assertSent(GenericMail::class, function (GenericMail $mail) {
1421+
$mail->build();
1422+
1423+
return $mail->hasTo('[email protected]')
1424+
&& $mail->hasSubject('Welcome!')
1425+
&& $mail->hasTag('tag1');
1426+
});
1427+
});
1428+
1429+
it('merges the template tags along with request ones', function () {
1430+
Config::set('mailcarrier.send.template_tags', true);
1431+
1432+
Template::factory()->create([
1433+
'slug' => 'welcome',
1434+
'tags' => ['tag1'],
1435+
]);
1436+
1437+
postJson(route('mailcarrier.send'), [
1438+
'enqueue' => false,
1439+
'template' => 'welcome',
1440+
'subject' => 'Welcome!',
1441+
'recipient' => '[email protected]',
1442+
'tags' => ['foo'],
1443+
])->assertOk();
1444+
1445+
Mail::assertSent(GenericMail::class, 1);
1446+
Mail::assertSent(GenericMail::class, function (GenericMail $mail) {
1447+
$mail->build();
1448+
1449+
return $mail->hasTo('[email protected]')
1450+
&& $mail->hasSubject('Welcome!')
1451+
&& $mail->hasTag('foo')
1452+
&& $mail->hasTag('tag1');
1453+
});
1454+
});

0 commit comments

Comments
 (0)