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