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