Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- *+---------------------------------+
- *| Mailer Class |
- *+---------------------------------+
- **/
- class Mailer
- {
- var $smtpServer = "";
- var $smtpPort = 25;
- var $lstAttachs = array();
- var $lstRecipients = array();
- var $lstHeaders = array();
- var $authUser = "";
- var $authPass = "";
- var $replyName = "";
- var $replyMail = "";
- var $fromName = "";
- var $fromMail = "";
- var $isMimeOn = 1;
- var $isDebug = 0;
- var $isHtml = 1;
- var $msgSubject = "";
- var $msgBody = "";
- var $smtpSocket = 0;
- function Mailer($smtpServer = false, $smtpPort = 25)
- {
- define("EOL", "\r\n");
- define("MSG_END", "\r\n.\r\n");
- define("EOH", "\r\n\r\n");
- define("SEP", "\r\n--#BOUNDARY#--");
- define("EOM", "QUIT\r\n\0");
- $this->smtpServer = (($smtpServer !== false) ? $smtpServer : $_SERVER["LOCAL_ADDR"]);
- $this->smtpPort = $smtpPort;
- }
- function setSubject($msgSubject)
- {
- $this->msgSubject = $msgSubject;
- }
- function setBody($msgBody)
- {
- $this->msgBody = $msgBody;
- }
- function setFrom($fromName, $fromMail)
- {
- $this->fromName = $fromName;
- $this->fromMail = $fromMail;
- }
- function setReplyTo($reply2Name, $reply2Mail)
- {
- $this->replyName = $reply2Name;
- $this->replyMail = $reply2Mail;
- }
- function addAttachment($filePath, $fileName)
- {
- $filePath = str_replace("\\", "/", $filePath);
- if(!isset($fileName))
- {
- $fileName = substr(strrchr($filePath, '/'), 1);
- }
- $fileExtension = substr(strrchr($fileName, '.'), 1);
- $mimeType = "application/octet-stream";
- if(isset($MimeTypes[$fileExtension]))
- {
- $mimeType = $MimeTypes[$fileExtension];
- }
- return $this->attachFile($filePath, $fileName, $mimeType);
- }
- function attachFile($filePath, $fileName, $mimeType = "application/octet-stream")
- {
- if(!filesize($filePath))
- {
- return false;
- }
- $this->lstAttachs[$fileName] = Array(
- "Path" => $filePath,
- "Mime" => $mimeType
- );
- return true;
- }
- function addRecipient($recName, $recMail, $recType)
- {
- if(!isset($recType))
- {
- $recType = "to";
- }
- else
- {
- $recType = strtolower($recType);
- }
- $recType = strtolower($recType);
- if(!is_array(isset($this->lstRecipients[$recType])))
- {
- $this->lstRecipients[$recType] = array();
- }
- $this->lstRecipients[$recType][$recMail] = $recName;
- }
- function addHeader($hdrName, $hdrValue)
- {
- $this->lstHeaders[$hdrName] = $hdrValue;
- }
- function setImportance($importance)
- {
- $this->addHeader("X-Importance", $importance);
- }
- function setSensitivity($sensitivity)
- {
- $this->addHeader("X-Sensitivity", $sensitivity, "X");
- }
- function setPriority($priority)
- {
- $this->addHeader("X-Priority", $priority);
- }
- function setAuth($authUser, $authPass)
- {
- $this->authUser = $authUser;
- $this->authPass = $authPass;
- }
- function _debugLine($line, $sent = 1)
- {
- $line = trim($line);
- if(!$this->isDebug)
- {
- return;
- }
- echo nl2br(htmlentities($line)."<br />");
- }
- function _sendLines($lstLines, $raw = 0)
- {
- if(!is_array($lstLines))
- {
- $lstLines = str_replace("\r", "", $lstLines);
- $lstLines = explode("\n", $lstLines);
- if(!count($lstLines))
- {
- $lstLines = array($lstLines);
- }
- }
- foreach($lstLines as $line)
- {
- $line = trim($line);
- $this->_debugLine($line);
- if(!fputs($this->smtpSocket, $line."\r\n"))
- {
- return false;
- }
- }
- return true;
- }
- function _getAnswer()
- {
- $line = "";
- while(!feof($this->smtpSocket))
- {
- $ch = fgetc($this->smtpSocket);
- if(!strlen($ch))
- {
- return false;
- }
- if($ch == "\n")
- {
- $this->_debugLine($line, 0);
- if($line[3] == " ")
- {
- return (int)substr($line, 0, 3);
- }
- $line = "";
- continue;
- }
- if($ch != "\r")
- {
- $line .= $ch;
- }
- }
- return false;
- }
- function _authLogin()
- {
- $buf = "AUTH LOGIN";
- $this->_sendLines($buf);
- if($this->_getAnswer() != 334)
- {
- fclose($this->smtpSocket);
- return false;
- }
- $buf=sprintf("%s", $this->authUser);
- $this->_sendLines($buf);
- if($this->_getAnswer() != 334)
- {
- fclose($this->smtpSocket);
- return false;
- }
- $buf=sprintf("%s", $this->authPass);
- $this->_sendLines($buf);
- if($this->_getAnswer() != 235)
- {
- fclose($this->smtpSocket);
- return false;
- }
- return true;
- }
- function connect($timeout = 5)
- {
- $errno = "";
- $errstr = "";
- $this->smtpSocket = fsockopen($this->smtpServer, $this->smtpPort, $errno, $errstr, $timeout);
- if(!$this->smtpSocket)
- {
- $this->_debugString($errno.":".$errstr."\r\n");
- return false;
- }
- if($this->_getAnswer() != 220)
- {
- fclose($this->smtpSocket);
- return false;
- }
- if(($this->authUser != "") && ($this->authPass != ""))
- {
- $buf = sprintf("EHLO %s", "localhost");
- }
- else
- {
- $buf = sprintf("HELO %s", "localhost");
- }
- $this->_sendLines($buf);
- $hiReply = $this->_getAnswer();
- if($hiReply == 250)
- {
- return $this->_authLogin();
- }
- $buf = sprintf("HELO %s","localhost");
- if($this->_getAnswer() != 250)
- {
- fclose($this->smtpSocket);
- return false;
- }
- return true;
- }
- function disconnect()
- {
- $this->_sendLines("QUIT");
- $quitReply = $this->_getAnswer();
- fclose($this->smtpSocket);
- if($quitReply == 221)
- {
- return true;
- }
- return true;
- }
- function _sendRecipients()
- {
- $result = 0;
- $mails = array();
- $mailsErr = array();
- while(list($type, $list) = each($this->lstRecipients))
- {
- while(list($mail, $name) = each($list))
- {
- if(in_array($mail, $mails))
- {
- continue;
- }
- $buf =sprintf("RCPT TO:<%s>", $mail);
- $this->_sendLines($buf);
- $rez =$this->_getAnswer();
- array_push($mails, $mail);
- if($rez == 250)
- {
- continue;
- }
- array_push($mailsErr, $mail);
- unset($this->lstRecipients[$type][$mail]);
- }
- }
- return ((count($mails) - count($mailsErr)) > 0);
- }
- function _sendHeaders()
- {
- reset($this->lstHeaders);
- while(list($name, $value) = each($this->lstHeaders))
- {
- $buf = "$name: $value";
- $this->_sendLines($buf);
- }
- reset($this->lstRecipients);
- while(list($type, $list) = each($this->lstRecipients))
- {
- $mails = array();
- while(list($mail, $name) = each($list))
- {
- array_push($mails, "$name <$mail>");
- }
- $type[0] = strtoupper($type[0]);
- if(isset($this->lstHeaders[$type]))
- {
- continue;
- }
- $buf = "$type: ".implode(",", $mails)."";
- $this->_sendLines($buf);
- }
- $buf = sprintf("From: %s <%s>", $this->fromName, $this->fromMail);
- $this->_sendLines($buf);
- if(strlen($this->replyMail))
- {
- $buf = sprintf("Reply-to: %s <%s>", $this->replyName, $this->replyMail);
- $this->_sendLines($buf);
- }
- $buf = sprintf("Subject: %s", $this->msgSubject);
- $this->_sendLines($buf);
- return true;
- }
- function _sendMessage()
- {
- if($this->isMimeOn)
- {
- $buf = "MIME-Version: 1.0\r\n"."Content-type: multipart/mixed; boundary=\"#BOUNDARY#\"\r\n\r\n";
- $this->_sendLines($buf);
- $buf = "\r\n--#BOUNDARY#\r\n"."Content-Type: text/".($this->isHtml ? "html" : "plain")."; charset=us-ascii\r\n";
- $this->_sendLines($buf);
- }
- else
- {
- $buf = "\r\n";
- $this->_sendLines($buf);
- }
- $this->_sendLines($this->msgBody, 1);
- return true;
- }
- function _sendAttachments()
- {
- if(!$this->isMimeOn)
- {
- return true;
- }
- if(!count($this->lstAttachs))
- {
- return true;
- }
- while(list($name, $file) = each($this->lstAttachs))
- {
- $fpath = $file['Path'];
- $mime = $file['Mime'];
- $fname = $this->names[$i];
- $newfile = fopen($fpath, "rb");
- $content = fread($newfile, filesize($fpath));
- fclose($newfile);
- $content = base64_encode($content);
- $buf = sprintf("\r\n\r\n--#BOUNDARY#\r\n"."Content-Type: ".$mime.";&;nbsp;name=%s\r\n"."Content-Length: ".filesize($file)."\r\n"."Content-Transfer-Encoding: base64\r\n"."Content-Disposition: attachment; filename=%s\r\n"."Content-ID: <%s>\r\n\r\n", $name, $name, $name);
- $this->_sendLines($buf);
- $this->_sendLines($content, 1);
- }
- return true;
- }
- function send($connect = false, $disconnect = false)
- {
- if($connect)
- if(!$this->connect())
- {
- return false;
- }
- $buf = sprintf("MAIL FROM:<%s>", $this->fromMail);
- $this->_sendLines($buf);
- if($this->_getAnswer() != 250)
- {
- fclose($this->smtpSocket);
- return false;
- }
- if(!$this->_sendRecipients())
- {
- fclose($this->smtpSocket);
- return false;
- }
- $this->_sendLines("DATA");
- if($this->_getAnswer() != 354)
- {
- fclose($this->smtpSocket); return false;
- }
- if(!$this->_sendHeaders())
- {
- fclose($this->smtpSocket);
- return false;
- }
- if(!$this->_sendMessage())
- {
- fclose($this->smtpSocket);
- return false;
- }
- if(!$this->_sendAttachments())
- {
- fclose($this->smtpSocket);
- return false;
- }
- $this->_sendLines(MSG_END);
- if($this->_getAnswer() != 250)
- {
- fclose($this->smtpSocket);
- return false;
- }
- if($disconnect)
- {
- $this->disconnect();
- }
- return true;
- }
- };
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement