Fixed a bug in displaying email addresses in the message list.
[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
465db5d7 8
df15de21 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 () {
4ba45d11 14 global $attachments;
15
16 if (count($attachments)>0)
17 return true;
18 else
19 return false;
df15de21 20 }
21
22 // Attach the files that are due to be attached
4ba45d11 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 }
df15de21 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;
4ba45d11 57 $mimeBoundaryString = "=-_+".substr(md5($temp),1,20);
465db5d7 58 }
df15de21 59
60 return $mimeBoundaryString;
465db5d7 61 }
62
df15de21 63 /* Print all the needed RFC822 headers */
c99c5b31 64 function write822Header ($fp, $t, $c, $b, $subject) {
65 global $REMOTE_ADDR, $SERVER_NAME;
df15de21 66 global $data_dir, $username, $domain, $version, $useSendmail;
465db5d7 67
465db5d7 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
df15de21 84 /* This creates an RFC 822 date showing GMT */
c99c5b31 85 $date = date("D, j M Y H:i:s +0000", gmmktime());
86
df15de21 87 /* Make an RFC822 Received: line */
c99c5b31 88 fputs ($fp, "Received: from $REMOTE_ADDR by $SERVER_NAME with HTTP; ");
89 fputs ($fp, "$date\n");
90
df15de21 91 /* The rest of the header */
c99c5b31 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
465db5d7 96
97 if ($cc_list) {
98 fputs($fp, "Cc: $cc_list\n"); // Who the CCs are
99 }
df15de21 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 }
465db5d7 109 }
df15de21 110
465db5d7 111 fputs($fp, "X-Mailer: SquirrelMail (version $version)\n"); // Identify SquirrelMail
df15de21 112
113 // Do the MIME-stuff
465db5d7 114 fputs($fp, "MIME-Version: 1.0\n");
df15de21 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");
b75c48df 132 fputs ($fp, stripslashes($body) . "\n");
4ba45d11 133 attachFiles($fp);
df15de21 134 fputs ($fp, "\n--".mimeBoundary()."--\n");
135 } else {
b75c48df 136 fputs ($fp, stripslashes($body) . "\n");
df15de21 137 }
c99c5b31 138 }
465db5d7 139
c99c5b31 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
df15de21 145 $fp = popen (escapeshellcmd("$sendmail_path -t -f$username@$domain"), "w");
c99c5b31 146
147 write822Header ($fp, $t, $c, $b, $subject);
df15de21 148 writeBody($fp, $body);
465db5d7 149
150 pclose($fp);
151 }
152
b8ea4ed6 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
c99c5b31 164 function sendSMTP($t, $c, $b, $subject, $body) {
165 global $username, $domain, $version, $smtpServerAddress, $smtpPort;
7831268e 166
40ee9452 167 $to = parseAddrs($t);
168 $cc = parseAddrs($c);
169 $bcc = parseAddrs($b);
d3cdb279 170 $from_addr = "$username@$domain";
d3cdb279 171
c175e2e4 172 $smtpConnection = fsockopen($smtpServerAddress, $smtpPort, $errorNumber, $errorString);
b8ea4ed6 173 if (!$smtpConnection) {
174 echo "Error connecting to SMTP Server.<br>";
175 echo "$errorNumber : $errorString<br>";
176 exit;
177 }
d3cdb279 178 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
3021b626 179 errorCheck($tmp);
b8ea4ed6 180
40ee9452 181 $to_list = getLineOfAddrs($to);
182 $cc_list = getLineOfAddrs($cc);
183
184 /** Lets introduce ourselves */
185 fputs($smtpConnection, "HELO $domain\n");
d3cdb279 186 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
3021b626 187 errorCheck($tmp);
7831268e 188
40ee9452 189 /** Ok, who is sending the message? */
33daaa7d 190 fputs($smtpConnection, "MAIL FROM:<$from_addr>\n");
d3cdb279 191 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
3021b626 192 errorCheck($tmp);
b8ea4ed6 193
40ee9452 194 /** send who the recipients are */
195 for ($i = 0; $i < count($to); $i++) {
196 fputs($smtpConnection, "RCPT TO:<$to[$i]>\n");
d3cdb279 197 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
3021b626 198 errorCheck($tmp);
40ee9452 199 }
200 for ($i = 0; $i < count($cc); $i++) {
201 fputs($smtpConnection, "RCPT TO:<$cc[$i]>\n");
d3cdb279 202 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
3021b626 203 errorCheck($tmp);
40ee9452 204 }
205 for ($i = 0; $i < count($bcc); $i++) {
206 fputs($smtpConnection, "RCPT TO:<$bcc[$i]>\n");
d3cdb279 207 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
3021b626 208 errorCheck($tmp);
40ee9452 209 }
b8ea4ed6 210
40ee9452 211 /** Lets start sending the actual message */
b8ea4ed6 212 fputs($smtpConnection, "DATA\n");
d3cdb279 213 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
3021b626 214 errorCheck($tmp);
7831268e 215
c99c5b31 216 write822Header ($smtpConnection, $t, $c, $b, $subject);
40ee9452 217
df15de21 218 writeBody($smtpConnection, $body); // send the body of the message
7831268e 219
40ee9452 220 fputs($smtpConnection, ".\n"); // end the DATA part
d3cdb279 221 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
3021b626 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 }
d3cdb279 226
40ee9452 227 fputs($smtpConnection, "QUIT\n"); // log off
78509c54 228
229 fclose($smtpConnection);
b8ea4ed6 230 }
3021b626 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 }
df15de21 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
465db5d7 339?>