6 ** Copyright (c) 1999-2000 The SquirrelMail development team
7 ** Licensed under the GNU GPL. For full terms see the file COPYING.
13 require_once ('../src/validate.php');
15 /* Print all the needed RFC822 headers */
16 function write822HeaderForDraft ($fp, $t, $c, $b, $subject, $more_headers) {
17 global $REMOTE_ADDR, $SERVER_NAME, $REMOTE_PORT;
18 global $data_dir, $username, $popuser, $domain, $version, $useSendmail;
19 global $default_charset, $HTTP_VIA, $HTTP_X_FORWARDED_FOR;
20 global $REMOTE_HOST, $identity;
22 // Storing the header to make sure the header is the same
23 // everytime the header is printed.
24 static $header, $headerlength;
27 if (isset($identity) && ($identity != 'default')) {
28 $reply_to = getPref($data_dir, $username, 'reply_to' . $identity);
29 $from = getPref($data_dir, $username, 'full_name' . $identity);
30 $from_addr = getPref($data_dir, $username, 'email_address' . $identity);
32 $reply_to = getPref($data_dir, $username, 'reply_to');
33 $from = getPref($data_dir, $username, 'full_name');
34 $from_addr = getPref($data_dir, $username, 'email_address');
37 if ($from_addr == '') {
38 $from_addr = $popuser.'@'.$domain;
41 /* Encoding 8-bit characters and making from line */
42 $subject = encodeHeader($subject);
44 $from = "<$from_addr>";
46 $from = '"' . encodeHeader($from) . "\" <$from_addr>";
49 /* This creates an RFC 822 date */
50 $date = date("D, j M Y H:i:s ", mktime()) . timezone();
52 /* Create a message-id */
53 $message_id = '<' . $REMOTE_PORT . '.' . $REMOTE_ADDR . '.';
54 $message_id .= time() . '.squirrel@' . $SERVER_NAME .'>';
56 /* Insert header fields */
57 $header = "Message-ID: $message_id\r\n";
58 $header .= "Date: $date\r\n";
59 $header .= "Subject: $subject\r\n";
60 $header .= "From: $from\r\n";
61 $header .= "To: $t\r\n"; // Who it's TO
63 /* Insert headers from the $more_headers array */
64 if(is_array($more_headers)) {
66 while(list($h_name, $h_val) = each($more_headers)) {
67 $header .= sprintf("%s: %s\r\n", $h_name, $h_val);
72 $header .= "Cc: $c\r\n"; // Who the CCs are
76 $header .= "Bcc: $b\r\n"; // Who the BCCs are
80 $header .= "Reply-To: $reply_to\r\n";
82 $header .= "X-Mailer: SquirrelMail (version $version)\r\n"; // Identify SquirrelMail
84 /* Do the MIME-stuff */
85 $header .= "MIME-Version: 1.0\r\n";
88 $header .= 'Content-Type: multipart/mixed; boundary="';
89 $header .= mimeBoundary();
92 if ($default_charset != '')
93 $header .= "Content-Type: text/plain; charset=$default_charset\r\n";
95 $header .= "Content-Type: text/plain;\r\n";
96 $header .= "Content-Transfer-Encoding: 8bit\r\n";
98 $header .= "\r\n"; // One blank line to separate header and body
100 $headerlength = strlen($header);
104 fputs ($fp, $header);
106 return $headerlength;
110 function writeBodyForDraft ($fp, $passedBody) {
111 global $default_charset;
113 $attachmentlength = 0;
116 $body = '--'.mimeBoundary()."\r\n";
118 if ($default_charset != "")
119 $body .= "Content-Type: text/plain; charset=$default_charset\r\n";
121 $body .= "Content-Type: text/plain\r\n";
123 $body .= "Content-Transfer-Encoding: 8bit\r\n\r\n";
124 $body .= $passedBody . "\r\n\r\n";
127 $attachmentlength = attachFiles($fp);
129 if (!isset($postbody)) $postbody = "";
130 $postbody .= "\r\n--".mimeBoundary()."--\r\n\r\n";
131 fputs ($fp, $postbody);
133 $body = $passedBody . "\r\n";
136 fputs ($fp, $postbody);
139 return (strlen($body) +
strlen($postbody) +
$attachmentlength);
143 function saveMessageAsDraft($t, $c, $b, $subject, $body, $reply_id) {
144 global $useSendmail, $msg_id, $is_reply, $mailbox, $onetimepad;
145 global $data_dir, $username, $domain, $key, $version, $sent_folder, $imapServerAddress, $imapPort;
146 global $draft_folder, $attachment_dir;
147 $more_headers = Array();
149 $imap_stream = sqimap_login($username, $key, $imapServerAddress, $imapPort, 1);
151 $tmpDraftFile = "draft-" . GenerateRandomString(32, '', 7);
152 while ( file_exists($attachment_dir .$tmpDraftFile) )
153 $tmpDraftFile = "draft-" . GenerateRandomString(32, '', 7);
154 $fp = fopen($attachment_dir . $tmpDraftFile, 'w');
156 $headerlength = write822HeaderForDraft ($fp, $t, $c, $b, $subject, $more_headers, FALSE);
157 $bodylength = writeBodyForDraft ($fp, $body, FALSE);
160 $length = ($headerlength +
$bodylength);
162 if (sqimap_mailbox_exists ($imap_stream, $draft_folder)) {
163 sqimap_append ($imap_stream, $draft_folder, $length);
164 write822HeaderForDraft ($imap_stream, $t, $c, $b, $subject, $more_headers, TRUE);
165 writeBodyForDraft ($imap_stream, $body, TRUE);
166 sqimap_append_done ($imap_stream);
168 sqimap_logout($imap_stream);
171 if (file_exists($attachment_dir . $tmpDraftFile) )
172 unlink ($attachment_dir . $tmpDraftFile);