1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace IrishDan\NotificationBundle\Adapter; |
4
|
|
|
|
5
|
|
|
use IrishDan\NotificationBundle\EmailableInterface; |
6
|
|
|
use IrishDan\NotificationBundle\Message\MessageInterface; |
7
|
|
|
use IrishDan\NotificationBundle\Notification\NotificationInterface; |
8
|
|
|
|
9
|
|
|
class MailMessageAdapter extends BaseMessageAdapter implements MessageAdapterInterface |
10
|
|
|
{ |
11
|
|
|
const CHANNEL = 'mail'; |
12
|
|
|
protected $mailConfiguration; |
13
|
|
|
protected $mailer; |
14
|
|
|
|
15
|
|
|
public function __construct(\Swift_Mailer $mailer, array $mailConfiguration = []) |
16
|
|
|
{ |
17
|
|
|
$this->mailConfiguration = $mailConfiguration; |
18
|
|
|
$this->mailer = $mailer; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Generates a Message object |
23
|
|
|
* |
24
|
|
|
* @param NotificationInterface $notification |
25
|
|
|
* @return \IrishDan\NotificationBundle\Message\Message |
26
|
|
|
*/ |
27
|
|
|
public function format(NotificationInterface $notification) |
28
|
|
|
{ |
29
|
|
|
$notification->setChannel(self::CHANNEL); |
30
|
|
|
parent::format($notification); |
31
|
|
|
|
32
|
|
|
/** @var EmailableInterface $notifiable */ |
33
|
|
|
$notifiable = $notification->getNotifiable(); |
34
|
|
|
if (!$notifiable instanceof EmailableInterface) { |
35
|
|
|
$this->createFormatterException(EmailableInterface::class, self::CHANNEL); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
// Build the dispatch data array. |
39
|
|
|
$dispatchData = [ |
40
|
|
|
'to' => $notifiable->getEmail(), |
41
|
|
|
'from' => $this->getSender($notification), |
42
|
|
|
'cc' => '', |
43
|
|
|
'bcc' => '', |
44
|
|
|
]; |
45
|
|
|
|
46
|
|
|
$messageData = self::createMessagaData($notification->getDataArray()); |
47
|
|
|
$data = $notification->getDataArray(); |
48
|
|
|
|
49
|
|
|
if (!empty($data['html_email'])) { |
50
|
|
|
$messageData['html_email'] = true; |
51
|
|
|
} |
52
|
|
|
// Add any email attachments. |
53
|
|
|
$messageData['attachments'] = empty($data['attachments']) ? [] : $data['attachments']; |
54
|
|
|
|
55
|
|
|
return self::createMessage($dispatchData, $messageData, self::CHANNEL); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
protected function getSender(NotificationInterface $notification) |
59
|
|
|
{ |
60
|
|
|
$data = $notification->getDataArray(); |
61
|
|
|
|
62
|
|
|
if (!empty($data['from'])) { |
63
|
|
|
return $data['from']; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
if (!empty($this->mailConfiguration['default_sender'])) { |
67
|
|
|
return $this->mailConfiguration['default_sender']; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
throw new \LogicException( |
71
|
|
|
'There is no "from" email address or "default_sender" configured' |
72
|
|
|
); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function dispatch(MessageInterface $message) |
76
|
|
|
{ |
77
|
|
|
// Get the dispatch and message data from the message. |
78
|
|
|
$dispatchData = $message->getDispatchData(); |
79
|
|
|
$messageData = $message->getMessageData(); |
80
|
|
|
|
81
|
|
|
$mail = \Swift_Message::newInstance() |
82
|
|
|
->setSubject($messageData['title']) |
83
|
|
|
->setBody($messageData['body']); |
84
|
|
|
|
85
|
|
|
$mail->setFrom($dispatchData['from']); |
86
|
|
|
$mail->setTo($dispatchData['to']); |
87
|
|
|
|
88
|
|
|
// Check if its a html email |
89
|
|
|
if (!empty($messageData['html_email'])) { |
90
|
|
|
$mail->setContentType('text/html'); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
// Add any attachments |
94
|
|
|
if (!empty($messageData['attachments'])) { |
95
|
|
|
foreach ($messageData['attachments'] as $path => $filename) { |
96
|
|
|
$mail->attach( |
97
|
|
|
\Swift_Attachment::fromPath($path)->setFilename($filename) |
98
|
|
|
); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$sent = $this->mailer->send($mail); |
103
|
|
|
|
104
|
|
|
return !empty($sent); |
105
|
|
|
} |
106
|
|
|
} |