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