--- /dev/null +++ b/class.mailer.php @@ -1,1 +1,336 @@ - +<?php + +/* +* Author : Razvan Stanga +* Date : 03/08/2009 +* Description : Sends MIME emails, TXT/HTML/Attachments +* +*/ + +class Mailer { + + private $cc; + private $bcc; + private $html; + private $txt; + private $attachments; + private $params; + private $charset; + + private $smtp_helo; + private $smtp_host; + private $smtp_port; + private $smtp_msg; + private $smtp_log; + private $smtp_conn; + private $smtp_user; + private $smtp_pass; + + private $sender; + private $from; + private $to; + private $replyto; + private $returnpath; + + private static $instance = array (); + public $IB = false; + public $ORDER = 1000; + + public function __construct () { + $this->cc = array (); + $this->bcc = array (); + $this->attachments = array (); + $this->params = ""; + } + + public static function getInstance ($id="default") { + if( self::$instance[$id] == null) { + self::$instance[$id] = new self; + } + return self::$instance[$id]; + } + + function init () { + + } + + public function setSMTP ($host, $port, $log=0) { + $this->smtp_host = $host; + $this->smtp_port = $port; + $this->smtp_log = $log; + } + + public function setHELO ($helo) { + if ( $helo ) { + $this->smtp_helo = $helo; + } else { + $this->smtp_helo = $this->smtp_host; + } + } + + public function setSASL ($smtp_user, $smtp_pass) { + $this->smtp_user = $smtp_user; + $this->smtp_pass = $smtp_pass; + } + + public function setTo ($email, $name="") { + if ( $name ) { + $this->to = "\"".$name."\" <".$email.">"; + } else { + $this->to = $email; + } + } + + public function setFrom ($email, $name="") { + if ( $name ) { + $this->from = "\"".$name."\" <".$email.">"; + } else { + $this->from = $email; + } + } + + public function setSender ($email, $name="") { + if ( $name ) { + $this->sender = "\"".$name."\" <".$email.">"; + } else { + $this->sender = $email; + } + } + + public function setReplyTo ($email, $name="") { + if ( $name ) { + $this->replyto = "\"".$name."\" <".$email.">"; + } else { + $this->replyto = $email; + } + } + + public function setReturnPath ($email) { + $this->returnpath = $email; + } + + public function setCharset ($charset) { + $this->charset = $charset; + } + + public function setCC ($email, $name="") { + if ( $name ) { + $this->cc[] = "\"".$name."\" <".$email.">"; + } else { + $this->cc[] = $email; + } + } + + public function setBCC ($email, $name="") { + if ( $name ) { + $this->bcc[] = "\"".$name."\" <".$email.">"; + } else { + $this->bcc[] = $email; + } + } + + public function setAttachment ($name, $path) { + $this->attachments[$name] = $path; + } + + public function setSubject ($subject) { + if ( function_exists ('mb_internal_encoding') ) { + mb_internal_encoding ($this->charset); + $subject = html_entity_decode ($subject, ENT_NOQUOTES, $this->charset); + $subject = mb_encode_mimeheader ($subject, $this->charset); + } + $this->subject = $subject; + } + + public function setHTML ($html) { + $this->html = $html; + } + + public function setTXT ($txt) { + $this->txt = $txt; + } + + public function setParams ($params) { + $this->params = $params; + } + + public function clearLog ($clear=1) { + if ( $clear == 1 ) { + $this->smtp_msg = ""; + } + } + + private function _open_smtp_conn() { + if ($this->smtp_conn = @fsockopen ($this->smtp_host, $this->smtp_port)){ + if (in_array($this->_get_smtp_response(), array(220, 250, 354))){ + return true; + } + } + return false; + } + + private function _extract_email($parse) { + preg_match_all("/[\._a-zA-Z0-9-]+@[\._a-zA-Z0-9-]+/i", $parse, $matches); + if (count($matches[0]) == 1){ + return $matches[0][0]; + } + elseif (!count($matches[0])){ + return false; + } + else { + return $matches[0]; + } + } + + private function _close_smtp_conn() { + $this->_send_smtp_command("QUIT"); + @fclose($this->smtp_conn); + } + + private function _send_smtp_command($command, $number="") { + if (@fwrite($this->smtp_conn, $command . "\r\n")){ + $this->smtp_msg .= $this->smtp_log == true ? $command . "\n" : ""; + if (!empty($number)){ + if (!in_array($this->_get_smtp_response(), (array)$number)){ + $this->_close_smtp_conn(); + return false; + } + } + return true; + } + return false; + } + + private function _get_smtp_response() { + do { + $response = chop(@fgets($this->smtp_conn, 1024)); + $this->smtp_msg .= $this->smtp_log == true ? $response . "\n" : ""; + } while($response{3} == "-"); + return intval(substr($response,0,3)); + } + + private function _smtp_send($headers, $body) { + if ($this->_open_smtp_conn()){ + if (!$this->_send_smtp_command("helo {$this->smtp_helo}", array(220, 250, 354))){return false;} + if(!empty($this->smtp_user) && !empty($this->smtp_pass)){ + if (!$this->_send_smtp_command("EHLO {$this->smtp_host}", array(220, 250, 354))){return false;} + if (!$this->_send_smtp_command("AUTH LOGIN", array(334))){return false;} + if (!$this->_send_smtp_command(base64_encode($this->smtp_user), array(334))){return false;} + if (!$this->_send_smtp_command(base64_encode($this->smtp_pass), array(235))){return false;} + } + if (!$this->_send_smtp_command("MAIL FROM:" . $this->returnpath, array(220, 250, 354))){return false;} // FixBug: http://www.developarts.com/version_14_de_nomad_mime_mail#comment-19 + $all_email = $this->_extract_email(implode(", ", array($this->to, implode (", ", $this->cc), implode (", ", $this->bcc)))); + foreach ((array)$all_email as $email){ + if (!$this->_send_smtp_command("RCPT TO:{$email}", array(220, 250, 354))){return false;} + } + if (!$this->_send_smtp_command("DATA", array(220, 250, 354))){return false;} + $this->_send_smtp_command ($headers); + $this->_send_smtp_command ($body); + if (!$this->_send_smtp_command (".", array(220, 250, 354))){return false;} + $this->_close_smtp_conn(); + return true; + } + return false; + } + + public function send ($debug=false) { + if ($this->replyto == '') { + $this->replyto = $this->from; + } + + $boundary = "boundary-" . md5 ( uniqid ("", 1) . time () ); + + // standard headers + $headers = "MIME-Version: 1.0\r\n"; + if ( count ($this->attachments) ) { + $headers .= "Content-Type: multipart/mixed; boundary=\"".$boundary."\"\r\n"; + } else { + $headers .= "Content-Type: multipart/alternative; boundary=\"".$boundary."\"\r\n"; + } + + $headers .= "From: ".$this->from."\r\n"; + $headers .= "Reply-To: ".$this->replyto."\r\n"; + $headers .= "Sender: ".$this->sender."\r\n"; + $headers .= "X-Mailer: razvi.ro/svn/mailer\r\n"; + if ( $this->smtp_host ) { + $headers .= "To: ".$this->to."\r\n"; + $headers .= "Subject: ".$this->subject."\r\n"; + } + // add CC header + if ( count ($this->cc) ) { + $headers .= "CC: ".implode (" ; ", $this->cc)."\r\n"; + } + // add BCC header + if ( count ($this->bcc) ) { + $headers .= "BCC: ".implode (" ; ", $this->bcc)."\r\n"; + } + $headers .= "Date: ".date ("r")."\r\n"; + + // message + $message = "This is a multi-part message in MIME format.\r\n\r\n"; + + // if there are attachments, add custom boundary for message + if ( count ($this->attachments) && ($this->txt || $this->html) ) { + $message .= "--".$boundary."\r\n"; + $pre = "msg-"; + $message .= "Content-Type: multipart/alternative; boundary=\"".$pre.$boundary."\"\r\n\r\n"; + } else { + $pre = ""; + } + + // txt message + if ( $this->txt ) { + $message .= "--".$pre.$boundary."\r\n"; + $message .= "Content-Type: text/plain; charset=\"".$this->charset."\"\r\n"; + $message .= "Content-Transfer-Encoding: 8bit\n"; + $message .= "\r\n"; + $message .= $this->txt; + $message .= "\r\n\r\n"; + } + // html message + if ( $this->html ) { + $message .= "--".$pre.$boundary."\r\n"; + $message .= "Content-Type: text/html; charset=\"".$this->charset."\"\r\n"; + $message .= "Content-Transfer-Encoding: 8bit\r\n"; + $message .= "\r\n"; + $message .= $this->html; + $message .= "\r\n\r\n"; + } + // close boundary for message + if ( $this->txt || $this->html ) { + $message .= "--".$pre.$boundary."--\r\n\r\n"; + } + // add attachments + if ( count ($this->attachments) ) { + foreach ( $this->attachments as $name => $path ) { + $attachment = chunk_split(base64_encode(file_get_contents($path))); + $message .= "--".$boundary."\r\n"; + $message .= "Content-Type: application/octet-stream; name=\"".$name."\"\r\n"; + $message .= "Content-Transfer-Encoding: base64\r\n"; + $message .= "Content-Disposition: attachment; filename=\"".$name."\"\r\n\r\n"; + $message .= $attachment; + } + $message .= "--".$boundary."--"; + } + + if ( $debug == true ) { + echo $headers; + echo $message; + } + if ( $this->smtp_host ) { + $this->_smtp_send ($headers, $message); + } else { + @mail ($this->to, $this->subject, $message, $headers, $this->params); + } + if ( $debug == true ) { + exit (); + } + } + + public function _smtp_msg () { + return $this->smtp_msg; + } +} + + +?>