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