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) {
11 if ($useSendmail==true) {
12 sendSendmail($t, $c, $b, $subject, $body);
14 sendSMTP($t, $c, $b, $subject, $body);
19 function write822Header ($fp, $t, $c, $b, $subject) {
20 global $REMOTE_ADDR, $SERVER_NAME;
21 global $data_dir, $username, $domain, $version;
25 $bcc = parseAddrs($b);
26 $from_addr = "$username@$domain";
27 $reply_to = getPref($data_dir, $username, "reply_to");
28 $from = getPref($data_dir, $username, "full_name");
30 $to_list = getLineOfAddrs($to);
31 $cc_list = getLineOfAddrs($cc);
32 $bcc_list = getLineOfAddrs($bcc);
35 $from = "<$from_addr>";
37 $from = $from . " <$from_addr>";
39 $date = date("D, j M Y H:i:s +0000", gmmktime());
41 /* Make a RFC822 Received: line */
42 fputs ($fp, "Received: from $REMOTE_ADDR by $SERVER_NAME with HTTP; ");
43 fputs ($fp, "$date\n");
45 fputs ($fp, "Date: $date\n");
46 fputs ($fp, "Subject: $subject\n"); // Subject
47 fputs ($fp, "From: $from\n"); // Subject
48 fputs ($fp, "To: $to_list\n"); // Who it's TO
51 fputs($fp, "Cc: $cc_list\n"); // Who the CCs are
54 fputs($fp, "Bcc: $bcc_list\n"); // BCCs is removed from header by sendmail
56 fputs($fp, "X-Mailer: SquirrelMail (version $version)\n"); // Identify SquirrelMail
57 fputs($fp, "MIME-Version: 1.0\n");
58 fputs($fp, "Content-Type: text/plain\n");
60 fputs($fp, "Reply-To: $reply_to\n");
63 // Send mail using the sendmail command
64 function sendSendmail($t, $c, $b, $subject, $body) {
65 global $sendmail_path, $username, $domain;
67 // open pipe to sendmail
68 $fp = popen ("$sendmail_path -t -f$username@$domain", "w");
70 write822Header ($fp, $t, $c, $b, $subject);
71 fputs($fp, "\n$body\n"); // send the body of the message
76 function smtpReadData($smtpConnection) {
77 $read = fgets($smtpConnection, 1024);
81 $data[$counter] = $read;
82 $read = fgets($smtpConnection, 1024);
87 function sendSMTP($t, $c, $b, $subject, $body) {
88 global $username, $domain, $version, $smtpServerAddress, $smtpPort;
92 $bcc = parseAddrs($b);
93 $from_addr = "$username@$domain";
95 $smtpConnection = fsockopen($smtpServerAddress, $smtpPort, $errorNumber, $errorString);
96 if (!$smtpConnection) {
97 echo "Error connecting to SMTP Server.<br>";
98 echo "$errorNumber : $errorString<br>";
101 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
104 $to_list = getLineOfAddrs($to);
105 $cc_list = getLineOfAddrs($cc);
107 /** Lets introduce ourselves */
108 fputs($smtpConnection, "HELO $domain\n");
109 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
112 /** Ok, who is sending the message? */
113 fputs($smtpConnection, "MAIL FROM:<$from_addr>\n");
114 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
117 /** send who the recipients are */
118 for ($i = 0; $i < count($to); $i++
) {
119 fputs($smtpConnection, "RCPT TO:<$to[$i]>\n");
120 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
123 for ($i = 0; $i < count($cc); $i++
) {
124 fputs($smtpConnection, "RCPT TO:<$cc[$i]>\n");
125 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
128 for ($i = 0; $i < count($bcc); $i++
) {
129 fputs($smtpConnection, "RCPT TO:<$bcc[$i]>\n");
130 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
134 /** Lets start sending the actual message */
135 fputs($smtpConnection, "DATA\n");
136 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
139 write822Header ($smtpConnection, $t, $c, $b, $subject);
141 fputs($smtpConnection, "$body\n"); // send the body of the message
143 fputs($smtpConnection, ".\n"); // end the DATA part
144 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
145 $num = errorCheck($tmp);
147 echo "<HTML><BODY BGCOLOR=FFFFFF>ERROR<BR>Message not sent!<BR>Reason given: $tmp<BR></BODY></HTML>";
150 fputs($smtpConnection, "QUIT\n"); // log off
152 fclose($smtpConnection);
156 function errorCheck($line) {
160 $err_num = substr($line, 0, strpos($line, " "));
162 case 500: $message = "Syntax error; command not recognized";
165 case 501: $message = "Syntax error in parameters or arguments";
168 case 502: $message = "Command not implemented";
171 case 503: $message = "Bad sequence of commands";
174 case 504: $message = "Command parameter not implemented";
179 case 211: $message = "System status, or system help reply";
182 case 214: $message = "Help message";
187 case 220: $message = "Service ready";
190 case 221: $message = "Service closing transmission channel";
193 case 421: $message = "Service not available, closing chanel";
198 case 250: $message = "Requested mail action okay, completed";
201 case 251: $message = "User not local; will forward";
204 case 450: $message = "Requested mail action not taken: mailbox unavailable";
207 case 550: $message = "Requested action not taken: mailbox unavailable";
210 case 451: $message = "Requested action aborted: error in processing";
213 case 551: $message = "User not local; please try forwarding";
216 case 452: $message = "Requested action not taken: insufficient system storage";
219 case 552: $message = "Requested mail action aborted: exceeding storage allocation";
222 case 553: $message = "Requested action not taken: mailbox name not allowed";
225 case 354: $message = "Start mail input; end with .";
228 case 554: $message = "Transaction failed";
231 default: $message = "Unknown response: $line";
238 echo "<HTML><BODY BGCOLOR=FFFFFF>";
240 echo "<BR><B>ERROR</B><BR><BR>";
241 echo " <B>Error Number: </B>$err_num<BR>";
242 echo " <B>Reason: </B>$message<BR>";
243 echo "<B>Server Response: </B>$line<BR>";
244 echo "<BR>MAIL NOT SENT";
245 echo "</TT></BODY></HTML>";