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