b8ea4ed6 |
1 | <? |
2 | /** smtp.php |
3 | ** |
4 | ** This contains all the functions needed to send messages through |
5 | ** an smtp server. |
6 | **/ |
7 | |
465db5d7 |
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 | |
b8ea4ed6 |
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 | |
465db5d7 |
75 | function sendSMTP($username, $domain, $t, $c, $b, $subject, $body, $version) { |
7831268e |
76 | include("../config/config.php"); |
77 | |
40ee9452 |
78 | $to = parseAddrs($t); |
79 | $cc = parseAddrs($c); |
80 | $bcc = parseAddrs($b); |
d3cdb279 |
81 | $from_addr = "$username@$domain"; |
d0747e26 |
82 | $reply_to = getPref($data_dir, $username, "reply_to"); |
83 | $from = getPref($data_dir, $username, "full_name"); |
b4da6659 |
84 | |
d3cdb279 |
85 | if ($from == "") |
33daaa7d |
86 | $from = "<$from_addr>"; |
d3cdb279 |
87 | else |
33daaa7d |
88 | $from = $from . " <$from_addr>"; |
d3cdb279 |
89 | |
c175e2e4 |
90 | $smtpConnection = fsockopen($smtpServerAddress, $smtpPort, $errorNumber, $errorString); |
b8ea4ed6 |
91 | if (!$smtpConnection) { |
92 | echo "Error connecting to SMTP Server.<br>"; |
93 | echo "$errorNumber : $errorString<br>"; |
94 | exit; |
95 | } |
d3cdb279 |
96 | $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024))); |
3021b626 |
97 | errorCheck($tmp); |
b8ea4ed6 |
98 | |
40ee9452 |
99 | $to_list = getLineOfAddrs($to); |
100 | $cc_list = getLineOfAddrs($cc); |
101 | |
102 | /** Lets introduce ourselves */ |
103 | fputs($smtpConnection, "HELO $domain\n"); |
d3cdb279 |
104 | $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024))); |
3021b626 |
105 | errorCheck($tmp); |
7831268e |
106 | |
40ee9452 |
107 | /** Ok, who is sending the message? */ |
33daaa7d |
108 | fputs($smtpConnection, "MAIL FROM:<$from_addr>\n"); |
d3cdb279 |
109 | $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024))); |
3021b626 |
110 | errorCheck($tmp); |
b8ea4ed6 |
111 | |
40ee9452 |
112 | /** send who the recipients are */ |
113 | for ($i = 0; $i < count($to); $i++) { |
114 | fputs($smtpConnection, "RCPT TO:<$to[$i]>\n"); |
d3cdb279 |
115 | $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024))); |
3021b626 |
116 | errorCheck($tmp); |
40ee9452 |
117 | } |
118 | for ($i = 0; $i < count($cc); $i++) { |
119 | fputs($smtpConnection, "RCPT TO:<$cc[$i]>\n"); |
d3cdb279 |
120 | $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024))); |
3021b626 |
121 | errorCheck($tmp); |
40ee9452 |
122 | } |
123 | for ($i = 0; $i < count($bcc); $i++) { |
124 | fputs($smtpConnection, "RCPT TO:<$bcc[$i]>\n"); |
d3cdb279 |
125 | $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024))); |
3021b626 |
126 | errorCheck($tmp); |
40ee9452 |
127 | } |
b8ea4ed6 |
128 | |
40ee9452 |
129 | /** Lets start sending the actual message */ |
b8ea4ed6 |
130 | fputs($smtpConnection, "DATA\n"); |
d3cdb279 |
131 | $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024))); |
3021b626 |
132 | errorCheck($tmp); |
7831268e |
133 | |
40ee9452 |
134 | fputs($smtpConnection, "Subject: $subject\n"); // Subject |
d3cdb279 |
135 | fputs($smtpConnection, "From: $from\n"); // Subject |
cace329e |
136 | fputs($smtpConnection, "To: $to_list\n"); // Who it's TO |
7831268e |
137 | |
40ee9452 |
138 | if ($cc_list) { |
cace329e |
139 | fputs($smtpConnection, "Cc: $cc_list\n"); // Who the CCs are |
40ee9452 |
140 | } |
141 | fputs($smtpConnection, "X-Mailer: SquirrelMail (version $version)\n"); // Identify SquirrelMail |
7ce342dc |
142 | fputs($smtpConnection, "MIME-Version: 1.0\n"); |
4809f489 |
143 | fputs($smtpConnection, "Content-Type: text/plain\n"); |
b4da6659 |
144 | if ($reply_to != "") |
145 | fputs($smtpConnection, "Reply-To: $reply_to\n"); |
40ee9452 |
146 | |
147 | fputs($smtpConnection, "$body\n"); // send the body of the message |
7831268e |
148 | |
40ee9452 |
149 | fputs($smtpConnection, ".\n"); // end the DATA part |
d3cdb279 |
150 | $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024))); |
3021b626 |
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 | } |
d3cdb279 |
155 | |
40ee9452 |
156 | fputs($smtpConnection, "QUIT\n"); // log off |
78509c54 |
157 | |
158 | fclose($smtpConnection); |
b8ea4ed6 |
159 | } |
3021b626 |
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 " <B>Error Number: </B>$err_num<BR>"; |
248 | echo " <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 | } |
465db5d7 |
256 | ?> |