Some minro modification to the use of config.php
[squirrelmail.git] / functions / smtp.php
CommitLineData
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
36fa79c8 8 function sendMessage($t, $c, $b, $subject, $body, $version) {
9 global $useSendmail;
465db5d7 10
11 if ($useSendmail==true) {
36fa79c8 12 sendSendmail($t, $c, $b, $subject, $body, $version);
465db5d7 13 } else {
36fa79c8 14 sendSMTP($t, $c, $b, $subject, $body, $version);
465db5d7 15 }
16
17 }
18
19 // Send mail using the sendmail command
36fa79c8 20 function sendSendmail($t, $c, $b, $subject, $body, $version) {
21 global $username, $domain, $data_dir, $sendmail_path;
465db5d7 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
36fa79c8 75 function sendSMTP($t, $c, $b, $subject, $body, $version) {
76 global $username, $domain;
77
7831268e 78 include("../config/config.php");
79
40ee9452 80 $to = parseAddrs($t);
81 $cc = parseAddrs($c);
82 $bcc = parseAddrs($b);
d3cdb279 83 $from_addr = "$username@$domain";
d0747e26 84 $reply_to = getPref($data_dir, $username, "reply_to");
85 $from = getPref($data_dir, $username, "full_name");
b4da6659 86
d3cdb279 87 if ($from == "")
33daaa7d 88 $from = "<$from_addr>";
d3cdb279 89 else
33daaa7d 90 $from = $from . " <$from_addr>";
d3cdb279 91
c175e2e4 92 $smtpConnection = fsockopen($smtpServerAddress, $smtpPort, $errorNumber, $errorString);
b8ea4ed6 93 if (!$smtpConnection) {
94 echo "Error connecting to SMTP Server.<br>";
95 echo "$errorNumber : $errorString<br>";
96 exit;
97 }
d3cdb279 98 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
3021b626 99 errorCheck($tmp);
b8ea4ed6 100
40ee9452 101 $to_list = getLineOfAddrs($to);
102 $cc_list = getLineOfAddrs($cc);
103
104 /** Lets introduce ourselves */
105 fputs($smtpConnection, "HELO $domain\n");
d3cdb279 106 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
3021b626 107 errorCheck($tmp);
7831268e 108
40ee9452 109 /** Ok, who is sending the message? */
33daaa7d 110 fputs($smtpConnection, "MAIL FROM:<$from_addr>\n");
d3cdb279 111 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
3021b626 112 errorCheck($tmp);
b8ea4ed6 113
40ee9452 114 /** send who the recipients are */
115 for ($i = 0; $i < count($to); $i++) {
116 fputs($smtpConnection, "RCPT TO:<$to[$i]>\n");
d3cdb279 117 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
3021b626 118 errorCheck($tmp);
40ee9452 119 }
120 for ($i = 0; $i < count($cc); $i++) {
121 fputs($smtpConnection, "RCPT TO:<$cc[$i]>\n");
d3cdb279 122 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
3021b626 123 errorCheck($tmp);
40ee9452 124 }
125 for ($i = 0; $i < count($bcc); $i++) {
126 fputs($smtpConnection, "RCPT TO:<$bcc[$i]>\n");
d3cdb279 127 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
3021b626 128 errorCheck($tmp);
40ee9452 129 }
b8ea4ed6 130
40ee9452 131 /** Lets start sending the actual message */
b8ea4ed6 132 fputs($smtpConnection, "DATA\n");
d3cdb279 133 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
3021b626 134 errorCheck($tmp);
7831268e 135
40ee9452 136 fputs($smtpConnection, "Subject: $subject\n"); // Subject
d3cdb279 137 fputs($smtpConnection, "From: $from\n"); // Subject
cace329e 138 fputs($smtpConnection, "To: $to_list\n"); // Who it's TO
7831268e 139
40ee9452 140 if ($cc_list) {
cace329e 141 fputs($smtpConnection, "Cc: $cc_list\n"); // Who the CCs are
40ee9452 142 }
143 fputs($smtpConnection, "X-Mailer: SquirrelMail (version $version)\n"); // Identify SquirrelMail
7ce342dc 144 fputs($smtpConnection, "MIME-Version: 1.0\n");
4809f489 145 fputs($smtpConnection, "Content-Type: text/plain\n");
b4da6659 146 if ($reply_to != "")
147 fputs($smtpConnection, "Reply-To: $reply_to\n");
40ee9452 148
149 fputs($smtpConnection, "$body\n"); // send the body of the message
7831268e 150
40ee9452 151 fputs($smtpConnection, ".\n"); // end the DATA part
d3cdb279 152 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
3021b626 153 $num = errorCheck($tmp);
154 if ($num != 250) {
155 echo "<HTML><BODY BGCOLOR=FFFFFF>ERROR<BR>Message not sent!<BR>Reason given: $tmp<BR></BODY></HTML>";
156 }
d3cdb279 157
40ee9452 158 fputs($smtpConnection, "QUIT\n"); // log off
78509c54 159
160 fclose($smtpConnection);
b8ea4ed6 161 }
3021b626 162
163
164 function errorCheck($line) {
165 // Status: 0 = fatal
166 // 5 = ok
167
168 $err_num = substr($line, 0, strpos($line, " "));
169 switch ($err_num) {
170 case 500: $message = "Syntax error; command not recognized";
171 $status = 0;
172 break;
173 case 501: $message = "Syntax error in parameters or arguments";
174 $status = 0;
175 break;
176 case 502: $message = "Command not implemented";
177 $status = 0;
178 break;
179 case 503: $message = "Bad sequence of commands";
180 $status = 0;
181 break;
182 case 504: $message = "Command parameter not implemented";
183 $status = 0;
184 break;
185
186
187 case 211: $message = "System status, or system help reply";
188 $status = 5;
189 break;
190 case 214: $message = "Help message";
191 $status = 5;
192 break;
193
194
195 case 220: $message = "Service ready";
196 $status = 5;
197 break;
198 case 221: $message = "Service closing transmission channel";
199 $status = 5;
200 break;
201 case 421: $message = "Service not available, closing chanel";
202 $status = 0;
203 break;
204
205
206 case 250: $message = "Requested mail action okay, completed";
207 $status = 5;
208 break;
209 case 251: $message = "User not local; will forward";
210 $status = 5;
211 break;
212 case 450: $message = "Requested mail action not taken: mailbox unavailable";
213 $status = 0;
214 break;
215 case 550: $message = "Requested action not taken: mailbox unavailable";
216 $status = 0;
217 break;
218 case 451: $message = "Requested action aborted: error in processing";
219 $status = 0;
220 break;
221 case 551: $message = "User not local; please try forwarding";
222 $status = 0;
223 break;
224 case 452: $message = "Requested action not taken: insufficient system storage";
225 $status = 0;
226 break;
227 case 552: $message = "Requested mail action aborted: exceeding storage allocation";
228 $status = 0;
229 break;
230 case 553: $message = "Requested action not taken: mailbox name not allowed";
231 $status = 0;
232 break;
233 case 354: $message = "Start mail input; end with .";
234 $status = 5;
235 break;
236 case 554: $message = "Transaction failed";
237 $status = 0;
238 break;
239 default: $message = "Unknown response: $line";
240 $status = 0;
241 $error_num = "001";
242 break;
243 }
244
245 if ($status == 0) {
246 echo "<HTML><BODY BGCOLOR=FFFFFF>";
247 echo "<TT>";
248 echo "<BR><B>ERROR</B><BR><BR>";
249 echo "&nbsp;&nbsp;&nbsp;<B>Error Number: </B>$err_num<BR>";
250 echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<B>Reason: </B>$message<BR>";
251 echo "<B>Server Response: </B>$line<BR>";
252 echo "<BR>MAIL NOT SENT";
253 echo "</TT></BODY></HTML>";
254 exit;
255 }
256 return $err_num;
257 }
465db5d7 258?>