Mailer.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. /*
  3. * This file is part of SwiftMailer.
  4. * (c) 2004-2009 Chris Corbyn
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. /**
  10. * Swift Mailer class.
  11. *
  12. * @author Chris Corbyn
  13. */
  14. class Swift_Mailer
  15. {
  16. /** The Transport used to send messages */
  17. private $transport;
  18. /**
  19. * Create a new Mailer using $transport for delivery.
  20. */
  21. public function __construct(Swift_Transport $transport)
  22. {
  23. $this->transport = $transport;
  24. }
  25. /**
  26. * Create a new class instance of one of the message services.
  27. *
  28. * For example 'mimepart' would create a 'message.mimepart' instance
  29. *
  30. * @param string $service
  31. *
  32. * @return object
  33. */
  34. public function createMessage($service = 'message')
  35. {
  36. return Swift_DependencyContainer::getInstance()
  37. ->lookup('message.'.$service);
  38. }
  39. /**
  40. * Send the given Message like it would be sent in a mail client.
  41. *
  42. * All recipients (with the exception of Bcc) will be able to see the other
  43. * recipients this message was sent to.
  44. *
  45. * Recipient/sender data will be retrieved from the Message object.
  46. *
  47. * The return value is the number of recipients who were accepted for
  48. * delivery.
  49. *
  50. * @param array $failedRecipients An array of failures by-reference
  51. *
  52. * @return int The number of successful recipients. Can be 0 which indicates failure
  53. */
  54. public function send(Swift_Mime_SimpleMessage $message, &$failedRecipients = null)
  55. {
  56. $failedRecipients = (array) $failedRecipients;
  57. // FIXME: to be removed in 7.0 (as transport must now start itself on send)
  58. if (!$this->transport->isStarted()) {
  59. $this->transport->start();
  60. }
  61. $sent = 0;
  62. try {
  63. $sent = $this->transport->send($message, $failedRecipients);
  64. } catch (Swift_RfcComplianceException $e) {
  65. foreach ($message->getTo() as $address => $name) {
  66. $failedRecipients[] = $address;
  67. }
  68. }
  69. return $sent;
  70. }
  71. /**
  72. * Register a plugin using a known unique key (e.g. myPlugin).
  73. */
  74. public function registerPlugin(Swift_Events_EventListener $plugin)
  75. {
  76. $this->transport->registerPlugin($plugin);
  77. }
  78. /**
  79. * The Transport used to send messages.
  80. *
  81. * @return Swift_Transport
  82. */
  83. public function getTransport()
  84. {
  85. return $this->transport;
  86. }
  87. }