removed saving sent for now
[squirrelmail.git] / functions / smtp.php
1 <?
2 /** smtp.php
3 **
4 ** This contains all the functions needed to send messages through
5 ** an smtp server or sendmail.
6 **/
7
8 $smtp_php = true;
9
10 // Returns true only if this message is multipart
11 function isMultipart () {
12 global $attachments;
13
14 if (count($attachments)>0)
15 return true;
16 else
17 return false;
18 }
19
20 // Attach the files that are due to be attached
21 function attachFiles ($fp) {
22 global $attachments, $attachment_dir;
23
24 while (list($localname, $remotename) = each($attachments)) {
25 // This is to make sure noone is giving a filename in another
26 // directory
27 $localname = ereg_replace ("\\/", "", $localname);
28
29 $fileinfo = fopen ($attachment_dir.$localname.".info", "r");
30 $filetype = fgets ($fileinfo, 8192);
31 fclose ($fileinfo);
32 $filetype = trim ($filetype);
33 if ($filetype=="")
34 $filetype = "application/octet-stream";
35
36 fputs ($fp, "--".mimeBoundary()."\r\n");
37 fputs ($fp, "Content-Type: $filetype\n");
38 fputs ($fp, "Content-Disposition: attachment; filename=\"$remotename\"\r\n");
39 fputs ($fp, "Content-Transfer-Encoding: base64\r\n\r\n");
40
41 $file = fopen ($attachment_dir.$localname, "r");
42 while ($tmp = fread($file, 57))
43 fputs ($fp, chunk_split(base64_encode($tmp)));
44 fclose ($file);
45
46 unlink ($attachment_dir.$localname);
47 unlink ($attachment_dir.$localname.".info");
48 }
49 }
50
51 // Return a nice MIME-boundary
52 function mimeBoundary () {
53 global $mimeBoundaryString, $version, $REMOTE_ADDR, $SERVER_NAME,
54 $REMOTE_PORT;
55
56 if ($mimeBoundaryString == "") {
57 $temp = "SquirrelMail".$version.$REMOTE_ADDR.$SERVER_NAME.
58 $REMOTE_PORT;
59 $mimeBoundaryString = "=-_+".substr(md5($temp),1,20);
60 }
61
62 return $mimeBoundaryString;
63 }
64
65 /* Time offset for correct timezone */
66 function timezone () {
67 $diff_second = date("Z");
68 if ($diff_second > 0)
69 $sign = "+";
70 else
71 $sign = "-";
72
73 $diff_second = abs($diff_second);
74
75 $diff_hour = floor ($diff_second / 3600);
76 $diff_minute = floor (($diff_second-3600*$diff_hour) / 60);
77
78 $zonename = "(".strftime("%Z").")";
79 $result = sprintf ("%s%02d%02d %s", $sign, $diff_hour, $diff_minute, $zonename);
80 return ($result);
81 }
82
83 /* Print all the needed RFC822 headers */
84 function write822Header ($fp, $t, $c, $b, $subject) {
85 global $REMOTE_ADDR, $SERVER_NAME;
86 global $data_dir, $username, $domain, $version, $useSendmail;
87
88 $to = parseAddrs($t);
89 $cc = parseAddrs($c);
90 $bcc = parseAddrs($b);
91 $reply_to = getPref($data_dir, $username, "reply_to");
92 $from = getPref($data_dir, $username, "full_name");
93 $from_addr = getPref($data_dir, $username, "email_address");
94
95 if ($from_addr == "")
96 $from_addr = "$username@$domain";
97
98 $to_list = getLineOfAddrs($to);
99 $cc_list = getLineOfAddrs($cc);
100 $bcc_list = getLineOfAddrs($bcc);
101
102 if ($from == "")
103 $from = "<$from_addr>";
104 else
105 $from = $from . " <$from_addr>";
106
107 /* This creates an RFC 822 date showing GMT */
108 $date = date("D, j M Y H:i:s ", mktime()) . timezone();
109
110 /* Make an RFC822 Received: line */
111 fputs ($fp, "Received: from $REMOTE_ADDR by $SERVER_NAME with HTTP; ");
112 fputs ($fp, "$date\n");
113
114 /* The rest of the header */
115 fputs ($fp, "Date: $date\r\n");
116 fputs ($fp, "Subject: $subject\r\n"); // Subject
117 fputs ($fp, "From: $from\r\n"); // Subject
118 fputs ($fp, "To: $to_list\r\n"); // Who it's TO
119
120 if ($cc_list) {
121 fputs($fp, "Cc: $cc_list\r\n"); // Who the CCs are
122 }
123
124 if ($reply_to != "")
125 fputs($fp, "Reply-To: $reply_to\r\n");
126
127 if ($useSendmail) {
128 if ($bcc_list) {
129 // BCCs is removed from header by sendmail
130 fputs($fp, "Bcc: $bcc_list\r\n");
131 }
132 }
133
134 fputs($fp, "X-Mailer: SquirrelMail (version $version)\r\n"); // Identify SquirrelMail
135
136 // Do the MIME-stuff
137 fputs($fp, "MIME-Version: 1.0\n");
138
139 if (isMultipart()) {
140 fputs ($fp, "Content-Type: multipart/mixed; boundary=\"");
141 fputs ($fp, mimeBoundary());
142 fputs ($fp, "\"\r\n");
143 } else {
144 fputs($fp, "Content-Type: text/plain; charset=ISO-8859-1\r\n");
145 fputs($fp, "Content-Transfer-Encoding: 8bit\r\n");
146 }
147 fputs ($fp, "\r\n");
148 }
149
150 // Send the body
151 function writeBody ($fp, $body) {
152 if (isMultipart()) {
153 fputs ($fp, "--".mimeBoundary()."\r\n");
154 fputs ($fp, "Content-Type: text/plain; charset=ISO-8859-1\r\n");
155 fputs ($fp, "Content-Transfer-Encoding: 8bit\r\n\r\n");
156 fputs ($fp, stripslashes($body) . "\r\n");
157 attachFiles($fp);
158 fputs ($fp, "\r\n--".mimeBoundary()."--\r\n");
159 } else {
160 fputs ($fp, stripslashes($body) . "\r\n");
161 }
162 fputs ($fp, "\r\n");
163 }
164
165 // Send mail using the sendmail command
166 function sendSendmail($t, $c, $b, $subject, $body) {
167 global $sendmail_path, $username, $domain;
168
169 // open pipe to sendmail
170 $fp = popen (escapeshellcmd("$sendmail_path -odb -oi -t -f$username@$domain"), "w");
171
172 write822Header ($fp, $t, $c, $b, $subject);
173 writeBody($fp, $body);
174
175 pclose($fp);
176 }
177
178 function smtpReadData($smtpConnection) {
179 $read = fgets($smtpConnection, 1024);
180 $counter = 0;
181 while ($read) {
182 echo $read . "<BR>";
183 $data[$counter] = $read;
184 $read = fgets($smtpConnection, 1024);
185 $counter++;
186 }
187 }
188
189 function sendSMTP($t, $c, $b, $subject, $body) {
190 global $username, $domain, $version, $smtpServerAddress, $smtpPort,
191 $data_dir;
192
193 $to = parseAddrs($t);
194 $cc = parseAddrs($c);
195 $bcc = parseAddrs($b);
196 $from_addr = getPref($data_dir, $username, "email_address");
197
198 if ($from_addr == "")
199 $from_addr = "$username@$domain";
200
201 $smtpConnection = fsockopen($smtpServerAddress, $smtpPort, $errorNumber, $errorString);
202 if (!$smtpConnection) {
203 echo "Error connecting to SMTP Server.<br>";
204 echo "$errorNumber : $errorString<br>";
205 exit;
206 }
207 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
208 errorCheck($tmp);
209
210 $to_list = getLineOfAddrs($to);
211 $cc_list = getLineOfAddrs($cc);
212
213 /** Lets introduce ourselves */
214 fputs($smtpConnection, "HELO $domain\r\n");
215 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
216 errorCheck($tmp);
217
218 /** Ok, who is sending the message? */
219 fputs($smtpConnection, "MAIL FROM:<$from_addr>\r\n");
220 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
221 errorCheck($tmp);
222
223 /** send who the recipients are */
224 for ($i = 0; $i < count($to); $i++) {
225 fputs($smtpConnection, "RCPT TO:<$to[$i]>\r\n");
226 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
227 errorCheck($tmp);
228 }
229 for ($i = 0; $i < count($cc); $i++) {
230 fputs($smtpConnection, "RCPT TO:<$cc[$i]>\r\n");
231 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
232 errorCheck($tmp);
233 }
234 for ($i = 0; $i < count($bcc); $i++) {
235 fputs($smtpConnection, "RCPT TO:<$bcc[$i]>\r\n");
236 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
237 errorCheck($tmp);
238 }
239
240 /** Lets start sending the actual message */
241 fputs($smtpConnection, "DATA\r\n");
242 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
243 errorCheck($tmp);
244
245 write822Header ($smtpConnection, $t, $c, $b, $subject);
246
247 writeBody($smtpConnection, $body); // send the body of the message
248
249 fputs($smtpConnection, ".\r\n"); // end the DATA part
250 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
251 $num = errorCheck($tmp);
252 if ($num != 250) {
253 echo "<HTML><BODY BGCOLOR=FFFFFF>ERROR<BR>Message not sent!<BR>Reason given: $tmp<BR></BODY></HTML>";
254 }
255
256 fputs($smtpConnection, "QUIT\r\n"); // log off
257
258 fclose($smtpConnection);
259 }
260
261
262 function errorCheck($line) {
263 // Status: 0 = fatal
264 // 5 = ok
265
266 $err_num = substr($line, 0, strpos($line, " "));
267 switch ($err_num) {
268 case 500: $message = "Syntax error; command not recognized";
269 $status = 0;
270 break;
271 case 501: $message = "Syntax error in parameters or arguments";
272 $status = 0;
273 break;
274 case 502: $message = "Command not implemented";
275 $status = 0;
276 break;
277 case 503: $message = "Bad sequence of commands";
278 $status = 0;
279 break;
280 case 504: $message = "Command parameter not implemented";
281 $status = 0;
282 break;
283
284
285 case 211: $message = "System status, or system help reply";
286 $status = 5;
287 break;
288 case 214: $message = "Help message";
289 $status = 5;
290 break;
291
292
293 case 220: $message = "Service ready";
294 $status = 5;
295 break;
296 case 221: $message = "Service closing transmission channel";
297 $status = 5;
298 break;
299 case 421: $message = "Service not available, closing chanel";
300 $status = 0;
301 break;
302
303
304 case 250: $message = "Requested mail action okay, completed";
305 $status = 5;
306 break;
307 case 251: $message = "User not local; will forward";
308 $status = 5;
309 break;
310 case 450: $message = "Requested mail action not taken: mailbox unavailable";
311 $status = 0;
312 break;
313 case 550: $message = "Requested action not taken: mailbox unavailable";
314 $status = 0;
315 break;
316 case 451: $message = "Requested action aborted: error in processing";
317 $status = 0;
318 break;
319 case 551: $message = "User not local; please try forwarding";
320 $status = 0;
321 break;
322 case 452: $message = "Requested action not taken: insufficient system storage";
323 $status = 0;
324 break;
325 case 552: $message = "Requested mail action aborted: exceeding storage allocation";
326 $status = 0;
327 break;
328 case 553: $message = "Requested action not taken: mailbox name not allowed";
329 $status = 0;
330 break;
331 case 354: $message = "Start mail input; end with .";
332 $status = 5;
333 break;
334 case 554: $message = "Transaction failed";
335 $status = 0;
336 break;
337 default: $message = "Unknown response: $line";
338 $status = 0;
339 $error_num = "001";
340 break;
341 }
342
343 if ($status == 0) {
344 echo "<HTML><BODY BGCOLOR=FFFFFF>";
345 echo "<TT>";
346 echo "<BR><B>ERROR</B><BR><BR>";
347 echo "&nbsp;&nbsp;&nbsp;<B>Error Number: </B>$err_num<BR>";
348 echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<B>Reason: </B>$message<BR>";
349 echo "<B>Server Response: </B>$line<BR>";
350 echo "<BR>MAIL NOT SENT";
351 echo "</TT></BODY></HTML>";
352 exit;
353 }
354 return $err_num;
355 }
356
357 function sendMessage($t, $c, $b, $subject, $body) {
358 global $useSendmail;
359 global $data_dir, $username, $domain, $key, $version, $sent_folder, $imapServerAddress;
360
361 if ($useSendmail==true) {
362 sendSendmail($t, $c, $b, $subject, $body);
363 } else {
364 sendSMTP($t, $c, $b, $subject, $body);
365 }
366
367 // $imap_stream = sqimap_login($username, $key, $imapServerAddress, 1);
368 // sqimap_append ($imap_stream, $sent_folder, $body, $t, $c, $b, $subject, $data_dir, $username, $domain, $version);
369 }
370
371 ?>