|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
/** |
|
4
|
|
|
* Copyright (c) Phauthentic (https://github.com/Phauthentic) |
|
5
|
|
|
* |
|
6
|
|
|
* Licensed under The MIT License |
|
7
|
|
|
* For full copyright and license information, please see the LICENSE.txt |
|
8
|
|
|
* Redistributions of files must retain the above copyright notice. |
|
9
|
|
|
* |
|
10
|
|
|
* @copyright Copyright (c) Phauthentic (https://github.com/Phauthentic) |
|
11
|
|
|
* @link https://github.com/Phauthentic |
|
12
|
|
|
* @license https://opensource.org/licenses/mit-license.php MIT License |
|
13
|
|
|
*/ |
|
14
|
|
|
namespace Phauthentic\Email\Mailer; |
|
15
|
|
|
|
|
16
|
|
|
use Phauthentic\Email\EmailInterface; |
|
17
|
|
|
use Swift_Attachment; |
|
18
|
|
|
use Swift_Mailer; |
|
19
|
|
|
use Swift_Message; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Swift Mailer |
|
23
|
|
|
*/ |
|
24
|
|
|
class SwiftMailer implements MailerInterface |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* Swift Mailer Instance |
|
28
|
|
|
* |
|
29
|
|
|
* @var \Swift_Mailer |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $mailer; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Constructor |
|
35
|
|
|
* |
|
36
|
|
|
* @param \Swift_Mailer $mailer Swift Mailer Instance |
|
37
|
|
|
*/ |
|
38
|
|
|
public function __construct(Swift_Mailer $mailer) |
|
39
|
|
|
{ |
|
40
|
|
|
$this->mailer = $mailer; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function getSwiftMessage(): Swift_Message |
|
44
|
|
|
{ |
|
45
|
|
|
return new Swift_Message(); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @inheritDoc |
|
50
|
|
|
*/ |
|
51
|
|
|
public function send(EmailInterface $email): bool |
|
52
|
|
|
{ |
|
53
|
|
|
$message = $this->defaultEmail($email); |
|
54
|
|
|
|
|
55
|
|
|
return (bool)$this->mailer->send($message); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function defaultEmail(EmailInterface $email) |
|
59
|
|
|
{ |
|
60
|
|
|
$swiftMail = new Swift_Message($email->getSubject()); |
|
61
|
|
|
$swiftMail |
|
62
|
|
|
->setFrom( |
|
63
|
|
|
$email->getSender()->getEmail(), |
|
64
|
|
|
$email->getSender()->getName() |
|
65
|
|
|
) |
|
66
|
|
|
->setTo((string)$email->getReceivers()[0]->getEmail(), $email->getReceivers()[0]->getName()); |
|
67
|
|
|
|
|
68
|
|
|
foreach ($email->getAttachments() as $attachment) { |
|
69
|
|
|
$swiftMail->attach(new Swift_Attachment($attachment->getFile())); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
return $this->setBody($swiftMail, $email); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Sets the body to the email implementation |
|
77
|
|
|
*/ |
|
78
|
|
|
protected function setBody(Swift_Message $swiftMail, EmailInterface $email) |
|
79
|
|
|
{ |
|
80
|
|
|
$text = $email->getTextContent(); |
|
81
|
|
|
$html = $email->getHtmlContent(); |
|
82
|
|
|
|
|
83
|
|
|
if (!empty($text) && !empty($html)) { |
|
84
|
|
|
$swiftMail->setBody($html, 'text/html'); |
|
85
|
|
|
$swiftMail->addPart($text, 'text/plain'); |
|
86
|
|
|
} else { |
|
87
|
|
|
if (!empty($text)) { |
|
88
|
|
|
$swiftMail->setBody($text, 'text/plain'); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
if (!empty($html)) { |
|
92
|
|
|
$swiftMail->setBody($html, 'text/html'); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
return $swiftMail; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
public function getEmail($name, $email) |
|
100
|
|
|
{ |
|
101
|
|
|
$name = $name . 'Email'; |
|
102
|
|
|
if (method_exists($this, $name)) { |
|
103
|
|
|
return $this->{$name}($email); |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
} |
|
108
|
|
|
|