deleting of folders should work now
[squirrelmail.git] / functions / smtp.php
CommitLineData
b8ea4ed6 1<?
2 /** smtp.php
3 **
4 ** This contains all the functions needed to send messages through
36fa79c8 5 ** an smtp server or sendmail.
b8ea4ed6 6 **/
7
d068c0ec 8 $smtp_php = true;
465db5d7 9
df15de21 10 // Returns true only if this message is multipart
11 function isMultipart () {
4ba45d11 12 global $attachments;
13
14 if (count($attachments)>0)
15 return true;
16 else
17 return false;
df15de21 18 }
19
20 // Attach the files that are due to be attached
4ba45d11 21 function attachFiles ($fp) {
c3c37167 22 global $attachments, $attachment_dir;
4ba45d11 23
24 while (list($localname, $remotename) = each($attachments)) {
05c8cad9 25 // This is to make sure noone is giving a filename in another
26 // directory
27 $localname = ereg_replace ("\\/", "", $localname);
28
c3c37167 29 $fileinfo = fopen ($attachment_dir.$localname.".info", "r");
4ba45d11 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
c3c37167 41 $file = fopen ($attachment_dir.$localname, "r");
4ba45d11 42 while ($tmp = fread($file, 57))
43 fputs ($fp, chunk_split(base64_encode($tmp)));
44 fclose ($file);
45
c3c37167 46 unlink ($attachment_dir.$localname);
47 unlink ($attachment_dir.$localname.".info");
4ba45d11 48 }
df15de21 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;
4ba45d11 59 $mimeBoundaryString = "=-_+".substr(md5($temp),1,20);
465db5d7 60 }
df15de21 61
62 return $mimeBoundaryString;
465db5d7 63 }
64
24397423 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
df15de21 83 /* Print all the needed RFC822 headers */
c99c5b31 84 function write822Header ($fp, $t, $c, $b, $subject) {
85 global $REMOTE_ADDR, $SERVER_NAME;
df15de21 86 global $data_dir, $username, $domain, $version, $useSendmail;
465db5d7 87
465db5d7 88 $to = parseAddrs($t);
89 $cc = parseAddrs($c);
90 $bcc = parseAddrs($b);
465db5d7 91 $reply_to = getPref($data_dir, $username, "reply_to");
92 $from = getPref($data_dir, $username, "full_name");
356d7825 93 $from_addr = getPref($data_dir, $username, "email_address");
94
95 if ($from_addr == "")
96 $from_addr = "$username@$domain";
465db5d7 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
df15de21 107 /* This creates an RFC 822 date showing GMT */
24397423 108 $date = date("D, j M Y H:i:s ", mktime()) . timezone();
c99c5b31 109
df15de21 110 /* Make an RFC822 Received: line */
c99c5b31 111 fputs ($fp, "Received: from $REMOTE_ADDR by $SERVER_NAME with HTTP; ");
112 fputs ($fp, "$date\n");
113
df15de21 114 /* The rest of the header */
c99c5b31 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
465db5d7 119
120 if ($cc_list) {
121 fputs($fp, "Cc: $cc_list\n"); // Who the CCs are
122 }
df15de21 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 }
465db5d7 132 }
df15de21 133
465db5d7 134 fputs($fp, "X-Mailer: SquirrelMail (version $version)\n"); // Identify SquirrelMail
df15de21 135
136 // Do the MIME-stuff
465db5d7 137 fputs($fp, "MIME-Version: 1.0\n");
df15de21 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");
b75c48df 155 fputs ($fp, stripslashes($body) . "\n");
4ba45d11 156 attachFiles($fp);
df15de21 157 fputs ($fp, "\n--".mimeBoundary()."--\n");
158 } else {
b75c48df 159 fputs ($fp, stripslashes($body) . "\n");
df15de21 160 }
c99c5b31 161 }
465db5d7 162
c99c5b31 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
33244b71 168 $fp = popen (escapeshellcmd("$sendmail_path -odb -oi -t -f$username@$domain"), "w");
c99c5b31 169
170 write822Header ($fp, $t, $c, $b, $subject);
df15de21 171 writeBody($fp, $body);
465db5d7 172
173 pclose($fp);
174 }
175
b8ea4ed6 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
c99c5b31 187 function sendSMTP($t, $c, $b, $subject, $body) {
356d7825 188 global $username, $domain, $version, $smtpServerAddress, $smtpPort,
189 $data_dir;
7831268e 190
40ee9452 191 $to = parseAddrs($t);
192 $cc = parseAddrs($c);
193 $bcc = parseAddrs($b);
356d7825 194 $from_addr = getPref($data_dir, $username, "email_address");
195
196 if ($from_addr == "")
197 $from_addr = "$username@$domain";
d3cdb279 198
c175e2e4 199 $smtpConnection = fsockopen($smtpServerAddress, $smtpPort, $errorNumber, $errorString);
b8ea4ed6 200 if (!$smtpConnection) {
201 echo "Error connecting to SMTP Server.<br>";
202 echo "$errorNumber : $errorString<br>";
203 exit;
204 }
d3cdb279 205 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
3021b626 206 errorCheck($tmp);
b8ea4ed6 207
40ee9452 208 $to_list = getLineOfAddrs($to);
209 $cc_list = getLineOfAddrs($cc);
210
211 /** Lets introduce ourselves */
212 fputs($smtpConnection, "HELO $domain\n");
d3cdb279 213 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
3021b626 214 errorCheck($tmp);
7831268e 215
40ee9452 216 /** Ok, who is sending the message? */
33daaa7d 217 fputs($smtpConnection, "MAIL FROM:<$from_addr>\n");
d3cdb279 218 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
3021b626 219 errorCheck($tmp);
b8ea4ed6 220
40ee9452 221 /** send who the recipients are */
222 for ($i = 0; $i < count($to); $i++) {
223 fputs($smtpConnection, "RCPT TO:<$to[$i]>\n");
d3cdb279 224 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
3021b626 225 errorCheck($tmp);
40ee9452 226 }
227 for ($i = 0; $i < count($cc); $i++) {
228 fputs($smtpConnection, "RCPT TO:<$cc[$i]>\n");
d3cdb279 229 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
3021b626 230 errorCheck($tmp);
40ee9452 231 }
232 for ($i = 0; $i < count($bcc); $i++) {
233 fputs($smtpConnection, "RCPT TO:<$bcc[$i]>\n");
d3cdb279 234 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
3021b626 235 errorCheck($tmp);
40ee9452 236 }
b8ea4ed6 237
40ee9452 238 /** Lets start sending the actual message */
b8ea4ed6 239 fputs($smtpConnection, "DATA\n");
d3cdb279 240 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
3021b626 241 errorCheck($tmp);
7831268e 242
c99c5b31 243 write822Header ($smtpConnection, $t, $c, $b, $subject);
40ee9452 244
df15de21 245 writeBody($smtpConnection, $body); // send the body of the message
7831268e 246
40ee9452 247 fputs($smtpConnection, ".\n"); // end the DATA part
d3cdb279 248 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
3021b626 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 }
d3cdb279 253
40ee9452 254 fputs($smtpConnection, "QUIT\n"); // log off
78509c54 255
256 fclose($smtpConnection);
b8ea4ed6 257 }
3021b626 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 }
df15de21 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
465db5d7 366?>