Mailer.php 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. * @package Swift
  13. * @author Chris Corbyn
  14. */
  15. class Swift_Mailer
  16. {
  17. /** The Transport used to send messages */
  18. private $_transport;
  19. /**
  20. * Create a new Mailer using $transport for delivery.
  21. *
  22. * @param Swift_Transport $transport
  23. */
  24. public function __construct(Swift_Transport $transport)
  25. {
  26. $this->_transport = $transport;
  27. }
  28. /**
  29. * Create a new Mailer instance.
  30. *
  31. * @param Swift_Transport $transport
  32. * @return Swift_Mailer
  33. */
  34. public static function newInstance(Swift_Transport $transport)
  35. {
  36. return new self($transport);
  37. }
  38. /**
  39. * Create a new class instance of one of the message services
  40. * For example 'mimepart' would create a 'message.mimepart' instance
  41. *
  42. * @param string $service
  43. * @return object
  44. */
  45. public function createMessage($service = 'message')
  46. {
  47. return Swift_DependencyContainer::getInstance()
  48. ->lookup('message.'.$service);
  49. }
  50. /**
  51. * Send the given Message like it would be sent in a mail client.
  52. *
  53. * All recipients (with the exception of Bcc) will be able to see the other
  54. * recipients this message was sent to.
  55. *
  56. * Recipient/sender data will be retrieved from the Message object.
  57. *
  58. * The return value is the number of recipients who were accepted for
  59. * delivery.
  60. *
  61. * @param Swift_Mime_Message $message
  62. * @param array &$failedRecipients, optional
  63. * @return int
  64. */
  65. public function send(Swift_Mime_Message $message, &$failedRecipients = null)
  66. {
  67. $failedRecipients = (array) $failedRecipients;
  68. if (!$this->_transport->isStarted()) {
  69. $this->_transport->start();
  70. }
  71. $sent = 0;
  72. try {
  73. $sent = $this->_transport->send($message, $failedRecipients);
  74. } catch (Swift_RfcComplianceException $e) {
  75. foreach ($message->getTo() as $address => $name) {
  76. $failedRecipients[] = $address;
  77. }
  78. }
  79. return $sent;
  80. }
  81. /**
  82. * Register a plugin using a known unique key (e.g. myPlugin).
  83. *
  84. * @param Swift_Events_EventListener $plugin
  85. * @param string $key
  86. */
  87. public function registerPlugin(Swift_Events_EventListener $plugin)
  88. {
  89. $this->_transport->registerPlugin($plugin);
  90. }
  91. /**
  92. * The Transport used to send messages.
  93. * @return Swift_Transport
  94. */
  95. public function getTransport()
  96. {
  97. return $this->_transport;
  98. }
  99. }