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