Some minro modification to the use of config.php
[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 function sendMessage($t, $c, $b, $subject, $body, $version) {
9 global $useSendmail;
10
11 if ($useSendmail==true) {
12 sendSendmail($t, $c, $b, $subject, $body, $version);
13 } else {
14 sendSMTP($t, $c, $b, $subject, $body, $version);
15 }
16
17 }
18
19 // Send mail using the sendmail command
20 function sendSendmail($t, $c, $b, $subject, $body, $version) {
21 global $username, $domain, $data_dir, $sendmail_path;
22
23 // This is pretty much equal to the code in sendSMTP
24 $to = parseAddrs($t);
25 $cc = parseAddrs($c);
26 $bcc = parseAddrs($b);
27 $from_addr = "$username@$domain";
28 $reply_to = getPref($data_dir, $username, "reply_to");
29 $from = getPref($data_dir, $username, "full_name");
30
31 $to_list = getLineOfAddrs($to);
32 $cc_list = getLineOfAddrs($cc);
33 $bcc_list = getLineOfAddrs($bcc);
34
35 if ($from == "")
36 $from = "<$from_addr>";
37 else
38 $from = $from . " <$from_addr>";
39
40 // open pipe to sendmail
41 $fp = popen ("$sendmail_path -t -f$from_addr", "w");
42
43 fputs($fp, "Subject: $subject\n"); // Subject
44 fputs($fp, "From: $from\n"); // Subject
45 fputs($fp, "To: $to_list\n"); // Who it's TO
46
47 if ($cc_list) {
48 fputs($fp, "Cc: $cc_list\n"); // Who the CCs are
49 }
50 if ($bcc_list) {
51 fputs($fp, "Bcc: $bcc_list\n"); // BCCs is removed from header by sendmail
52 }
53 fputs($fp, "X-Mailer: SquirrelMail (version $version)\n"); // Identify SquirrelMail
54 fputs($fp, "MIME-Version: 1.0\n");
55 fputs($fp, "Content-Type: text/plain\n");
56 if ($reply_to != "")
57 fputs($fp, "Reply-To: $reply_to\n");
58
59 fputs($fp, "\n$body\n"); // send the body of the message
60
61 pclose($fp);
62 }
63
64 function smtpReadData($smtpConnection) {
65 $read = fgets($smtpConnection, 1024);
66 $counter = 0;
67 while ($read) {
68 echo $read . "<BR>";
69 $data[$counter] = $read;
70 $read = fgets($smtpConnection, 1024);
71 $counter++;
72 }
73 }
74
75 function sendSMTP($t, $c, $b, $subject, $body, $version) {
76 global $username, $domain;
77
78 include("../config/config.php");
79
80 $to = parseAddrs($t);
81 $cc = parseAddrs($c);
82 $bcc = parseAddrs($b);
83 $from_addr = "$username@$domain";
84 $reply_to = getPref($data_dir, $username, "reply_to");
85 $from = getPref($data_dir, $username, "full_name");
86
87 if ($from == "")
88 $from = "<$from_addr>";
89 else
90 $from = $from . " <$from_addr>";
91
92 $smtpConnection = fsockopen($smtpServerAddress, $smtpPort, $errorNumber, $errorString);
93 if (!$smtpConnection) {
94 echo "Error connecting to SMTP Server.<br>";
95 echo "$errorNumber : $errorString<br>";
96 exit;
97 }
98 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
99 errorCheck($tmp);
100
101 $to_list = getLineOfAddrs($to);
102 $cc_list = getLineOfAddrs($cc);
103
104 /** Lets introduce ourselves */
105 fputs($smtpConnection, "HELO $domain\n");
106 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
107 errorCheck($tmp);
108
109 /** Ok, who is sending the message? */
110 fputs($smtpConnection, "MAIL FROM:<$from_addr>\n");
111 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
112 errorCheck($tmp);
113
114 /** send who the recipients are */
115 for ($i = 0; $i < count($to); $i++) {
116 fputs($smtpConnection, "RCPT TO:<$to[$i]>\n");
117 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
118 errorCheck($tmp);
119 }
120 for ($i = 0; $i < count($cc); $i++) {
121 fputs($smtpConnection, "RCPT TO:<$cc[$i]>\n");
122 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
123 errorCheck($tmp);
124 }
125 for ($i = 0; $i < count($bcc); $i++) {
126 fputs($smtpConnection, "RCPT TO:<$bcc[$i]>\n");
127 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
128 errorCheck($tmp);
129 }
130
131 /** Lets start sending the actual message */
132 fputs($smtpConnection, "DATA\n");
133 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
134 errorCheck($tmp);
135
136 fputs($smtpConnection, "Subject: $subject\n"); // Subject
137 fputs($smtpConnection, "From: $from\n"); // Subject
138 fputs($smtpConnection, "To: $to_list\n"); // Who it's TO
139
140 if ($cc_list) {
141 fputs($smtpConnection, "Cc: $cc_list\n"); // Who the CCs are
142 }
143 fputs($smtpConnection, "X-Mailer: SquirrelMail (version $version)\n"); // Identify SquirrelMail
144 fputs($smtpConnection, "MIME-Version: 1.0\n");
145 fputs($smtpConnection, "Content-Type: text/plain\n");
146 if ($reply_to != "")
147 fputs($smtpConnection, "Reply-To: $reply_to\n");
148
149 fputs($smtpConnection, "$body\n"); // send the body of the message
150
151 fputs($smtpConnection, ".\n"); // end the DATA part
152 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
153 $num = errorCheck($tmp);
154 if ($num != 250) {
155 echo "<HTML><BODY BGCOLOR=FFFFFF>ERROR<BR>Message not sent!<BR>Reason given: $tmp<BR></BODY></HTML>";
156 }
157
158 fputs($smtpConnection, "QUIT\n"); // log off
159
160 fclose($smtpConnection);
161 }
162
163
164 function errorCheck($line) {
165 // Status: 0 = fatal
166 // 5 = ok
167
168 $err_num = substr($line, 0, strpos($line, " "));
169 switch ($err_num) {
170 case 500: $message = "Syntax error; command not recognized";
171 $status = 0;
172 break;
173 case 501: $message = "Syntax error in parameters or arguments";
174 $status = 0;
175 break;
176 case 502: $message = "Command not implemented";
177 $status = 0;
178 break;
179 case 503: $message = "Bad sequence of commands";
180 $status = 0;
181 break;
182 case 504: $message = "Command parameter not implemented";
183 $status = 0;
184 break;
185
186
187 case 211: $message = "System status, or system help reply";
188 $status = 5;
189 break;
190 case 214: $message = "Help message";
191 $status = 5;
192 break;
193
194
195 case 220: $message = "Service ready";
196 $status = 5;
197 break;
198 case 221: $message = "Service closing transmission channel";
199 $status = 5;
200 break;
201 case 421: $message = "Service not available, closing chanel";
202 $status = 0;
203 break;
204
205
206 case 250: $message = "Requested mail action okay, completed";
207 $status = 5;
208 break;
209 case 251: $message = "User not local; will forward";
210 $status = 5;
211 break;
212 case 450: $message = "Requested mail action not taken: mailbox unavailable";
213 $status = 0;
214 break;
215 case 550: $message = "Requested action not taken: mailbox unavailable";
216 $status = 0;
217 break;
218 case 451: $message = "Requested action aborted: error in processing";
219 $status = 0;
220 break;
221 case 551: $message = "User not local; please try forwarding";
222 $status = 0;
223 break;
224 case 452: $message = "Requested action not taken: insufficient system storage";
225 $status = 0;
226 break;
227 case 552: $message = "Requested mail action aborted: exceeding storage allocation";
228 $status = 0;
229 break;
230 case 553: $message = "Requested action not taken: mailbox name not allowed";
231 $status = 0;
232 break;
233 case 354: $message = "Start mail input; end with .";
234 $status = 5;
235 break;
236 case 554: $message = "Transaction failed";
237 $status = 0;
238 break;
239 default: $message = "Unknown response: $line";
240 $status = 0;
241 $error_num = "001";
242 break;
243 }
244
245 if ($status == 0) {
246 echo "<HTML><BODY BGCOLOR=FFFFFF>";
247 echo "<TT>";
248 echo "<BR><B>ERROR</B><BR><BR>";
249 echo "&nbsp;&nbsp;&nbsp;<B>Error Number: </B>$err_num<BR>";
250 echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<B>Reason: </B>$message<BR>";
251 echo "<B>Server Response: </B>$line<BR>";
252 echo "<BR>MAIL NOT SENT";
253 echo "</TT></BODY></HTML>";
254 exit;
255 }
256 return $err_num;
257 }
258?>