Attachment.php 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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, $contentType = null)
  25. {
  26. call_user_func_array(
  27. array($this, 'Swift_Mime_Attachment::__construct'),
  28. Swift_DependencyContainer::getInstance()
  29. ->createDependenciesFor('mime.attachment')
  30. );
  31. $this->setBody($data);
  32. $this->setFilename($filename);
  33. if ($contentType) {
  34. $this->setContentType($contentType);
  35. }
  36. }
  37. /**
  38. * Create a new Attachment.
  39. * @param string|Swift_OutputByteStream $data
  40. * @param string $filename
  41. * @param string $contentType
  42. * @return Swift_Mime_Attachment
  43. */
  44. public static function newInstance($data = null, $filename = null, $contentType = null)
  45. {
  46. return new self($data, $filename, $contentType);
  47. }
  48. /**
  49. * Create a new Attachment from a filesystem path.
  50. * @param string $path
  51. * @param string $contentType optional
  52. * @return Swift_Mime_Attachment
  53. */
  54. public static function fromPath($path, $contentType = null)
  55. {
  56. return self::newInstance()->setFile(
  57. new Swift_ByteStream_FileByteStream($path),
  58. $contentType
  59. );
  60. }
  61. }