Added a constat to all files in functions/ to be able to chech whether the
[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()."\n");
37 fputs ($fp, "Content-Type: $filetype\n");
38 fputs ($fp, "Content-Disposition: attachment; filename=\"$remotename\"\n");
39 fputs ($fp, "Content-Transfer-Encoding: base64\n\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\n");
116 fputs ($fp, "Subject: $subject\n"); // Subject
117 fputs ($fp, "From: $from\n"); // Subject
118 fputs ($fp, "To: $to_list\n"); // Who it's TO
119
120 if ($cc_list) {
121 fputs($fp, "Cc: $cc_list\n"); // Who the CCs are
122 }
123
124 if ($reply_to != "")
125 fputs($fp, "Reply-To: $reply_to\n");
126
127 if ($useSendmail) {
128 if ($bcc_list) {
129 // BCCs is removed from header by sendmail
130 fputs($fp, "Bcc: $bcc_list\n");
131 }
132 }
133
134 fputs($fp, "X-Mailer: SquirrelMail (version $version)\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, "\"\n");
143 } else {
144 fputs($fp, "Content-Type: text/plain; charset=ISO-8859-1\n");
145 fputs($fp, "Content-Transfer-Encoding: 8bit\n");
146 }
147 }
148
149 // Send the body
150 function writeBody ($fp, $body) {
151 if (isMultipart()) {
152 fputs ($fp, "--".mimeBoundary()."\n");
153 fputs ($fp, "Content-Type: text/plain; charset=ISO-8859-1\n");
154 fputs ($fp, "Content-Transfer-Encoding: 8bit\n\n");
155 fputs ($fp, stripslashes($body) . "\n");
156 attachFiles($fp);
157 fputs ($fp, "\n--".mimeBoundary()."--\n");
158 } else {
159 fputs ($fp, stripslashes($body) . "\n");
160 }
161 }
162
163 // Send mail using the sendmail command
164 function sendSendmail($t, $c, $b, $subject, $body) {
165 global $sendmail_path, $username, $domain;
166
167 // open pipe to sendmail
168 $fp = popen (escapeshellcmd("$sendmail_path -odb -oi -t -f$username@$domain"), "w");
169
170 write822Header ($fp, $t, $c, $b, $subject);
171 writeBody($fp, $body);
172
173 pclose($fp);
174 }
175
176 function smtpReadData($smtpConnection) {
177 $read = fgets($smtpConnection, 1024);
178 $counter = 0;
179 while ($read) {
180 echo $read . "<BR>";
181 $data[$counter] = $read;
182 $read = fgets($smtpConnection, 1024);
183 $counter++;
184 }
185 }
186
187 function sendSMTP($t, $c, $b, $subject, $body) {
188 global $username, $domain, $version, $smtpServerAddress, $smtpPort,
189 $data_dir;
190
191 $to = parseAddrs($t);
192 $cc = parseAddrs($c);
193 $bcc = parseAddrs($b);
194 $from_addr = getPref($data_dir, $username, "email_address");
195
196 if ($from_addr == "")
197 $from_addr = "$username@$domain";
198
199 $smtpConnection = fsockopen($smtpServerAddress, $smtpPort, $errorNumber, $errorString);
200 if (!$smtpConnection) {
201 echo "Error connecting to SMTP Server.<br>";
202 echo "$errorNumber : $errorString<br>";
203 exit;
204 }
205 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
206 errorCheck($tmp);
207
208 $to_list = getLineOfAddrs($to);
209 $cc_list = getLineOfAddrs($cc);
210
211 /** Lets introduce ourselves */
212 fputs($smtpConnection, "HELO $domain\n");
213 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
214 errorCheck($tmp);
215
216 /** Ok, who is sending the message? */
217 fputs($smtpConnection, "MAIL FROM:<$from_addr>\n");
218 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
219 errorCheck($tmp);
220
221 /** send who the recipients are */
222 for ($i = 0; $i < count($to); $i++) {
223 fputs($smtpConnection, "RCPT TO:<$to[$i]>\n");
224 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
225 errorCheck($tmp);
226 }
227 for ($i = 0; $i < count($cc); $i++) {
228 fputs($smtpConnection, "RCPT TO:<$cc[$i]>\n");
229 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
230 errorCheck($tmp);
231 }
232 for ($i = 0; $i < count($bcc); $i++) {
233 fputs($smtpConnection, "RCPT TO:<$bcc[$i]>\n");
234 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
235 errorCheck($tmp);
236 }
237
238 /** Lets start sending the actual message */
239 fputs($smtpConnection, "DATA\n");
240 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
241 errorCheck($tmp);
242
243 write822Header ($smtpConnection, $t, $c, $b, $subject);
244
245 writeBody($smtpConnection, $body); // send the body of the message
246
247 fputs($smtpConnection, ".\n"); // end the DATA part
248 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
249 $num = errorCheck($tmp);
250 if ($num != 250) {
251 echo "<HTML><BODY BGCOLOR=FFFFFF>ERROR<BR>Message not sent!<BR>Reason given: $tmp<BR></BODY></HTML>";
252 }
253
254 fputs($smtpConnection, "QUIT\n"); // log off
255
256 fclose($smtpConnection);
257 }
258
259
260 function errorCheck($line) {
261 // Status: 0 = fatal
262 // 5 = ok
263
264 $err_num = substr($line, 0, strpos($line, " "));
265 switch ($err_num) {
266 case 500: $message = "Syntax error; command not recognized";
267 $status = 0;
268 break;
269 case 501: $message = "Syntax error in parameters or arguments";
270 $status = 0;
271 break;
272 case 502: $message = "Command not implemented";
273 $status = 0;
274 break;
275 case 503: $message = "Bad sequence of commands";
276 $status = 0;
277 break;
278 case 504: $message = "Command parameter not implemented";
279 $status = 0;
280 break;
281
282
283 case 211: $message = "System status, or system help reply";
284 $status = 5;
285 break;
286 case 214: $message = "Help message";
287 $status = 5;
288 break;
289
290
291 case 220: $message = "Service ready";
292 $status = 5;
293 break;
294 case 221: $message = "Service closing transmission channel";
295 $status = 5;
296 break;
297 case 421: $message = "Service not available, closing chanel";
298 $status = 0;
299 break;
300
301
302 case 250: $message = "Requested mail action okay, completed";
303 $status = 5;
304 break;
305 case 251: $message = "User not local; will forward";
306 $status = 5;
307 break;
308 case 450: $message = "Requested mail action not taken: mailbox unavailable";
309 $status = 0;
310 break;
311 case 550: $message = "Requested action not taken: mailbox unavailable";
312 $status = 0;
313 break;
314 case 451: $message = "Requested action aborted: error in processing";
315 $status = 0;
316 break;
317 case 551: $message = "User not local; please try forwarding";
318 $status = 0;
319 break;
320 case 452: $message = "Requested action not taken: insufficient system storage";
321 $status = 0;
322 break;
323 case 552: $message = "Requested mail action aborted: exceeding storage allocation";
324 $status = 0;
325 break;
326 case 553: $message = "Requested action not taken: mailbox name not allowed";
327 $status = 0;
328 break;
329 case 354: $message = "Start mail input; end with .";
330 $status = 5;
331 break;
332 case 554: $message = "Transaction failed";
333 $status = 0;
334 break;
335 default: $message = "Unknown response: $line";
336 $status = 0;
337 $error_num = "001";
338 break;
339 }
340
341 if ($status == 0) {
342 echo "<HTML><BODY BGCOLOR=FFFFFF>";
343 echo "<TT>";
344 echo "<BR><B>ERROR</B><BR><BR>";
345 echo "&nbsp;&nbsp;&nbsp;<B>Error Number: </B>$err_num<BR>";
346 echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<B>Reason: </B>$message<BR>";
347 echo "<B>Server Response: </B>$line<BR>";
348 echo "<BR>MAIL NOT SENT";
349 echo "</TT></BODY></HTML>";
350 exit;
351 }
352 return $err_num;
353 }
354
355 function sendMessage($t, $c, $b, $subject, $body) {
356 global $useSendmail;
357
358 if ($useSendmail==true) {
359 sendSendmail($t, $c, $b, $subject, $body);
360 } else {
361 sendSMTP($t, $c, $b, $subject, $body);
362 }
363
364 }
365
366 ?>