|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Usamamuneerchaudhary\Notifier\Services\ChannelDrivers; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\Log; |
|
6
|
|
|
use Illuminate\Support\Facades\Mail; |
|
7
|
|
|
use Usamamuneerchaudhary\Notifier\Models\Notification; |
|
8
|
|
|
use Usamamuneerchaudhary\Notifier\Services\AnalyticsService; |
|
9
|
|
|
|
|
10
|
|
|
class EmailDriver implements ChannelDriverInterface |
|
11
|
|
|
{ |
|
12
|
|
|
public function send(Notification $notification): bool |
|
13
|
|
|
{ |
|
14
|
|
|
try { |
|
15
|
|
|
$channel = \Usamamuneerchaudhary\Notifier\Models\NotificationChannel::where('type', 'email')->first(); |
|
16
|
|
|
$user = $notification->user; |
|
|
|
|
|
|
17
|
|
|
|
|
18
|
|
|
if (!$user || !$user->email) { |
|
19
|
|
|
return false; |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
$settings = $channel->settings ?? []; |
|
23
|
|
|
$fromAddress = $settings['from_address'] ?? config('mail.from.address', '[email protected]'); |
|
24
|
|
|
$fromName = $settings['from_name'] ?? config('mail.from.name', 'Notification'); |
|
25
|
|
|
|
|
26
|
|
|
// Inject tracking pixel if analytics is enabled |
|
27
|
|
|
$content = $notification->content; |
|
28
|
|
|
$trackingToken = $notification->data['tracking_token'] ?? null; |
|
29
|
|
|
|
|
30
|
|
|
if ($trackingToken) { |
|
31
|
|
|
$analyticsService = app(AnalyticsService::class); |
|
32
|
|
|
if ($analyticsService->isOpenTrackingEnabled()) { |
|
33
|
|
|
$content .= $analyticsService->generateTrackingPixel($trackingToken); |
|
34
|
|
|
} |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
$isHtml = strip_tags($content) !== $content; |
|
38
|
|
|
|
|
39
|
|
|
if ($isHtml) { |
|
40
|
|
|
Mail::html($content, function (\Illuminate\Mail\Message $message) use ($notification, $user, $fromAddress, $fromName) { |
|
41
|
|
|
$message->to($user->email) |
|
42
|
|
|
->subject($notification->subject) |
|
43
|
|
|
->from($fromAddress, $fromName); |
|
44
|
|
|
}); |
|
45
|
|
|
} else { |
|
46
|
|
|
Mail::raw($content, function (\Illuminate\Mail\Message $message) use ($notification, $user, $fromAddress, $fromName) { |
|
47
|
|
|
$message->to($user->email) |
|
48
|
|
|
->subject($notification->subject) |
|
49
|
|
|
->from($fromAddress, $fromName); |
|
50
|
|
|
}); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
return true; |
|
54
|
|
|
} catch (\Exception $e) { |
|
55
|
|
|
Log::error("Email notification failed: " . $e->getMessage()); |
|
56
|
|
|
return false; |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function validateSettings(array $settings): bool |
|
61
|
|
|
{ |
|
62
|
|
|
return !empty($settings['from_address'] ?? null); |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.