-
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathSubscriberMailChannel.php
More file actions
130 lines (115 loc) · 4.63 KB
/
Copy pathSubscriberMailChannel.php
File metadata and controls
130 lines (115 loc) · 4.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
namespace YlsIdeas\SubscribableNotifications\Channels;
use Illuminate\Contracts\Mail\Mailable;
use Illuminate\Notifications\Channels\MailChannel;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
use YlsIdeas\SubscribableNotifications\Contracts\AppliesToMailingList;
use YlsIdeas\SubscribableNotifications\Contracts\CanUnsubscribe;
use YlsIdeas\SubscribableNotifications\Contracts\CheckNotifiableSubscriptionStatus;
use YlsIdeas\SubscribableNotifications\Contracts\CheckSubscriptionStatusBeforeSendingNotifications;
/**
* @deprecated v1.x will be removed in v2.0. Automatic unsubscribe-link injection via a custom
* MailChannel is replaced by explicit opt-in: return SubscribableMailMessage::via($notifiable, $this)
* from your notification's toMail() method instead.
* See the upgrade guide: UPGRADE.md
*/
class SubscriberMailChannel extends MailChannel
{
/**
* The mailer implementation.
*
* @var \Illuminate\Contracts\Mail\Factory
*/
protected $mailer;
/**
* Send the given notification.
*
* @param mixed $notifiable
* @param \Illuminate\Notifications\Notification $notification
* @return \Illuminate\Mail\SentMessage|null
*/
public function send($notifiable, Notification $notification)
{
trigger_error(
'SubscriberMailChannel is deprecated and will be removed in v2.0.'
. ' Return SubscribableMailMessage::via($notifiable, $this) from your notification\'s'
. ' toMail() method to opt in to unsubscribe-link injection explicitly.',
E_USER_DEPRECATED
);
// Check if the user would want the mail
if ($notifiable instanceof CheckSubscriptionStatusBeforeSendingNotifications &&
$notification instanceof CheckNotifiableSubscriptionStatus &&
$notification->checkMailSubscriptionStatus() &&
! $notifiable->mailSubscriptionStatus($notification)) {
return null;
}
if (method_exists($notification, 'toMail')) {
$message = $notification->toMail($notifiable);
} else {
throw new \RuntimeException('Notification does not support sending mail');
}
// Inject unsubscribe links for rendering in the view
if ($notifiable instanceof CanUnsubscribe && $message instanceof MailMessage) {
if ($notification instanceof AppliesToMailingList) {
$message->viewData['unsubscribeLink'] = $notifiable->unsubscribeLink(
$notification->usesMailingList()
);
}
$message->viewData['unsubscribeLinkForAll'] = $notifiable->unsubscribeLink();
}
if (! $notifiable->routeNotificationFor('mail', $notification) &&
! $message instanceof Mailable) {
return null;
}
if ($message instanceof Mailable) {
$message->send($this->mailer);
return null;
}
return $this->mailer->mailer($message->mailer ?? null)->send(
$this->buildView($message),
array_merge($message->data(), $this->additionalMessageData($notification)),
$this->messageBuilder($notifiable, $notification, $message)
);
}
/**
* Build the mail message.
*
* @param \Illuminate\Mail\Message $mailMessage
* @param mixed $notifiable
* @param \Illuminate\Notifications\Notification $notification
* @param \Illuminate\Notifications\Messages\MailMessage $message
*
* @return void
*/
protected function buildMessage($mailMessage, $notifiable, $notification, $message)
{
parent::buildMessage($mailMessage, $notifiable, $notification, $message);
if ($notifiable instanceof CanUnsubscribe) {
$mailMessage->getHeaders()->addTextHeader(
'List-Unsubscribe',
sprintf('<%s>', $notifiable->unsubscribeLink(
$notification instanceof AppliesToMailingList
? $notification->usesMailingList()
: null
))
);
}
}
/**
* Build the notification's view.
*
* @param \Illuminate\Notifications\Messages\MailMessage $message
* @return string|array
*/
protected function buildView($message)
{
if ($message->view) {
return $message->view;
}
return [
'html' => $this->markdown->render('subscriber::html', $message->data()),
'text' => $this->markdown->renderText('subscriber::text', $message->data()),
];
}
}