including-the-files.rst 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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 */
  31. For PHP versions starting with 5.3 it is recommended using the native quoted
  32. printable encoder. It uses PHP’s native ``quoted_printable_encode()``-function
  33. to achieve much better performance::
  34. Swift::init(function () {
  35. Swift_DependencyContainer::getInstance()
  36. ->register('mime.qpcontentencoder')
  37. ->asAliasOf('mime.nativeqpcontentencoder');
  38. });