TYPO3 API  SVNRelease
Mailer.php
Go to the documentation of this file.
00001 <?php
00002 
00003 /*
00004  * This file is part of SwiftMailer.
00005  * (c) 2004-2009 Chris Corbyn
00006  *
00007  * For the full copyright and license information, please view the LICENSE
00008  * file that was distributed with this source code.
00009  */
00010 
00011 //@require 'Swift/Transport.php';
00012 //@require 'Swift/Mime/Message.php';
00013 //@require 'Swift/Mailer/RecipientIterator.php';
00014 //@require 'Swift/Events/EventListener.php';
00015 
00016 /**
00017  * Swift Mailer class.
00018  *
00019  * @package Swift
00020  * @author Chris Corbyn
00021  */
00022 class Swift_Mailer
00023 {
00024 
00025   /** The Transport used to send messages */
00026   private $_transport;
00027 
00028   /**
00029    * Create a new Mailer using $transport for delivery.
00030    *
00031    * @param Swift_Transport $transport
00032    */
00033   public function __construct(Swift_Transport $transport)
00034   {
00035     $this->_transport = $transport;
00036   }
00037 
00038   /**
00039    * Create a new Mailer instance.
00040    *
00041    * @param Swift_Transport $transport
00042    * @return Swift_Mailer
00043    */
00044   public static function newInstance(Swift_Transport $transport)
00045   {
00046     return new self($transport);
00047   }
00048 
00049   /**
00050    * Send the given Message like it would be sent in a mail client.
00051    *
00052    * All recipients (with the exception of Bcc) will be able to see the other
00053    * recipients this message was sent to.
00054    *
00055    * If you need to send to each recipient without disclosing details about the
00056    * other recipients see {@link batchSend()}.
00057    *
00058    * Recipient/sender data will be retreived from the Message object.
00059    *
00060    * The return value is the number of recipients who were accepted for
00061    * delivery.
00062    *
00063    * @param Swift_Mime_Message $message
00064    * @param array &$failedRecipients, optional
00065    * @return int
00066    * @see batchSend()
00067    */
00068   public function send(Swift_Mime_Message $message, &$failedRecipients = null)
00069   {
00070     $failedRecipients = (array) $failedRecipients;
00071 
00072     if (!$this->_transport->isStarted())
00073     {
00074       $this->_transport->start();
00075     }
00076 
00077     return $this->_transport->send($message, $failedRecipients);
00078   }
00079 
00080   /**
00081    * Send the given Message to all recipients individually.
00082    *
00083    * This differs from {@link send()} in the way headers are presented to the
00084    * recipient.  The only recipient in the "To:" field will be the individual
00085    * recipient it was sent to.
00086    *
00087    * If an iterator is provided, recipients will be read from the iterator
00088    * one-by-one, otherwise recipient data will be retreived from the Message
00089    * object.
00090    *
00091    * Sender information is always read from the Message object.
00092    *
00093    * The return value is the number of recipients who were accepted for
00094    * delivery.
00095    *
00096    * @param Swift_Mime_Message $message
00097    * @param array &$failedRecipients, optional
00098    * @param Swift_Mailer_RecipientIterator $it, optional
00099    * @return int
00100    * @see send()
00101    */
00102   public function batchSend(Swift_Mime_Message $message,
00103     &$failedRecipients = null,
00104     Swift_Mailer_RecipientIterator $it = null)
00105   {
00106     $failedRecipients = (array) $failedRecipients;
00107 
00108     $sent = 0;
00109     $to = $message->getTo();
00110     $cc = $message->getCc();
00111     $bcc = $message->getBcc();
00112 
00113     if (!empty($cc))
00114     {
00115       $message->setCc(array());
00116     }
00117     if (!empty($bcc))
00118     {
00119       $message->setBcc(array());
00120     }
00121 
00122     //Use an iterator if set
00123     if (isset($it))
00124     {
00125       while ($it->hasNext())
00126       {
00127         $message->setTo($it->nextRecipient());
00128         $sent += $this->send($message, $failedRecipients);
00129       }
00130     }
00131     else
00132     {
00133       foreach ($to as $address => $name)
00134       {
00135         $message->setTo(array($address => $name));
00136         $sent += $this->send($message, $failedRecipients);
00137       }
00138     }
00139 
00140     $message->setTo($to);
00141 
00142     if (!empty($cc))
00143     {
00144       $message->setCc($cc);
00145     }
00146     if (!empty($bcc))
00147     {
00148       $message->setBcc($bcc);
00149     }
00150 
00151     return $sent;
00152   }
00153 
00154   /**
00155    * Register a plugin using a known unique key (e.g. myPlugin).
00156    *
00157    * @param Swift_Events_EventListener $plugin
00158    * @param string $key
00159    */
00160   public function registerPlugin(Swift_Events_EventListener $plugin)
00161   {
00162     $this->_transport->registerPlugin($plugin);
00163   }
00164 
00165   /**
00166    * The Transport used to send messages.
00167    * @return Swift_Transport
00168    */
00169   public function getTransport()
00170   {
00171     return $this->_transport;
00172   }
00173 }