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