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