Published in: PHP
SMTP Mail Sender (PHP) - Kagan GNGR
<?php /** * This is a drop-in replacement for mail() for *nix systems to force them * to use a SMTP server instead of sendmail. (I suppose it could also be * used on windows, but theres not much point). * * To use: set the SMTP_SERVER, and just use the SendMail() function below * (same paramters as mail()). * * Btw, I did not write most of this. I made a couple modifications and * fixes, and wrote SendMail(), but thats it. So there is no warrenty on * this at all :) * * * - Greg MacLellan */ // the SMTP server to use /* * smtp.php * * @(#) $Header: /cvsroot/PHPlibrary/smtp.php,v 1.10 2000/06/03 02:29:41 mlemos Exp $ * */ class smtp_class { var $host_name=""; var $host_port=25; var $localhost=""; var $timeout=0; var $error=""; var $debug=0; var $esmtp=1; var $esmtp_host=""; var $maximum_piped_recipients=100; /* private variables - DO NOT ACCESS */ var $state="Disconnected"; var $connection=0; var $pending_recipients=0; /* Private methods - DO NOT CALL */ Function OutputDebug($message) { } Function GetLine() { for($line="";;) { { $this->error="reached the end of stream while reading from socket"; return(0); } { $this->error="it was not possible to read line from socket"; return(0); } $line.=$data; if($length>=2 ") { if($this->debug) $this->OutputDebug("< $line"); return($line); } } } Function PutLine($line) { if($this->debug) $this->OutputDebug("> $line"); ")) { $this->error="it was not possible to write line to socket"; return(0); } return(1); } Function PutData($data) { { if($this->debug) $this->OutputDebug("> $data"); { $this->error="it was not possible to write data to socket"; return(0); } } return(1); } Function VerifyResultLines($code,$responses="") { while(($line=$this->GetLine($this->connection))) { { { $this->error=$line; return(0); } } else { { { $this->error=$line; return(0); } } else { { $this->error=$line; return(0); } } } return(1); } return(-1); } Function FlushRecipients() { if($this->pending_sender) { if($this->VerifyResultLines("250")<=0) return(0); $this->pending_sender=0; } for(;$this->pending_recipients;$this->pending_recipients--) { return(0); } return(1); } /* Public methods */ Function Connect() { $this->error=$error=""; $this->esmtp_host=""; { switch($error) { case -3: $this->error="-3 socket could not be created"; return(0); case -4: $this->error="-4 dns lookup on hostname \"".$host_name."\" failed"; return(0); case -5: $this->error="-5 connection refused or timed out"; return(0); case -6: $this->error="-6 fdopen() call failed"; return(0); case -7: $this->error="-7 setvbuf() call failed"; return(0); default: $this->error=$error." could not connect to the host \"".$this->host_name."\""; return(0); } } else { $localhost="localhost"; $success=0; if($this->VerifyResultLines("220")>0) { if($this->esmtp) { if($this->PutLine("EHLO $localhost") && $this->VerifyResultLines("250",&$responses)>0) { for($response=1;$response<count($responses);$response++) { } $success=1; } } if(!$success && $this->PutLine("HELO $localhost") && $this->VerifyResultLines("250")>0) $success=1; } if($success) { $this->state="Connected"; return(1); } else { $this->connection=0; $this->state="Disconnected"; return(0); } } } Function MailFrom($sender) { { $this->error="connection is not in the initial state"; return(0); } $this->error=""; if(!$this->PutLine("MAIL FROM:<$sender>")) return(0); && $this->VerifyResultLines("250")<=0) return(0); $this->state="SenderSet"; $this->pending_sender=1; $this->pending_recipients=0; return(1); } Function SetRecipient($recipient) { switch($this->state) { case "SenderSet": case "RecipientSet": break; default: $this->error="connection is not in the recipient setting state"; return(0); } $this->error=""; if(!$this->PutLine("RCPT TO:<$recipient>")) return(0); { $this->pending_recipients++; if($this->pending_recipients>=$this->maximum_piped_recipients) { if(!$this->FlushRecipients()) return(0); } } else { return(0); } $this->state="RecipientSet"; return(1); } Function StartData() { { $this->error="connection is not in the start sending data state"; return(0); } $this->error=""; if(!$this->PutLine("DATA")) return(0); if($this->pending_recipients) { if(!$this->FlushRecipients()) return(0); } if($this->VerifyResultLines("354")<=0) return(0); $this->state="SendingData"; return(1); } Function PrepareData($data,&$output) { for($output="",$position=0;$position<$length;) { $next_position=$length; for($current=$position;$current<$length;$current++) { switch($data[$current]) { case "\n": $next_position=$current+1; break 2; case "\r": $next_position=$current+1; if($data[$next_position]=="\n") $next_position++; break 2; } } if($data[$position]==".") $output.="."; "; $position=$next_position; } } Function SendData($data) { { $this->error="connection is not in the sending data state"; return(0); } $this->error=""; return($this->PutData(&$data)); } Function EndSendingData() { { $this->error="connection is not in the sending data state"; return(0); } $this->error=""; if(!$this->PutLine(" .") || $this->VerifyResultLines("250")<=0) return(0); $this->state="Connected"; return(1); } Function ResetConnection() { switch($this->state) { case "Connected": return(1); case "SendingData": $this->error="can not reset the connection while sending data"; return(0); case "Disconnected": $this->error="can not reset the connection before it is established"; return(0); } $this->error=""; if(!$this->PutLine("RSET") || $this->VerifyResultLines("250")<=0) return(0); $this->state="Connected"; return(1); } Function Disconnect($quit=1) { { $this->error="it was not previously established a SMTP connection"; return(0); } $this->error=""; && $quit && (!$this->PutLine("QUIT") || $this->VerifyResultLines("221")<=0)) return(0); $this->connection=0; $this->state="Disconnected"; return(1); } Function SendMessage($sender,$recipients,$headers,$body) { if(($success=$this->Connect())) { if(($success=$this->MailFrom($sender))) { for($recipient=0;$recipient<count($recipients);$recipient++) { if(!($success=$this->SetRecipient($recipients[$recipient]))) break; } if($success && ($success=$this->StartData())) { for($header_data="",$header=0;$header<count($headers);$header++) $header_data.=$headers[$header]." "; if(($success=$this->SendData($header_data." "))) { $this->PrepareData($body,&$body_data); $success=$this->SendData($body_data); } if($success) $success=$this->EndSendingData(); } } $error=$this->error; $disconnect_success=$this->Disconnect($success); if($success) $success=$disconnect_success; else $this->error=$error; } return($success); } } function SendMail($to, $subject = "", $message = "", $headers = "") { $smtp = new smtp_class; $smtp->host_name = SMTP_SERVER; $smtp->localhost = "localhost"; // If the "to" variable contains the Person's name and email address, // separate them with the matches array ... eg. John Doe <johndoe@hotmail.com> $ToName = $matches[1]; $ToAddress = $matches[2]; } else { $ToAddress = $ToName = $to; } $headers .= "To: $ToName <$ToAddress>\n"; $headers .= "Subject: $subject\n"; // it's the From header, handle it a bit differently // trim out the name/address from "Person <person@domain.com>" $FromName = $matches[1]; $FromAddress = $matches[2]; } else { $FromAddress = $FromName = $to; } $headersarray[] = "From: $FromName <$FromAddress>"; $headersarray[] = $val; } } return true; } else { return false; } } ?>
You need to login to post a comment.
