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