compose.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 1999-2025 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. * @param boolean $only_build_message_object When TRUE, only builds the
  73. * message object that it
  74. * intends to send and returns
  75. * it (returned success code
  76. * will be -1 and message ID
  77. * emtpy) (OPTIONAL; default
  78. * is FALSE)
  79. *
  80. * @return array A three-element array, the first element being a
  81. * boolean value indicating if the message was successfully
  82. * sent or not, the second element being the message's
  83. * assigned Message-ID, if available (only available as of
  84. * SquirrelMail 1.4.14 and 1.5.2), and the third element
  85. * being the message object itself.
  86. * If $only_build_message_object is TRUE, only the third
  87. * element is useful; first two should be ignored - the
  88. * message is never sent in this case.
  89. *
  90. */
  91. function sq_send_mail($to, $subject, $body, $from, $cc='', $bcc='',
  92. $message='', $only_build_message_object=FALSE)
  93. {
  94. require_once(SM_PATH . 'functions/mime.php');
  95. require_once(SM_PATH . 'class/mime.class.php');
  96. if (empty($message))
  97. {
  98. $message = new Message();
  99. $header = new Rfc822Header();
  100. $message->setBody($body);
  101. $content_type = new ContentType('text/plain');
  102. global $special_encoding, $default_charset;
  103. if ($special_encoding)
  104. $header->encoding = $special_encoding;
  105. else
  106. $header->encoding = '8bit';
  107. if ($default_charset)
  108. $content_type->properties['charset']=$default_charset;
  109. $header->content_type = $content_type;
  110. $header->parseField('To', $to);
  111. $header->parseField('Cc', $cc);
  112. $header->parseField('Bcc', $bcc);
  113. $header->parseField('From', $from);
  114. $header->parseField('Subject', $subject);
  115. $message->rfc822_header = $header;
  116. }
  117. //sm_print_r($message);exit;
  118. if ($only_build_message_object)
  119. return array(-1, '', $message);
  120. global $useSendmail;
  121. // ripped from src/compose.php - based on both 1.5.2 and 1.4.14
  122. //
  123. if (!$useSendmail) {
  124. require_once(SM_PATH . 'class/deliver/Deliver_SMTP.class.php');
  125. $deliver = new Deliver_SMTP();
  126. global $smtpServerAddress, $smtpPort, $pop_before_smtp,
  127. $domain, $pop_before_smtp_host, $smtp_stream_options;
  128. $authPop = (isset($pop_before_smtp) && $pop_before_smtp) ? true : false;
  129. if (empty($pop_before_smtp_host)) $pop_before_smtp_host = $smtpServerAddress;
  130. $user = '';
  131. $pass = '';
  132. get_smtp_user($user, $pass);
  133. $stream = $deliver->initStream($message,$domain,0,
  134. $smtpServerAddress, $smtpPort, $user, $pass, $authPop, $pop_before_smtp_host, $smtp_stream_options);
  135. } else {
  136. require_once(SM_PATH . 'class/deliver/Deliver_SendMail.class.php');
  137. global $sendmail_path, $sendmail_args;
  138. // Check for outdated configuration
  139. if (!isset($sendmail_args)) {
  140. if ($sendmail_path=='/var/qmail/bin/qmail-inject') {
  141. $sendmail_args = '';
  142. } else {
  143. $sendmail_args = '-i -t';
  144. }
  145. }
  146. $deliver = new Deliver_SendMail(array('sendmail_args'=>$sendmail_args));
  147. $stream = $deliver->initStream($message,$sendmail_path);
  148. }
  149. $success = false;
  150. $message_id = '';
  151. if ($stream) {
  152. $deliver->mail($message, $stream);
  153. if (!empty($message->rfc822_header->message_id)) {
  154. $message_id = $message->rfc822_header->message_id;
  155. }
  156. $success = $deliver->finalizeStream($stream);
  157. }
  158. return array($success, $message_id, $message);
  159. }