Attachment.php 2.0KB

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