1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Netgen\InformationCollection\Core\Mailer; |
6
|
|
|
|
7
|
|
|
use Netgen\InformationCollection\API\Exception\EmailNotSentException; |
8
|
|
|
use Netgen\InformationCollection\API\MailerInterface; |
9
|
|
|
use Netgen\InformationCollection\API\Value\DataTransfer\EmailContent; |
10
|
|
|
|
11
|
|
|
class Mailer implements MailerInterface |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var \Swift_Mailer |
15
|
|
|
*/ |
16
|
|
|
protected $internalMailer; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Mailer constructor. |
20
|
|
|
* |
21
|
|
|
* @param \Swift_Mailer $internalMailer |
22
|
|
|
*/ |
23
|
|
|
public function __construct(\Swift_Mailer $internalMailer) |
24
|
|
|
{ |
25
|
|
|
$this->internalMailer = $internalMailer; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* {@inheritdoc} |
30
|
|
|
*/ |
31
|
|
|
public function createAndSendMessage(EmailContent $data): void |
32
|
|
|
{ |
33
|
|
|
$message = new \Swift_Message(); |
34
|
|
|
|
35
|
|
|
try { |
36
|
|
|
$message->setTo($data->getRecipients()); |
37
|
|
|
} catch (\Swift_RfcComplianceException $e) { |
38
|
|
|
throw new EmailNotSentException('recipients', $e->getMessage()); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
try { |
42
|
|
|
$message->setFrom($data->getSender()); |
43
|
|
|
} catch (\Swift_RfcComplianceException $e) { |
44
|
|
|
throw new EmailNotSentException('sender', $e->getMessage()); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$message->setSubject($data->getSubject()); |
48
|
|
|
$message->setBody($data->getBody(), 'text/html'); |
49
|
|
|
|
50
|
|
|
if ($data->hasAttachments()) { |
51
|
|
|
foreach ($data->getAttachments() as $attachment) { |
52
|
|
|
$message->attach( |
53
|
|
|
\Swift_Attachment::fromPath($attachment->inputUri, $attachment->mimeType) |
54
|
|
|
->setFilename($attachment->fileName) |
55
|
|
|
); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
if (!$this->internalMailer->send($message)) { |
60
|
|
|
throw new EmailNotSentException('send', 'invalid mailer configuration?'); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|