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