1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace WZRD\Mail; |
4
|
|
|
|
5
|
|
|
use Swift_Attachment; |
6
|
|
|
use Swift_EmbeddedFile; |
7
|
|
|
use Swift_Mailer; |
8
|
|
|
use Swift_Message; |
9
|
|
|
use WZRD\Contracts\Mail\Mailer; |
10
|
|
|
use WZRD\Contracts\Mail\Message as MessageContract; |
11
|
|
|
|
12
|
|
|
class SwiftMailer implements Mailer |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Swift. |
16
|
|
|
* |
17
|
|
|
* @var Swift_Mailer |
18
|
|
|
*/ |
19
|
|
|
private $swift; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Construct with SwiftMailer instance. |
23
|
|
|
* |
24
|
|
|
* @param Swift_Mailer $swift |
25
|
|
|
*/ |
26
|
|
|
public function __construct(Swift_Mailer $swift) |
27
|
|
|
{ |
28
|
|
|
$this->swift = $swift; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Send a mail. |
33
|
|
|
* |
34
|
|
|
* @param WZRD\Contracts\Mail\Message $message |
35
|
|
|
* @param array $options |
36
|
|
|
*/ |
37
|
|
|
public function send(MessageContract $message, $options = []) |
38
|
|
|
{ |
39
|
|
|
// Initialisation d'un nouveau message |
40
|
|
|
$swift_message = Swift_Message::newInstance(); |
41
|
|
|
|
42
|
|
|
// Compose |
43
|
|
|
$swift_message->setFrom($message->getFrom()); |
|
|
|
|
44
|
|
|
$swift_message->setTo($message->getTo()); |
45
|
|
|
$swift_message->setCc($message->getCc()); |
46
|
|
|
$swift_message->setBcc($message->getBcc()); |
47
|
|
|
$swift_message->setSubject($message->getSubject()); |
48
|
|
|
$swift_message->addPart($message->getText(), 'text/plain'); |
49
|
|
|
$swift_message->addPart($message->getHtml(), 'text/html'); |
50
|
|
|
|
51
|
|
|
// Attachments |
52
|
|
View Code Duplication |
foreach ($message->getAttachments() as $attachment) { |
|
|
|
|
53
|
|
|
list($file, $options) = $attachment; |
54
|
|
|
|
55
|
|
|
$content_type = empty($options['content-type']) ? null : $options['content-type']; |
56
|
|
|
|
57
|
|
|
$attachment = Swift_Attachment::fromPath($file, $content_type); |
58
|
|
|
|
59
|
|
|
if (!empty($options['filename'])) { |
60
|
|
|
$attachment->setFilename($options['filename']); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$swift_message->attach($attachment); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
// Inline attachments |
67
|
|
View Code Duplication |
foreach ($message->getInlines() as $inline) { |
|
|
|
|
68
|
|
|
list($file, $options) = $inline; |
69
|
|
|
|
70
|
|
|
$content_type = empty($options['content-type']) ? null : $options['content-type']; |
71
|
|
|
|
72
|
|
|
$attachment = Swift_EmbeddedFile::fromPath($file, $content_type); |
|
|
|
|
73
|
|
|
|
74
|
|
|
if (!empty($options['filename'])) { |
75
|
|
|
$attachment->setFilename($options['filename']); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$swift_message->embed($attachment); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
// Send ! |
82
|
|
|
$this->swift->send($swift_message); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: