546d612c4a6832bf672819874a1caa7b45d5de00
3 * Deliver_SMTP.class.php
5 * Copyright (c) 1999-2003 The SquirrelMail Project Team
6 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 * Delivery backend for the Deliver class.
13 require_once(SM_PATH
. 'class/deliver/Deliver.class.php');
15 class Deliver_SMTP
extends Deliver
{
17 function preWriteToStream(&$s) {
19 if ($s{0} == '.') $s = '.' . $s;
20 $s = str_replace("\n.","\n..",$s);
24 function initStream($message, $domain, $length=0, $host='', $port='', $user='', $pass='', $authpop=false) {
25 global $use_smtp_tls,$smtp_auth_mech,$username,$key,$onetimepad;
28 $this->authPop($host, '', $username, $pass);
31 $rfc822_header = $message->rfc822_header
;
32 $from = $rfc822_header->from
[0];
33 $to = $rfc822_header->to
;
34 $cc = $rfc822_header->cc
;
35 $bcc = $rfc822_header->bcc
;
37 if (($use_smtp_tls == true) and (check_php_version(4,3)) and (extension_loaded('openssl'))) {
38 $stream = fsockopen('tls://' . $host, $port, $errorNumber, $errorString);
40 $stream = fsockopen($host, $port, $errorNumber, $errorString);
44 $this->dlv_msg
= $errorString;
45 $this->dlv_ret_nr
= $errorNumber;
48 $tmp = fgets($stream, 1024);
49 if ($this->errorCheck($tmp, $stream)) {
53 /* If $_SERVER['HTTP_HOST'] is set, use that in our HELO to the SMTP
54 server. This should fix the DNS issues some people have had */
55 if (sqgetGlobalVar('HTTP_HOST', $HTTP_HOST, SQ_SERVER
)) { // HTTP_HOST is set
56 $helohost = $HTTP_HOST;
57 } else { // For some reason, HTTP_HOST is not set - revert to old behavior
61 /* Lets introduce ourselves */
62 if (( $smtp_auth_mech == 'cram-md5') or ( $smtp_auth_mech == 'digest-md5' )) {
63 // Doing some form of non-plain auth
64 fputs($stream, "EHLO $helohost\r\n");
65 $tmp = fgets($stream,1024);
66 if ($this->errorCheck($tmp,$stream)) {
69 if ($smtp_auth_mech == 'cram-md5') {
70 fputs($stream, "AUTH CRAM-MD5\r\n");
71 } elseif ($smtp_auth_mech == 'digest-md5') {
72 fputs($stream, "AUTH DIGEST-MD5\r\n");
74 $tmp = fgets($stream,1024);
76 if ($this->errorCheck($tmp,$stream)) {
80 // At this point, $tmp should hold "334 <challenge string>"
81 $chall = substr($tmp,4);
82 // Depending on mechanism, generate response string
83 if ($smtp_auth_mech == 'cram-md5') {
84 $response = cram_md5_response($username,$pass,$chall);
85 } elseif ($smtp_auth_mech == 'digest-md5') {
86 $response = digest_md5_response($username,$pass,$chall,'smtp',$host);
88 fputs($stream, $response);
90 // Let's see what the server had to say about that
91 $tmp = fgets($stream,1024);
92 if ($this->errorCheck($tmp,$stream)) {
96 // CRAM-MD5 is done at this point. If DIGEST-MD5, there's a bit more to go
97 if ($smtp_auth_mech == 'digest-md5')
99 // $tmp contains rspauth, but I don't store that yet. (No need yet)
100 fputs($stream,"\r\n");
101 $tmp = fgets($stream,1024);
103 if ($this->errorCheck($tmp,$stream)) {
107 // CRAM-MD5 and DIGEST-MD5 code ends here
108 } elseif ($smtp_auth_mech == 'none') {
109 // No auth at all, just send helo and then send the mail
110 fputs($stream, "HELO $helohost\r\n");
111 $tmp = fgets($stream, 1024);
112 if ($this->errorCheck($tmp, $stream)) {
115 } elseif ($smtp_auth_mech == 'login') {
117 fputs($stream, "EHLO $helohost\r\n");
118 $tmp = fgets($stream, 1024);
119 if ($this->errorCheck($tmp, $stream)) {
122 fputs($stream, "AUTH LOGIN\r\n");
123 $tmp = fgets($stream, 1024);
125 if ($this->errorCheck($tmp, $stream)) {
128 fputs($stream, base64_encode ($username) . "\r\n");
129 $tmp = fgets($stream, 1024);
130 if ($this->errorCheck($tmp, $stream)) {
134 fputs($stream, base64_encode($pass) . "\r\n");
135 $tmp = fgets($stream, 1024);
136 if ($this->errorCheck($tmp, $stream)) {
140 /* Right here, they've reached an unsupported auth mechanism.
141 This is the ugliest hack I've ever done, but it'll do till I can fix
142 things up better tomorrow. So tired... */
143 if ($this->errorCheck("535 Unable to use this auth type",$stream)) {
148 /* Ok, who is sending the message? */
149 fputs($stream, 'MAIL FROM: <'.$from->mailbox
.'@'.$from->host
.">\r\n");
150 $tmp = fgets($stream, 1024);
151 if ($this->errorCheck($tmp, $stream)) {
155 /* send who the recipients are */
156 for ($i = 0, $cnt = count($to); $i < $cnt; $i++
) {
157 if (!$to[$i]->host
) $to[$i]->host
= $domain;
158 if ($to[$i]->mailbox
) {
159 fputs($stream, 'RCPT TO: <'.$to[$i]->mailbox
.'@'.$to[$i]->host
.">\r\n");
160 $tmp = fgets($stream, 1024);
161 if ($this->errorCheck($tmp, $stream)) {
167 for ($i = 0, $cnt = count($cc); $i < $cnt; $i++
) {
168 if (!$cc[$i]->host
) $cc[$i]->host
= $domain;
169 if ($cc[$i]->mailbox
) {
170 fputs($stream, 'RCPT TO: <'.$cc[$i]->mailbox
.'@'.$cc[$i]->host
.">\r\n");
171 $tmp = fgets($stream, 1024);
172 if ($this->errorCheck($tmp, $stream)) {
177 for ($i = 0, $cnt = count($bcc); $i < $cnt; $i++
) {
178 if (!$bcc[$i]->host
) $bcc[$i]->host
= $domain;
179 if ($bcc[$i]->mailbox
) {
180 fputs($stream, 'RCPT TO: <'.$bcc[$i]->mailbox
.'@'.$bcc[$i]->host
.">\r\n");
181 $tmp = fgets($stream, 1024);
182 if ($this->errorCheck($tmp, $stream)) {
187 /* Lets start sending the actual message */
188 fputs($stream, "DATA\r\n");
189 $tmp = fgets($stream, 1024);
190 if ($this->errorCheck($tmp, $stream)) {
196 function finalizeStream($stream) {
197 fputs($stream, ".\r\n"); /* end the DATA part */
198 $tmp = fgets($stream, 1024);
199 $this->errorCheck($tmp, $stream);
200 if ($this->dlv_ret_nr
!= 250) {
203 fputs($stream, "QUIT\r\n"); /* log off */
208 /* check if an SMTP reply is an error and set an error message) */
209 function errorCheck($line, $smtpConnection) {
211 $err_num = substr($line, 0, 3);
212 $this->dlv_ret_nr
= $err_num;
213 $server_msg = substr($line, 4);
215 while(substr($line, 0, 4) == ($err_num.'-')) {
216 $line = fgets($smtpConnection, 1024);
217 $server_msg .= substr($line, 4);
220 if ( ((int) $err_num{0}) < 4)
226 case '421': $message = _("Service not available, closing channel");
228 case '432': $message = _("A password transition is needed");
230 case '450': $message = _("Requested mail action not taken: mailbox unavailable");
232 case '451': $message = _("Requested action aborted: error in processing");
234 case '452': $message = _("Requested action not taken: insufficient system storage");
236 case '454': $message = _("Temporary authentication failure");
238 case '500': $message = _("Syntax error; command not recognized");
240 case '501': $message = _("Syntax error in parameters or arguments");
242 case '502': $message = _("Command not implemented");
244 case '503': $message = _("Bad sequence of commands");
246 case '504': $message = _("Command parameter not implemented");
248 case '530': $message = _("Authentication required");
250 case '534': $message = _("Authentication mechanism is too weak");
252 case '535': $message = _("Authentication failed");
254 case '538': $message = _("Encryption required for requested authentication mechanism");
256 case '550': $message = _("Requested action not taken: mailbox unavailable");
258 case '551': $message = _("User not local; please try forwarding");
260 case '552': $message = _("Requested mail action aborted: exceeding storage allocation");
262 case '553': $message = _("Requested action not taken: mailbox name not allowed");
264 case '554': $message = _("Transaction failed");
266 default: $message = _("Unknown response");
270 $this->dlv_msg
= $message;
271 $this->dlv_server_msg
= nl2br(htmlspecialchars($server_msg));
276 function authPop($pop_server='', $pop_port='', $user, $pass) {
281 $pop_server = 'localhost';
283 $popConnection = fsockopen($pop_server, $pop_port, $err_no, $err_str);
284 if (!$popConnection) {
285 error_log("Error connecting to POP Server ($pop_server:$pop_port)"
286 . " $err_no : $err_str");
288 $tmp = fgets($popConnection, 1024); /* banner */
289 if (!eregi("^\+OK", $tmp, $regs)) {
292 fputs($popConnection, "USER $user\r\n");
293 $tmp = fgets($popConnection, 1024);
294 if (!eregi("^\+OK", $tmp, $regs)) {
297 fputs($popConnection, 'PASS ' . $pass . "\r\n");
298 $tmp = fgets($popConnection, 1024);
299 if (!eregi("^\+OK", $tmp, $regs)) {
302 fputs($popConnection, "QUIT\r\n"); /* log off */
303 fclose($popConnection);