fbe4f707e8d43eacdad80c675f1cec73ef2f395e
4 ** This contains all the functions needed to send messages through
5 ** an smtp server or sendmail.
8 function sendMessage($t, $c, $b, $subject, $body, $version) {
11 if ($useSendmail==true) {
12 sendSendmail($t, $c, $b, $subject, $body, $version);
14 sendSMTP($t, $c, $b, $subject, $body, $version);
19 // Send mail using the sendmail command
20 function sendSendmail($t, $c, $b, $subject, $body, $version) {
21 global $username, $domain, $data_dir, $sendmail_path;
23 // This is pretty much equal to the code in sendSMTP
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");
31 $to_list = getLineOfAddrs($to);
32 $cc_list = getLineOfAddrs($cc);
33 $bcc_list = getLineOfAddrs($bcc);
36 $from = "<$from_addr>";
38 $from = $from . " <$from_addr>";
40 // open pipe to sendmail
41 $fp = popen ("$sendmail_path -t -f$from_addr", "w");
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
48 fputs($fp, "Cc: $cc_list\n"); // Who the CCs are
51 fputs($fp, "Bcc: $bcc_list\n"); // BCCs is removed from header by sendmail
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");
57 fputs($fp, "Reply-To: $reply_to\n");
59 fputs($fp, "\n$body\n"); // send the body of the message
64 function smtpReadData($smtpConnection) {
65 $read = fgets($smtpConnection, 1024);
69 $data[$counter] = $read;
70 $read = fgets($smtpConnection, 1024);
75 function sendSMTP($t, $c, $b, $subject, $body, $version) {
76 global $username, $domain;
78 include("../config/config.php");
82 $bcc = parseAddrs($b);
83 $from_addr = "$username@$domain";
84 $reply_to = getPref($data_dir, $username, "reply_to");
85 $from = getPref($data_dir, $username, "full_name");
88 $from = "<$from_addr>";
90 $from = $from . " <$from_addr>";
92 $smtpConnection = fsockopen($smtpServerAddress, $smtpPort, $errorNumber, $errorString);
93 if (!$smtpConnection) {
94 echo "Error connecting to SMTP Server.<br>";
95 echo "$errorNumber : $errorString<br>";
98 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
101 $to_list = getLineOfAddrs($to);
102 $cc_list = getLineOfAddrs($cc);
104 /** Lets introduce ourselves */
105 fputs($smtpConnection, "HELO $domain\n");
106 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
109 /** Ok, who is sending the message? */
110 fputs($smtpConnection, "MAIL FROM:<$from_addr>\n");
111 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
114 /** send who the recipients are */
115 for ($i = 0; $i < count($to); $i++
) {
116 fputs($smtpConnection, "RCPT TO:<$to[$i]>\n");
117 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
120 for ($i = 0; $i < count($cc); $i++
) {
121 fputs($smtpConnection, "RCPT TO:<$cc[$i]>\n");
122 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
125 for ($i = 0; $i < count($bcc); $i++
) {
126 fputs($smtpConnection, "RCPT TO:<$bcc[$i]>\n");
127 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
131 /** Lets start sending the actual message */
132 fputs($smtpConnection, "DATA\n");
133 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
136 fputs($smtpConnection, "Subject: $subject\n"); // Subject
137 fputs($smtpConnection, "From: $from\n"); // Subject
138 fputs($smtpConnection, "To: $to_list\n"); // Who it's TO
141 fputs($smtpConnection, "Cc: $cc_list\n"); // Who the CCs are
143 fputs($smtpConnection, "X-Mailer: SquirrelMail (version $version)\n"); // Identify SquirrelMail
144 fputs($smtpConnection, "MIME-Version: 1.0\n");
145 fputs($smtpConnection, "Content-Type: text/plain\n");
147 fputs($smtpConnection, "Reply-To: $reply_to\n");
149 fputs($smtpConnection, "$body\n"); // send the body of the message
151 fputs($smtpConnection, ".\n"); // end the DATA part
152 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
153 $num = errorCheck($tmp);
155 echo "<HTML><BODY BGCOLOR=FFFFFF>ERROR<BR>Message not sent!<BR>Reason given: $tmp<BR></BODY></HTML>";
158 fputs($smtpConnection, "QUIT\n"); // log off
160 fclose($smtpConnection);
164 function errorCheck($line) {
168 $err_num = substr($line, 0, strpos($line, " "));
170 case 500: $message = "Syntax error; command not recognized";
173 case 501: $message = "Syntax error in parameters or arguments";
176 case 502: $message = "Command not implemented";
179 case 503: $message = "Bad sequence of commands";
182 case 504: $message = "Command parameter not implemented";
187 case 211: $message = "System status, or system help reply";
190 case 214: $message = "Help message";
195 case 220: $message = "Service ready";
198 case 221: $message = "Service closing transmission channel";
201 case 421: $message = "Service not available, closing chanel";
206 case 250: $message = "Requested mail action okay, completed";
209 case 251: $message = "User not local; will forward";
212 case 450: $message = "Requested mail action not taken: mailbox unavailable";
215 case 550: $message = "Requested action not taken: mailbox unavailable";
218 case 451: $message = "Requested action aborted: error in processing";
221 case 551: $message = "User not local; please try forwarding";
224 case 452: $message = "Requested action not taken: insufficient system storage";
227 case 552: $message = "Requested mail action aborted: exceeding storage allocation";
230 case 553: $message = "Requested action not taken: mailbox name not allowed";
233 case 354: $message = "Start mail input; end with .";
236 case 554: $message = "Transaction failed";
239 default: $message = "Unknown response: $line";
246 echo "<HTML><BODY BGCOLOR=FFFFFF>";
248 echo "<BR><B>ERROR</B><BR><BR>";
249 echo " <B>Error Number: </B>$err_num<BR>";
250 echo " <B>Reason: </B>$message<BR>";
251 echo "<B>Server Response: </B>$line<BR>";
252 echo "<BR>MAIL NOT SENT";
253 echo "</TT></BODY></HTML>";