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