|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Hautzi\SystemMailBundle\SystemMailer\Mailer; |
|
4
|
|
|
|
|
5
|
|
|
use Hautzi\SystemMailBundle\SystemMailer\ParsedMessage; |
|
6
|
|
|
|
|
7
|
|
|
class SwiftMailer implements MailerInterface |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* @var \Swift_Mailer |
|
11
|
|
|
*/ |
|
12
|
|
|
private $mailer; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @param \Swift_Mailer $mailer |
|
16
|
|
|
*/ |
|
17
|
|
|
public function __construct(\Swift_Mailer $mailer) |
|
18
|
|
|
{ |
|
19
|
|
|
$this->mailer = $mailer; |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Sends out a ParsedMessage |
|
24
|
|
|
* |
|
25
|
|
|
* @param ParsedMessage $parsedMessage |
|
26
|
|
|
* @param callable $messageModifier |
|
27
|
|
|
*/ |
|
28
|
|
|
public function send(ParsedMessage $parsedMessage, \Closure $messageModifier = null) |
|
29
|
|
|
{ |
|
30
|
|
|
$message = $this->transformMessage($parsedMessage); |
|
31
|
|
|
|
|
32
|
|
|
if (null !== $messageModifier) { |
|
33
|
|
|
$messageModifier($message); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
$this->mailer->send($message); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Creates a swift message from a ParsedMessage, handles defaults |
|
41
|
|
|
* |
|
42
|
|
|
* @param ParsedMessage $parsedMessage |
|
43
|
|
|
* |
|
44
|
|
|
* @return \Swift_Message |
|
45
|
|
|
*/ |
|
46
|
|
|
protected function transformMessage(ParsedMessage $parsedMessage) |
|
47
|
|
|
{ |
|
48
|
|
|
$message = new \Swift_Message(); |
|
49
|
|
|
|
|
50
|
|
|
if ($from = $parsedMessage->getFrom()) { |
|
51
|
|
|
$message->setFrom($from); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
// handle to with defaults |
|
55
|
|
|
if ($to = $parsedMessage->getTo()) { |
|
56
|
|
|
$message->setTo($to); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
// handle cc with defaults |
|
60
|
|
|
if ($cc = $parsedMessage->getCc()) { |
|
61
|
|
|
$message->setCc($cc); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
// handle bcc with defaults |
|
65
|
|
|
if ($bcc = $parsedMessage->getBcc()) { |
|
66
|
|
|
$message->setBcc($bcc); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
// handle reply to with defaults |
|
70
|
|
|
if ($replyTo = $parsedMessage->getReplyTo()) { |
|
71
|
|
|
$message->setReplyTo($replyTo); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
// handle subject with default |
|
75
|
|
|
if ($subject = $parsedMessage->getSubject()) { |
|
76
|
|
|
$message->setSubject($subject); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
// handle body, no default values here |
|
80
|
|
|
$message->setBody($parsedMessage->getMessageText()); |
|
81
|
|
|
if ($parsedMessage->getMessageHtml()) { |
|
82
|
|
|
$message->addPart($parsedMessage->getMessageHtml(), 'text/html'); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
return $message; |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|