added saving attachments again, and removed a "click here" screen after
[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
3379d740 36 fputs ($fp, "--".mimeBoundary()."\r\n");
4ba45d11 37 fputs ($fp, "Content-Type: $filetype\n");
3379d740 38 fputs ($fp, "Content-Disposition: attachment; filename=\"$remotename\"\r\n");
39 fputs ($fp, "Content-Transfer-Encoding: base64\r\n\r\n");
4ba45d11 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 */
3379d740 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
465db5d7 119
120 if ($cc_list) {
3379d740 121 fputs($fp, "Cc: $cc_list\r\n"); // Who the CCs are
465db5d7 122 }
df15de21 123
124 if ($reply_to != "")
3379d740 125 fputs($fp, "Reply-To: $reply_to\r\n");
df15de21 126
127 if ($useSendmail) {
128 if ($bcc_list) {
129 // BCCs is removed from header by sendmail
3379d740 130 fputs($fp, "Bcc: $bcc_list\r\n");
df15de21 131 }
465db5d7 132 }
df15de21 133
3379d740 134 fputs($fp, "X-Mailer: SquirrelMail (version $version)\r\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());
3379d740 142 fputs ($fp, "\"\r\n");
df15de21 143 } else {
3379d740 144 fputs($fp, "Content-Type: text/plain; charset=ISO-8859-1\r\n");
145 fputs($fp, "Content-Transfer-Encoding: 8bit\r\n");
df15de21 146 }
3379d740 147 fputs ($fp, "\r\n");
df15de21 148 }
149
150 // Send the body
151 function writeBody ($fp, $body) {
152 if (isMultipart()) {
3379d740 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");
4ba45d11 157 attachFiles($fp);
3379d740 158 fputs ($fp, "\r\n--".mimeBoundary()."--\r\n");
df15de21 159 } else {
3379d740 160 fputs ($fp, stripslashes($body) . "\r\n");
df15de21 161 }
3379d740 162 fputs ($fp, "\r\n");
c99c5b31 163 }
465db5d7 164
c99c5b31 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
33244b71 170 $fp = popen (escapeshellcmd("$sendmail_path -odb -oi -t -f$username@$domain"), "w");
c99c5b31 171
172 write822Header ($fp, $t, $c, $b, $subject);
df15de21 173 writeBody($fp, $body);
465db5d7 174
175 pclose($fp);
176 }
177
b8ea4ed6 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
c99c5b31 189 function sendSMTP($t, $c, $b, $subject, $body) {
356d7825 190 global $username, $domain, $version, $smtpServerAddress, $smtpPort,
191 $data_dir;
7831268e 192
40ee9452 193 $to = parseAddrs($t);
194 $cc = parseAddrs($c);
195 $bcc = parseAddrs($b);
356d7825 196 $from_addr = getPref($data_dir, $username, "email_address");
197
198 if ($from_addr == "")
199 $from_addr = "$username@$domain";
d3cdb279 200
c175e2e4 201 $smtpConnection = fsockopen($smtpServerAddress, $smtpPort, $errorNumber, $errorString);
b8ea4ed6 202 if (!$smtpConnection) {
203 echo "Error connecting to SMTP Server.<br>";
204 echo "$errorNumber : $errorString<br>";
205 exit;
206 }
d3cdb279 207 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
3021b626 208 errorCheck($tmp);
b8ea4ed6 209
40ee9452 210 $to_list = getLineOfAddrs($to);
211 $cc_list = getLineOfAddrs($cc);
212
213 /** Lets introduce ourselves */
3379d740 214 fputs($smtpConnection, "HELO $domain\r\n");
d3cdb279 215 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
3021b626 216 errorCheck($tmp);
7831268e 217
40ee9452 218 /** Ok, who is sending the message? */
3379d740 219 fputs($smtpConnection, "MAIL FROM:<$from_addr>\r\n");
d3cdb279 220 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
3021b626 221 errorCheck($tmp);
b8ea4ed6 222
40ee9452 223 /** send who the recipients are */
224 for ($i = 0; $i < count($to); $i++) {
3379d740 225 fputs($smtpConnection, "RCPT TO:<$to[$i]>\r\n");
d3cdb279 226 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
3021b626 227 errorCheck($tmp);
40ee9452 228 }
229 for ($i = 0; $i < count($cc); $i++) {
3379d740 230 fputs($smtpConnection, "RCPT TO:<$cc[$i]>\r\n");
d3cdb279 231 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
3021b626 232 errorCheck($tmp);
40ee9452 233 }
234 for ($i = 0; $i < count($bcc); $i++) {
3379d740 235 fputs($smtpConnection, "RCPT TO:<$bcc[$i]>\r\n");
d3cdb279 236 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
3021b626 237 errorCheck($tmp);
40ee9452 238 }
b8ea4ed6 239
40ee9452 240 /** Lets start sending the actual message */
3379d740 241 fputs($smtpConnection, "DATA\r\n");
d3cdb279 242 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
3021b626 243 errorCheck($tmp);
7831268e 244
c99c5b31 245 write822Header ($smtpConnection, $t, $c, $b, $subject);
40ee9452 246
df15de21 247 writeBody($smtpConnection, $body); // send the body of the message
7831268e 248
3379d740 249 fputs($smtpConnection, ".\r\n"); // end the DATA part
d3cdb279 250 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
3021b626 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 }
d3cdb279 255
3379d740 256 fputs($smtpConnection, "QUIT\r\n"); // log off
78509c54 257
258 fclose($smtpConnection);
b8ea4ed6 259 }
3021b626 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 }
df15de21 356
357 function sendMessage($t, $c, $b, $subject, $body) {
358 global $useSendmail;
d17b1a71 359 global $data_dir, $username, $domain, $key, $version, $sent_folder, $imapServerAddress;
d1b8b679 360
df15de21 361 if ($useSendmail==true) {
d1b8b679 362 sendSendmail($t, $c, $b, $subject, $body);
df15de21 363 } else {
d1b8b679 364 sendSMTP($t, $c, $b, $subject, $body);
df15de21 365 }
d1b8b679 366
fc3348ac 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);
df15de21 369 }
370
465db5d7 371?>