EmbeddedFile.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. /**
  3. * Swift Mailer Embedded File (like an image or a midi file)
  4. * Please read the LICENSE file
  5. * @copyright Chris Corbyn <chris@w3style.co.uk>
  6. * @author Chris Corbyn <chris@w3style.co.uk>
  7. * @package Swift_Message
  8. * @license GNU Lesser General Public License
  9. */
  10. require_once dirname(__FILE__) . "/../ClassLoader.php";
  11. Swift_ClassLoader::load("Swift_Message_Attachment");
  12. /**
  13. * Embedded File component for Swift Mailer
  14. * @package Swift_Message
  15. * @author Chris Corbyn <chris@w3style.co.uk>
  16. */
  17. class Swift_Message_EmbeddedFile extends Swift_Message_Attachment
  18. {
  19. /**
  20. * The content-id in the headers (used in <img src=...> values)
  21. * @var string
  22. */
  23. protected $cid = null;
  24. /**
  25. * Constructor
  26. * @param mixed The input source. Can be a file or a string
  27. * @param string The filename to use, optional
  28. * @param string The MIME type to use, optional
  29. * @param string The Content-ID to use, optional
  30. * @param string The encoding format to use, optional
  31. */
  32. public function __construct($data=null, $name=null, $type="application/octet-stream", $cid=null, $encoding="base64")
  33. {
  34. parent::__construct($data, $name, $type, $encoding, "inline");
  35. if ($cid === null)
  36. {
  37. $cid = self::generateFileName("swift-" . uniqid(time()) . ".");
  38. $cid = urlencode($cid) . "@" . (!empty($_SERVER["SERVER_NAME"]) ? $_SERVER["SERVER_NAME"] : "swift");
  39. }
  40. $this->setContentId($cid);
  41. if ($name === null && !($data instanceof Swift_File)) $this->setFileName($cid);
  42. $this->headers->set("Content-Description", null);
  43. }
  44. /**
  45. * Get the level in the MIME hierarchy at which this section should appear.
  46. * @return string
  47. */
  48. public function getLevel()
  49. {
  50. return Swift_Message_Mime::LEVEL_RELATED;
  51. }
  52. /**
  53. * Set the Content-Id to use
  54. * @param string The content-id
  55. */
  56. public function setContentId($id)
  57. {
  58. $id = (string) $id;
  59. $this->cid = $id;
  60. $this->headers->set("Content-ID", "<" . $id . ">");
  61. }
  62. /**
  63. * Get the content-id of this file
  64. * @return string
  65. */
  66. public function getContentId()
  67. {
  68. return $this->cid;
  69. }
  70. }