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