Mailer.php 2.8KB

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