compose.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. /**
  3. * compose.php
  4. *
  5. * Functions for message compositon: writing a message, attaching files etc.
  6. *
  7. * @author Thijs Kinkhorst <kink at squirrelmail.org>
  8. * @copyright &copy; 1999-2009 The SquirrelMail Project Team
  9. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  10. * @version $Id$
  11. * @package squirrelmail
  12. */
  13. /**
  14. * Get a new file to write an attachment to.
  15. * This function makes sure it doesn't overwrite other attachments,
  16. * preventing collisions and race conditions.
  17. *
  18. * @return filename of the tempfile only (not full path)
  19. * @since 1.5.2
  20. */
  21. function sq_get_attach_tempfile()
  22. {
  23. global $username, $attachment_dir;
  24. $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
  25. // using PHP >= 4.3.2 we can be truly atomic here
  26. $filemods = check_php_version ( 4,3,2 ) ? 'x' : 'w';
  27. // give up after 1000 tries
  28. $TMP_MAX = 1000;
  29. for ($try=0; $try<$TMP_MAX; ++$try) {
  30. $localfilename = GenerateRandomString(32, '', 7);
  31. $full_localfilename = "$hashed_attachment_dir/$localfilename";
  32. // filename collision. try again
  33. if ( file_exists($full_localfilename) ) {
  34. continue;
  35. }
  36. // try to open for (binary) writing
  37. $fp = @fopen( $full_localfilename, $filemods);
  38. if ( $fp !== FALSE ) {
  39. // success! make sure it's not readable, close and return filename
  40. chmod($full_localfilename, 0600);
  41. fclose($fp);
  42. return $localfilename;
  43. }
  44. }
  45. // we tried 1000 times but didn't succeed.
  46. error_box( _("Could not open temporary file to store attachment. Contact your system administrator to resolve this issue.") );
  47. return FALSE;
  48. }
  49. /**
  50. * Send a simple mail message using SquirrelMail's API.
  51. *
  52. * Until SquirrelMail is sufficiently redesigned, this
  53. * function is a stand-in for a simple mail delivery
  54. * call. Currently, it only sends plaintext messages
  55. * (unless the caller uses the $message parameter).
  56. *
  57. * @param string $to The destination recipient.
  58. * @param string $subject The message subject.
  59. * @param string $body The message body.
  60. * @param string $from The sender.
  61. * @param string $cc The destination carbon-copy recipient.
  62. * (OPTIONAL; default no Cc:)
  63. * @param string $bcc The destination blind carbon-copy recipient.
  64. * (OPTIONAL; default no Bcc:)
  65. * @param object $message If the caller wants to construct a more
  66. * complicated message themselves and pass
  67. * it here, this function will take care
  68. * of the rest - handing it over to SMTP
  69. * or Sendmail. If this parameter is non-
  70. * empty, all other parameters are ignored.
  71. * (OPTIONAL: default is empty)
  72. *
  73. * @return array A two-element array, the first element being a
  74. * boolean value indicating if the message was successfully
  75. * sent or not, and the second element being the message's
  76. * assigned Message-ID, if available (only available as of
  77. * SquirrelMail 1.4.14 and 1.5.2)
  78. *
  79. */
  80. function sq_send_mail($to, $subject, $body, $from, $cc='', $bcc='', $message='')
  81. {
  82. require_once(SM_PATH . 'functions/mime.php');
  83. require_once(SM_PATH . 'class/mime.class.php');
  84. if (empty($message))
  85. {
  86. $message = new Message();
  87. $header = new Rfc822Header();
  88. $message->setBody($body);
  89. $content_type = new ContentType('text/plain');
  90. global $special_encoding, $default_charset;
  91. if ($special_encoding)
  92. $rfc822_header->encoding = $special_encoding;
  93. else
  94. $rfc822_header->encoding = '8bit';
  95. if ($default_charset)
  96. $content_type->properties['charset']=$default_charset;
  97. $header->content_type = $content_type;
  98. $header->parseField('To', $to);
  99. $header->parseField('Cc', $cc);
  100. $header->parseField('Bcc', $bcc);
  101. $header->parseField('From', $from);
  102. $header->parseField('Subject', $subject);
  103. $message->rfc822_header = $header;
  104. }
  105. //sm_print_r($message);exit;
  106. global $useSendmail;
  107. // ripped from src/compose.php - based on both 1.5.2 and 1.4.14
  108. //
  109. if (!$useSendmail) {
  110. require_once(SM_PATH . 'class/deliver/Deliver_SMTP.class.php');
  111. $deliver = new Deliver_SMTP();
  112. global $smtpServerAddress, $smtpPort, $pop_before_smtp,
  113. $domain, $pop_before_smtp_host;
  114. $authPop = (isset($pop_before_smtp) && $pop_before_smtp) ? true : false;
  115. if (empty($pop_before_smtp_host)) $pop_before_smtp_host = $smtpServerAddress;
  116. $user = '';
  117. $pass = '';
  118. get_smtp_user($user, $pass);
  119. $stream = $deliver->initStream($message,$domain,0,
  120. $smtpServerAddress, $smtpPort, $user, $pass, $authPop, $pop_before_smtp_host);
  121. } else {
  122. require_once(SM_PATH . 'class/deliver/Deliver_SendMail.class.php');
  123. global $sendmail_path, $sendmail_args;
  124. // Check for outdated configuration
  125. if (!isset($sendmail_args)) {
  126. if ($sendmail_path=='/var/qmail/bin/qmail-inject') {
  127. $sendmail_args = '';
  128. } else {
  129. $sendmail_args = '-i -t';
  130. }
  131. }
  132. $deliver = new Deliver_SendMail(array('sendmail_args'=>$sendmail_args));
  133. $stream = $deliver->initStream($message,$sendmail_path);
  134. }
  135. $success = false;
  136. $message_id = '';
  137. if ($stream) {
  138. $deliver->mail($message, $stream);
  139. if (!empty($message->rfc822_header->message_id)) {
  140. $message_id = $message->rfc822_header->message_id;
  141. }
  142. $success = $deliver->finalizeStream($stream);
  143. }
  144. return array($success, $message_id);
  145. }