Advertisement
Guest User

Untitled

a guest
Jun 6th, 2017
528
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 12.59 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  *+---------------------------------+
  5.  *|          Mailer Class           |
  6.  *+---------------------------------+
  7. **/
  8.  
  9. class Mailer
  10. {
  11.  
  12.     var $smtpServer = "";
  13.     var $smtpPort = 25;
  14.     var $lstAttachs = array();
  15.     var $lstRecipients = array();
  16.     var $lstHeaders = array();
  17.     var $authUser = "";
  18.     var $authPass = "";
  19.     var $replyName = "";
  20.     var $replyMail = "";
  21.     var $fromName = "";
  22.     var $fromMail = "";
  23.     var $isMimeOn = 1;
  24.     var $isDebug = 0;
  25.     var $isHtml = 1;
  26.     var $msgSubject = "";
  27.     var $msgBody = "";
  28.     var $smtpSocket = 0;
  29.    
  30.     function Mailer($smtpServer = false, $smtpPort = 25)
  31.     {
  32.         define("EOL", "\r\n");
  33.         define("MSG_END", "\r\n.\r\n");
  34.         define("EOH", "\r\n\r\n");
  35.         define("SEP", "\r\n--#BOUNDARY#--");
  36.         define("EOM", "QUIT\r\n\0");
  37.         $this->smtpServer = (($smtpServer !== false) ? $smtpServer : $_SERVER["LOCAL_ADDR"]);
  38.         $this->smtpPort = $smtpPort;
  39.     }
  40.    
  41.     function setSubject($msgSubject)
  42.     {
  43.         $this->msgSubject = $msgSubject;
  44.     }
  45.    
  46.     function setBody($msgBody)
  47.     {
  48.         $this->msgBody = $msgBody;
  49.     }
  50.    
  51.     function setFrom($fromName, $fromMail)
  52.     {
  53.         $this->fromName = $fromName;
  54.         $this->fromMail = $fromMail;
  55.     }
  56.    
  57.     function setReplyTo($reply2Name, $reply2Mail)
  58.     {
  59.         $this->replyName = $reply2Name;
  60.         $this->replyMail = $reply2Mail;
  61.     }
  62.    
  63.     function addAttachment($filePath, $fileName)
  64.     {
  65.         $filePath = str_replace("\\", "/", $filePath);
  66.        
  67.         if(!isset($fileName))
  68.         {
  69.             $fileName = substr(strrchr($filePath, '/'), 1);
  70.         }
  71.        
  72.         $fileExtension = substr(strrchr($fileName, '.'), 1);
  73.         $mimeType = "application/octet-stream";
  74.        
  75.         if(isset($MimeTypes[$fileExtension]))
  76.         {
  77.             $mimeType = $MimeTypes[$fileExtension];
  78.         }
  79.        
  80.         return $this->attachFile($filePath, $fileName, $mimeType);
  81.     }
  82.    
  83.     function attachFile($filePath, $fileName, $mimeType = "application/octet-stream")
  84.     {
  85.         if(!filesize($filePath))
  86.         {
  87.           return false;
  88.         }
  89.        
  90.         $this->lstAttachs[$fileName] = Array(
  91.             "Path" => $filePath,
  92.             "Mime" => $mimeType
  93.         );
  94.        
  95.         return true;
  96.     }
  97.    
  98.     function addRecipient($recName, $recMail, $recType)
  99.     {
  100.         if(!isset($recType))
  101.         {
  102.             $recType = "to";
  103.         }
  104.         else
  105.         {
  106.             $recType = strtolower($recType);
  107.         }
  108.        
  109.         $recType = strtolower($recType);
  110.        
  111.         if(!is_array(isset($this->lstRecipients[$recType])))
  112.         {
  113.             $this->lstRecipients[$recType] = array();
  114.         }
  115.        
  116.         $this->lstRecipients[$recType][$recMail] = $recName;
  117.     }
  118.    
  119.     function addHeader($hdrName, $hdrValue)
  120.     {
  121.         $this->lstHeaders[$hdrName] = $hdrValue;
  122.     }
  123.    
  124.     function setImportance($importance)
  125.     {
  126.         $this->addHeader("X-Importance", $importance);
  127.     }
  128.    
  129.     function setSensitivity($sensitivity)
  130.     {
  131.         $this->addHeader("X-Sensitivity", $sensitivity, "X");
  132.     }
  133.    
  134.     function setPriority($priority)
  135.     {
  136.         $this->addHeader("X-Priority", $priority);
  137.     }
  138.    
  139.     function setAuth($authUser, $authPass)
  140.     {
  141.         $this->authUser = $authUser;
  142.         $this->authPass = $authPass;
  143.     }
  144.    
  145.     function _debugLine($line, $sent = 1)
  146.     {
  147.         $line = trim($line);
  148.        
  149.         if(!$this->isDebug)
  150.         {
  151.             return;
  152.         }
  153.        
  154.         echo nl2br(htmlentities($line)."<br />");
  155.     }
  156.    
  157.     function _sendLines($lstLines, $raw = 0)
  158.     {
  159.         if(!is_array($lstLines))
  160.         {
  161.             $lstLines = str_replace("\r", "", $lstLines);
  162.             $lstLines = explode("\n", $lstLines);
  163.            
  164.             if(!count($lstLines))
  165.             {
  166.                 $lstLines = array($lstLines);
  167.             }
  168.         }
  169.    
  170.         foreach($lstLines as $line)
  171.         {
  172.             $line = trim($line);
  173.             $this->_debugLine($line);
  174.            
  175.             if(!fputs($this->smtpSocket, $line."\r\n"))
  176.             {
  177.                 return false;
  178.             }
  179.         }
  180.         return true;
  181.     }
  182.    
  183.     function _getAnswer()
  184.     {
  185.         $line = "";
  186.        
  187.         while(!feof($this->smtpSocket))
  188.         {
  189.             $ch = fgetc($this->smtpSocket);
  190.            
  191.             if(!strlen($ch))
  192.             {
  193.                 return false;
  194.             }
  195.            
  196.             if($ch == "\n")
  197.             {
  198.                 $this->_debugLine($line, 0);
  199.            
  200.                 if($line[3] == " ")
  201.                 {
  202.                     return (int)substr($line, 0, 3);
  203.                 }
  204.                
  205.                 $line = "";
  206.                 continue;
  207.             }
  208.            
  209.             if($ch != "\r")
  210.             {
  211.                 $line .= $ch;
  212.             }
  213.         }
  214.        
  215.         return false;
  216.     }
  217.    
  218.     function _authLogin()
  219.     {
  220.         $buf = "AUTH LOGIN";
  221.         $this->_sendLines($buf);
  222.        
  223.         if($this->_getAnswer() != 334)
  224.         {
  225.             fclose($this->smtpSocket);
  226.             return false;
  227.         }
  228.        
  229.         $buf=sprintf("%s", $this->authUser);
  230.         $this->_sendLines($buf);
  231.        
  232.         if($this->_getAnswer() != 334)
  233.         {
  234.             fclose($this->smtpSocket);
  235.             return false;
  236.         }
  237.        
  238.         $buf=sprintf("%s", $this->authPass);
  239.         $this->_sendLines($buf);
  240.        
  241.         if($this->_getAnswer() != 235)
  242.         {
  243.             fclose($this->smtpSocket);
  244.             return false;
  245.         }
  246.        
  247.         return true;
  248.     }
  249.    
  250.     function connect($timeout = 5)
  251.     {
  252.         $errno = "";
  253.         $errstr = "";
  254.         $this->smtpSocket = fsockopen($this->smtpServer, $this->smtpPort, $errno, $errstr, $timeout);
  255.        
  256.         if(!$this->smtpSocket)
  257.         {
  258.             $this->_debugString($errno.":".$errstr."\r\n");
  259.             return false;
  260.         }
  261.        
  262.        
  263.         if($this->_getAnswer() != 220)
  264.         {
  265.             fclose($this->smtpSocket);
  266.             return false;
  267.         }
  268.        
  269.         if(($this->authUser != "") && ($this->authPass != ""))
  270.         {
  271.             $buf = sprintf("EHLO %s", "localhost");
  272.         }
  273.         else
  274.         {
  275.             $buf = sprintf("HELO %s", "localhost");
  276.         }
  277.        
  278.         $this->_sendLines($buf);
  279.         $hiReply = $this->_getAnswer();
  280.        
  281.         if($hiReply == 250)
  282.         {
  283.             return $this->_authLogin();
  284.         }
  285.        
  286.         $buf = sprintf("HELO %s","localhost");
  287.        
  288.         if($this->_getAnswer() != 250)
  289.         {
  290.             fclose($this->smtpSocket);
  291.             return false;
  292.         }
  293.        
  294.         return true;
  295.     }
  296.    
  297.     function disconnect()
  298.     {
  299.         $this->_sendLines("QUIT");
  300.         $quitReply = $this->_getAnswer();
  301.         fclose($this->smtpSocket);
  302.        
  303.         if($quitReply == 221)
  304.         {
  305.             return true;
  306.         }
  307.        
  308.         return true;
  309.     }
  310.    
  311.     function _sendRecipients()
  312.     {
  313.         $result = 0;
  314.         $mails = array();
  315.         $mailsErr = array();
  316.    
  317.         while(list($type, $list) = each($this->lstRecipients))
  318.         {
  319.    
  320.             while(list($mail, $name) = each($list))
  321.             {
  322.    
  323.                 if(in_array($mail, $mails))
  324.                 {
  325.                     continue;
  326.                 }
  327.                
  328.                 $buf =sprintf("RCPT TO:<%s>", $mail);
  329.                 $this->_sendLines($buf);
  330.                 $rez =$this->_getAnswer();
  331.                 array_push($mails, $mail);
  332.    
  333.                 if($rez == 250)
  334.                 {
  335.                     continue;
  336.                 }
  337.                
  338.                 array_push($mailsErr, $mail);
  339.                 unset($this->lstRecipients[$type][$mail]);
  340.             }
  341.         }
  342.        
  343.         return ((count($mails) - count($mailsErr)) > 0);
  344.     }
  345.    
  346.     function _sendHeaders()
  347.     {
  348.         reset($this->lstHeaders);
  349.        
  350.         while(list($name, $value) = each($this->lstHeaders))
  351.         {
  352.             $buf = "$name: $value";
  353.             $this->_sendLines($buf);
  354.         }
  355.        
  356.         reset($this->lstRecipients);
  357.        
  358.         while(list($type, $list) = each($this->lstRecipients))
  359.         {
  360.             $mails = array();
  361.        
  362.             while(list($mail, $name) = each($list))
  363.             {
  364.                 array_push($mails, "$name <$mail>");
  365.             }
  366.        
  367.             $type[0] = strtoupper($type[0]);
  368.        
  369.             if(isset($this->lstHeaders[$type]))
  370.             {
  371.                 continue;
  372.             }
  373.        
  374.             $buf = "$type: ".implode(",", $mails)."";
  375.             $this->_sendLines($buf);
  376.         }
  377.        
  378.         $buf = sprintf("From: %s <%s>", $this->fromName, $this->fromMail);
  379.         $this->_sendLines($buf);
  380.        
  381.         if(strlen($this->replyMail))
  382.         {
  383.             $buf = sprintf("Reply-to: %s <%s>", $this->replyName, $this->replyMail);
  384.             $this->_sendLines($buf);
  385.         }
  386.        
  387.         $buf = sprintf("Subject: %s", $this->msgSubject);
  388.         $this->_sendLines($buf);
  389.         return true;
  390.     }
  391.    
  392.     function _sendMessage()
  393.     {
  394.         if($this->isMimeOn)
  395.         {
  396.             $buf = "MIME-Version: 1.0\r\n"."Content-type: multipart/mixed; boundary=\"#BOUNDARY#\"\r\n\r\n";
  397.             $this->_sendLines($buf);
  398.             $buf = "\r\n--#BOUNDARY#\r\n"."Content-Type: text/".($this->isHtml ? "html" : "plain")."; charset=us-ascii\r\n";
  399.             $this->_sendLines($buf);
  400.         }
  401.         else
  402.         {
  403.             $buf = "\r\n";
  404.             $this->_sendLines($buf);
  405.         }
  406.    
  407.         $this->_sendLines($this->msgBody, 1);
  408.         return true;
  409.     }
  410.    
  411.     function _sendAttachments()
  412.     {
  413.         if(!$this->isMimeOn)
  414.         {
  415.             return true;
  416.         }
  417.    
  418.         if(!count($this->lstAttachs))
  419.         {
  420.             return true;
  421.         }
  422.    
  423.         while(list($name, $file) = each($this->lstAttachs))
  424.         {
  425.             $fpath = $file['Path'];
  426.             $mime = $file['Mime'];
  427.             $fname = $this->names[$i];
  428.             $newfile = fopen($fpath, "rb");
  429.             $content = fread($newfile, filesize($fpath));
  430.             fclose($newfile);
  431.             $content = base64_encode($content);
  432.             $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);
  433.             $this->_sendLines($buf);
  434.             $this->_sendLines($content, 1);
  435.         }
  436.    
  437.         return true;
  438.     }
  439.    
  440.     function send($connect = false, $disconnect = false)
  441.     {
  442.         if($connect)
  443.             if(!$this->connect())
  444.             {
  445.                 return false;
  446.             }
  447.            
  448.             $buf = sprintf("MAIL FROM:<%s>", $this->fromMail);
  449.             $this->_sendLines($buf);
  450.            
  451.             if($this->_getAnswer() != 250)
  452.             {
  453.                 fclose($this->smtpSocket);
  454.                 return false;
  455.             }
  456.            
  457.             if(!$this->_sendRecipients())
  458.             {
  459.                 fclose($this->smtpSocket);
  460.                 return false;
  461.             }
  462.            
  463.             $this->_sendLines("DATA");
  464.            
  465.             if($this->_getAnswer() != 354)
  466.             {
  467.                 fclose($this->smtpSocket); return false;
  468.             }
  469.            
  470.             if(!$this->_sendHeaders())
  471.             {
  472.                 fclose($this->smtpSocket);
  473.                 return false;
  474.             }
  475.            
  476.             if(!$this->_sendMessage())
  477.             {
  478.                 fclose($this->smtpSocket);
  479.                 return false;
  480.             }
  481.            
  482.             if(!$this->_sendAttachments())
  483.             {
  484.                 fclose($this->smtpSocket);
  485.                 return false;
  486.             }
  487.            
  488.             $this->_sendLines(MSG_END);
  489.            
  490.             if($this->_getAnswer() != 250)
  491.             {
  492.                 fclose($this->smtpSocket);
  493.                 return false;
  494.             }
  495.            
  496.             if($disconnect)
  497.             {
  498.                 $this->disconnect();
  499.             }
  500.            
  501.             return true;
  502.     }
  503. };
  504. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement