Added feature #15998: Create a new API to send mails based on SwiftMailer to replace...
[Packages/TYPO3.CMS.git] / typo3 / contrib / swiftmailer / classes / Swift / Mailer.php
1 <?php
2
3 /*
4 * This file is part of SwiftMailer.
5 * (c) 2004-2009 Chris Corbyn
6 *
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
9 */
10
11 //@require 'Swift/Transport.php';
12 //@require 'Swift/Mime/Message.php';
13 //@require 'Swift/Mailer/RecipientIterator.php';
14 //@require 'Swift/Events/EventListener.php';
15
16 /**
17 * Swift Mailer class.
18 *
19 * @package Swift
20 * @author Chris Corbyn
21 */
22 class Swift_Mailer
23 {
24
25 /** The Transport used to send messages */
26 private $_transport;
27
28 /**
29 * Create a new Mailer using $transport for delivery.
30 *
31 * @param Swift_Transport $transport
32 */
33 public function __construct(Swift_Transport $transport)
34 {
35 $this->_transport = $transport;
36 }
37
38 /**
39 * Create a new Mailer instance.
40 *
41 * @param Swift_Transport $transport
42 * @return Swift_Mailer
43 */
44 public static function newInstance(Swift_Transport $transport)
45 {
46 return new self($transport);
47 }
48
49 /**
50 * Send the given Message like it would be sent in a mail client.
51 *
52 * All recipients (with the exception of Bcc) will be able to see the other
53 * recipients this message was sent to.
54 *
55 * If you need to send to each recipient without disclosing details about the
56 * other recipients see {@link batchSend()}.
57 *
58 * Recipient/sender data will be retreived from the Message object.
59 *
60 * The return value is the number of recipients who were accepted for
61 * delivery.
62 *
63 * @param Swift_Mime_Message $message
64 * @param array &$failedRecipients, optional
65 * @return int
66 * @see batchSend()
67 */
68 public function send(Swift_Mime_Message $message, &$failedRecipients = null)
69 {
70 $failedRecipients = (array) $failedRecipients;
71
72 if (!$this->_transport->isStarted())
73 {
74 $this->_transport->start();
75 }
76
77 return $this->_transport->send($message, $failedRecipients);
78 }
79
80 /**
81 * Send the given Message to all recipients individually.
82 *
83 * This differs from {@link send()} in the way headers are presented to the
84 * recipient. The only recipient in the "To:" field will be the individual
85 * recipient it was sent to.
86 *
87 * If an iterator is provided, recipients will be read from the iterator
88 * one-by-one, otherwise recipient data will be retreived from the Message
89 * object.
90 *
91 * Sender information is always read from the Message object.
92 *
93 * The return value is the number of recipients who were accepted for
94 * delivery.
95 *
96 * @param Swift_Mime_Message $message
97 * @param array &$failedRecipients, optional
98 * @param Swift_Mailer_RecipientIterator $it, optional
99 * @return int
100 * @see send()
101 */
102 public function batchSend(Swift_Mime_Message $message,
103 &$failedRecipients = null,
104 Swift_Mailer_RecipientIterator $it = null)
105 {
106 $failedRecipients = (array) $failedRecipients;
107
108 $sent = 0;
109 $to = $message->getTo();
110 $cc = $message->getCc();
111 $bcc = $message->getBcc();
112
113 if (!empty($cc))
114 {
115 $message->setCc(array());
116 }
117 if (!empty($bcc))
118 {
119 $message->setBcc(array());
120 }
121
122 //Use an iterator if set
123 if (isset($it))
124 {
125 while ($it->hasNext())
126 {
127 $message->setTo($it->nextRecipient());
128 $sent += $this->send($message, $failedRecipients);
129 }
130 }
131 else
132 {
133 foreach ($to as $address => $name)
134 {
135 $message->setTo(array($address => $name));
136 $sent += $this->send($message, $failedRecipients);
137 }
138 }
139
140 $message->setTo($to);
141
142 if (!empty($cc))
143 {
144 $message->setCc($cc);
145 }
146 if (!empty($bcc))
147 {
148 $message->setBcc($bcc);
149 }
150
151 return $sent;
152 }
153
154 /**
155 * Register a plugin using a known unique key (e.g. myPlugin).
156 *
157 * @param Swift_Events_EventListener $plugin
158 * @param string $key
159 */
160 public function registerPlugin(Swift_Events_EventListener $plugin)
161 {
162 $this->_transport->registerPlugin($plugin);
163 }
164
165 /**
166 * The Transport used to send messages.
167 * @return Swift_Transport
168 */
169 public function getTransport()
170 {
171 return $this->_transport;
172 }
173 }