including-the-files.rst 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. Including Swift Mailer (Autoloading)
  2. ====================================
  3. If you are using Composer, Swift Mailer will be automatically autoloaded.
  4. If not, you can use the built-in autoloader by requiring the
  5. ``swift_required.php`` file::
  6. require_once '/path/to/swift-mailer/lib/swift_required.php';
  7. /* rest of code goes here */
  8. If you want to override the default Swift Mailer configuration, call the
  9. ``init()`` method on the ``Swift`` class and pass it a valid PHP callable (a
  10. PHP function name, a PHP 5.3 anonymous function, ...)::
  11. require_once '/path/to/swift-mailer/lib/swift_required.php';
  12. function swiftmailer_configurator() {
  13. // configure Swift Mailer
  14. Swift_DependencyContainer::getInstance()->...
  15. Swift_Preferences::getInstance()->...
  16. }
  17. Swift::init('swiftmailer_configurator');
  18. /* rest of code goes here */
  19. The advantage of using the ``init()`` method is that your code will be
  20. executed only if you use Swift Mailer in your script.
  21. .. note::
  22. While Swift Mailer's autoloader is designed to play nicely with other
  23. autoloaders, sometimes you may have a need to avoid using Swift Mailer's
  24. autoloader and use your own instead. Include the ``swift_init.php``
  25. instead of the ``swift_required.php`` if you need to do this. The very
  26. minimum include is the ``swift_init.php`` file since Swift Mailer will not
  27. work without the dependency injection this file sets up:
  28. .. code-block:: php
  29. require_once '/path/to/swift-mailer/lib/swift_init.php';
  30. /* rest of code goes here */