4 * This file is part of SwiftMailer.
5 * (c) 2004-2009 Chris Corbyn
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
11 //@require 'Swift/Transport.php';
12 //@require 'Swift/Mime/Message.php';
13 //@require 'Swift/Mailer/RecipientIterator.php';
14 //@require 'Swift/Events/EventListener.php';
20 * @author Chris Corbyn
25 /** The Transport used to send messages */
29 * Create a new Mailer using $transport for delivery.
31 * @param Swift_Transport $transport
33 public function __construct(Swift_Transport
$transport)
35 $this->_transport
= $transport;
39 * Create a new Mailer instance.
41 * @param Swift_Transport $transport
42 * @return Swift_Mailer
44 public static function newInstance(Swift_Transport
$transport)
46 return new self($transport);
50 * Send the given Message like it would be sent in a mail client.
52 * All recipients (with the exception of Bcc) will be able to see the other
53 * recipients this message was sent to.
55 * If you need to send to each recipient without disclosing details about the
56 * other recipients see {@link batchSend()}.
58 * Recipient/sender data will be retreived from the Message object.
60 * The return value is the number of recipients who were accepted for
63 * @param Swift_Mime_Message $message
64 * @param array &$failedRecipients, optional
68 public function send(Swift_Mime_Message
$message, &$failedRecipients = null)
70 $failedRecipients = (array) $failedRecipients;
72 if (!$this->_transport
->isStarted())
74 $this->_transport
->start();
77 return $this->_transport
->send($message, $failedRecipients);
81 * Send the given Message to all recipients individually.
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.
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
91 * Sender information is always read from the Message object.
93 * The return value is the number of recipients who were accepted for
96 * @param Swift_Mime_Message $message
97 * @param array &$failedRecipients, optional
98 * @param Swift_Mailer_RecipientIterator $it, optional
102 public function batchSend(Swift_Mime_Message
$message,
103 &$failedRecipients = null,
104 Swift_Mailer_RecipientIterator
$it = null)
106 $failedRecipients = (array) $failedRecipients;
109 $to = $message->getTo();
110 $cc = $message->getCc();
111 $bcc = $message->getBcc();
115 $message->setCc(array());
119 $message->setBcc(array());
122 //Use an iterator if set
125 while ($it->hasNext())
127 $message->setTo($it->nextRecipient());
128 $sent +
= $this->send($message, $failedRecipients);
133 foreach ($to as $address => $name)
135 $message->setTo(array($address => $name));
136 $sent +
= $this->send($message, $failedRecipients);
140 $message->setTo($to);
144 $message->setCc($cc);
148 $message->setBcc($bcc);
155 * Register a plugin using a known unique key (e.g. myPlugin).
157 * @param Swift_Events_EventListener $plugin
160 public function registerPlugin(Swift_Events_EventListener
$plugin)
162 $this->_transport
->registerPlugin($plugin);
166 * The Transport used to send messages.
167 * @return Swift_Transport
169 public function getTransport()
171 return $this->_transport
;