Attachment.php 1.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. * @package Swift
  12. * @subpackage Mime
  13. * @author Chris Corbyn
  14. */
  15. class Swift_Attachment extends Swift_Mime_Attachment
  16. {
  17. /**
  18. * Create a new Attachment.
  19. * Details may be optionally provided to the constructor.
  20. * @param string|Swift_OutputByteStream $data
  21. * @param string $filename
  22. * @param string $contentType
  23. */
  24. public function __construct($data = null, $filename = null,
  25. $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. {
  36. $this->setContentType($contentType);
  37. }
  38. }
  39. /**
  40. * Create a new Attachment.
  41. * @param string|Swift_OutputByteStream $data
  42. * @param string $filename
  43. * @param string $contentType
  44. * @return Swift_Mime_Attachment
  45. */
  46. public static function newInstance($data = null, $filename = null,
  47. $contentType = null)
  48. {
  49. return new self($data, $filename, $contentType);
  50. }
  51. /**
  52. * Create a new Attachment from a filesystem path.
  53. * @param string $path
  54. * @param string $contentType optional
  55. * @return Swift_Mime_Attachment
  56. */
  57. public static function fromPath($path, $contentType = null)
  58. {
  59. return self::newInstance()->setFile(
  60. new Swift_ByteStream_FileByteStream($path),
  61. $contentType
  62. );
  63. }
  64. }