Attachment.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. * Attachment class for attaching files to a {@link Swift_Mime_Message}.
  11. *
  12. * @author Chris Corbyn
  13. */
  14. class Swift_Attachment extends Swift_Mime_Attachment
  15. {
  16. /**
  17. * Create a new Attachment.
  18. *
  19. * Details may be optionally provided to the constructor.
  20. *
  21. * @param string|Swift_OutputByteStream $data
  22. * @param string $filename
  23. * @param string $contentType
  24. */
  25. public function __construct($data = null, $filename = null, $contentType = null)
  26. {
  27. call_user_func_array(
  28. array($this, 'Swift_Mime_Attachment::__construct'),
  29. Swift_DependencyContainer::getInstance()
  30. ->createDependenciesFor('mime.attachment')
  31. );
  32. $this->setBody($data);
  33. $this->setFilename($filename);
  34. if ($contentType) {
  35. $this->setContentType($contentType);
  36. }
  37. }
  38. /**
  39. * Create a new Attachment.
  40. *
  41. * @param string|Swift_OutputByteStream $data
  42. * @param string $filename
  43. * @param string $contentType
  44. *
  45. * @return Swift_Mime_Attachment
  46. */
  47. public static function newInstance($data = null, $filename = null, $contentType = null)
  48. {
  49. return new self($data, $filename, $contentType);
  50. }
  51. /**
  52. * Create a new Attachment from a filesystem path.
  53. *
  54. * @param string $path
  55. * @param string $contentType optional
  56. *
  57. * @return Swift_Mime_Attachment
  58. */
  59. public static function fromPath($path, $contentType = null)
  60. {
  61. return self::newInstance()->setFile(
  62. new Swift_ByteStream_FileByteStream($path),
  63. $contentType
  64. );
  65. }
  66. }