home *** CD-ROM | disk | FTP | other *** search
/ H4CK3R 4 / hacker04 / 04_HACK04.ISO / src / PHP / pop3smtpclasses.php3.txt < prev    next >
Encoding:
Text File  |  2002-05-06  |  11.1 KB  |  460 lines

  1. POP3, SMTP Classes 
  2.  
  3. include file for POP3 SMTP classes derived from Roderick Groesbeek's Email library, modified to include sending attachments. 
  4.  
  5.  
  6.  
  7. <?php
  8. if ($EMAIL_INC) return;
  9. $EMAIL_INC= "defined";
  10. define("SmtpPort",25);
  11.  
  12. class Pop3 {
  13.     var $subject;                         // string the email's subject 
  14.     var $from_email;                      // string sender's email address 
  15.     var $from_name;                       // string sender's name (opt) 
  16.     var $to_email;                        // string recipient's email 
  17.     var $to_name;                         // string recipient's name (opt) 
  18.     var $body;                            // string body copy 
  19.     var $filename;                        // the filename 
  20.     var $socket;                // the current socket
  21.     var $Line;
  22.     var $Status;
  23.  
  24.     function pop3_open($server, $port)  
  25.     {
  26.  
  27.         $this->Socket = fsockopen($server, $port);
  28.         if ($this->Socket <= 0){
  29.             return false;
  30.         }
  31.     $this->Line = fgets($this->Socket, 1024);
  32.     $this->Status["LASTRESULT"] = substr($this->Line, 0, 1);
  33.     $this->Status["LASTRESULTTXT"] = substr($this->Line, 0, 1024);
  34.  
  35.     if ($this->Status["LASTRESULT"] <> "+") return false;
  36.     return true;
  37.     }
  38.  
  39.     function pop3_user($user)
  40.     {
  41.  
  42.         if ($this->Socket < 0){
  43.           return false;
  44.         }
  45.         fputs($this->Socket, "USER $this->user\r\n");
  46.         $this->Line = fgets($this->Socket, 1024);
  47.         $this->Status["LASTRESULT"] = substr($this->Line, 0, 1);
  48.         $this->Status["LASTRESULTTXT"] = substr($this->Line, 0, 1024);
  49.  
  50.         if ($this->Status["LASTRESULT"] <> "+") return false;
  51.  
  52.         return true;
  53.     }
  54.  
  55.     function pop3_pass( $pass)
  56.     {
  57.  
  58.         fputs($this->Socket, "PASS $pass\r\n");
  59.         $this->Line = fgets($this->Socket, 1024);
  60.         $this->Status["LASTRESULT"] = substr($this->Line, 0, 1);
  61.         $this->Status["LASTRESULTTXT"] = substr($this->Line, 0, 1024);
  62.  
  63.         if ($this->Status["LASTRESULT"] <> "+") return 0;
  64.  
  65.         return 1;
  66.     }
  67.     
  68.     function pop3_stat()
  69.     {
  70.  
  71.         fputs($this->Socket, "STAT\r\n");
  72.         $this->Line = fgets($this->Socket, 1024);
  73.         $this->Status["LASTRESULT"] = substr($this->Line, 0, 1);
  74.         $this->Status["LASTRESULTTXT"] = substr($this->Line, 0, 1024);
  75.  
  76.         if ($this->Status["LASTRESULT"] <> "+") return 0;
  77.  
  78.         if (!eregi("+OK (.*) (.*)", $this->Line, $regs)) 
  79.             return 0;
  80.  
  81.         return $regs[1];
  82.     }
  83.  
  84.     function pop3_list()
  85.     {
  86.         fputs($this->Socket, "LIST\r\n");
  87.         $this->Line = fgets($this->Socket, 1024);
  88.         $this->Status["LASTRESULT"] = substr($this->Line, 0, 1);
  89.         $this->Status["LASTRESULTTXT"] = substr($this->Line, 0, 1024);
  90.  
  91.         if ($this->Status["LASTRESULT"] <> "+") return 0;
  92.  
  93.         $i = 0;
  94.         while  (substr($this->Line  =  fgets($this->Socket, 1024),  0,  1)  <>  ".")
  95.         {
  96.             $articles[$i] = $this->Line;
  97.             $i++;
  98.         }
  99.         $articles["count"] = $i;
  100.  
  101.         return $articles;
  102.     }
  103.  
  104.     function pop3_retr($nr)
  105.     {
  106.     
  107.         fputs($this->Socket, "RETR $nr\r\n");
  108.         $this->Line = fgets($this->Socket, 1024);
  109.         $this->Status["LASTRESULT"] = substr($this->Line, 0, 1);
  110.         $this->Status["LASTRESULTTXT"] = substr($this->Line, 0, 1024);
  111.  
  112.         if ($this->Status["LASTRESULT"] <> "+") return 0;
  113.  
  114.         while  (substr($this->Line  =  fgets($this->Socket, 1024),  0,  1)  <>  ".")
  115.         {
  116.             $data[$i] = $this->Line;
  117.             $i++;
  118.         }
  119.         $data["count"] = $i;
  120.  
  121.         return $data;
  122.     }
  123.  
  124.     function pop3_dele( $nr)
  125.     {
  126.  
  127.         fputs($this->Socket, "DELE $nr\r\n");
  128.         $this->Line = fgets($this->Socket, 1024);
  129.         $this->Status["LASTRESULT"] = substr($this->Line, 0, 1);
  130.         $this->Status["LASTRESULTTXT"] = substr($this->Line, 0, 1024);
  131.  
  132.         if ($this->Status["LASTRESULT"] <> "+") return 0;
  133.         return 1;
  134.     }
  135.  
  136.     function pop3_quit()
  137.     {
  138.  
  139.         fputs($this->Socket, "QUIT\r\n");
  140.         $this->Line = fgets($this->Socket, 1024);
  141.         $this->Status["LASTRESULT"] = substr($this->Line, 0, 1);
  142.         $this->Status["LASTRESULTTXT"] = substr($this->Line, 0, 1024);
  143.  
  144.         if ($this->Status["LASTRESULT"] <> "+") return 0;
  145.  
  146.         return 1;
  147.     }
  148. }
  149.  
  150. class Smtp {
  151.  
  152.     var $Subject;             // string the email's subject 
  153.     var $FromName;                // string sender's name (opt) 
  154.     var $ToName;                  // string recipient's name (opt) 
  155.     var $Body;                    // string body copy 
  156.     var $Attachment;        // attachment (optional)
  157.     var $AttachmentType;
  158.     var $Socket;
  159.     var $Line;
  160.     var $Status;
  161.  
  162.     function Smtp($Server = "localhost",$Port = SmtpPort)
  163.     {    
  164.         return $this->Open($Server, $Port);
  165.     }
  166.     
  167.     function SmtpMail($FromEmail, $FromName, $ToEmail, $ToName, $Subject, $Body, $Attachment=null, $AttachmentType="TEXT")
  168.     {
  169.         $this->Subject   = $Subject;
  170.         $this->ToName    = $ToName;
  171.  
  172.         $this->FromName    = $FromName;
  173.         $this->Body      = $Body;
  174.  
  175.         $this->Attachment = $Attachment;
  176.         $this->AttachmentType = $AttachmentType;
  177.  
  178.         if ($this->Helo() == false){
  179.             return false;
  180.         }
  181.         if ($this->MailFrom($FromEmail) == false){
  182.             return false;
  183.         }
  184.         if ($this->RcptTo($ToEmail) == false){
  185.             return false;
  186.         }
  187.         if ($this->Body() == false){
  188.             return false;
  189.         }
  190.         if ($this->Quit() == false){
  191.             return false;
  192.         }
  193.     }
  194.  
  195.     function Open($Server, $Port)
  196.     {
  197.  
  198.      $this->Socket = fsockopen($Server, $Port);
  199.      if ($this->Socket < 0) return false;
  200.  
  201.      $this->Line = fgets($this->Socket, 1024);
  202.  
  203.      $this->Status["LASTRESULT"] = substr($this->Line, 0, 1);
  204.      $this->Status["LASTRESULTTXT"] = substr($this->Line, 0, 1024);
  205.  
  206.      if ($this->Status["LASTRESULT"] <> "2") return false;
  207.  
  208.      return true;
  209.     }
  210.  
  211.  
  212.     function Helo()
  213.     {
  214.         if (fputs($this->Socket, "helo\r\n") < 0 ){
  215.             return false;
  216.         }
  217.         $this->Line = fgets($this->Socket, 1024);
  218.  
  219.         $this->Status["LASTRESULT"] = substr($this->Line, 0, 1);
  220.         $this->Status["LASTRESULTTXT"] = substr($this->Line, 0, 1024);
  221.  
  222.         if ($this->Status["LASTRESULT"] <> "2") return false;
  223.  
  224.         return true;  
  225.     }
  226.  
  227.     function Ehlo()
  228.     {
  229.  
  230.         /* Well, let's use "helo" for now.. Until we need the
  231.         extra func's   [Unk]
  232.         */
  233.         if(fputs($this->Socket, "helo localhost\r\n")<0){
  234.             return false;
  235.         }
  236.         $this->Line = fgets($this->Socket, 1024);
  237.  
  238.         $this->Status["LASTRESULT"] = substr($this->Line, 0, 1);
  239.         $this->Status["LASTRESULTTXT"] = substr($this->Line, 0, 1024);
  240.  
  241.         if ($this->Status["LASTRESULT"] <> "2") return false;
  242.  
  243.         return true;
  244.     }
  245.  
  246.  
  247.     function MailFrom($FromEmail)
  248.     {
  249.  
  250.         if (fputs($this->Socket, "MAIL FROM: <$FromEmail>\r\n")<0){
  251.             return false;
  252.         }
  253.  
  254.         $this->Line = fgets($this->Socket, 1024);
  255.  
  256.         $this->Status["LASTRESULT"] = substr($this->Line, 0, 1);
  257.         $this->Status["LASTRESULTTXT"] = substr($this->Line, 0, 1024);
  258.  
  259.         if ($this->Status["LASTRESULT"] <> "2") return false;
  260.  
  261.         return true;
  262.     }
  263.  
  264.     function RcptTo($ToEmail)
  265.     {
  266.  
  267.         if(fputs($this->Socket, "RCPT TO: <$ToEmail>\r\n")<0){
  268.             return false;
  269.         }
  270.         $this->Line = fgets($this->Socket, 1024);
  271.  
  272.         $this->Status["LASTRESULT"] = substr($this->Line, 0, 1);
  273.         $this->Status["LASTRESULTTXT"] = substr($this->Line, 0, 1024);
  274.  
  275.         if ($this->Status["LASTRESULT"] <> "2") return false;
  276.             return true;
  277.     }
  278.  
  279.     function Body()
  280.     {
  281.         $FileSize = 0;
  282.         $Attachment = null;
  283.         $fp = null;
  284.  
  285.         $buffer = sprintf("From: %s\r\nTo:%s\r\nSubject:%s\r\n", $this->FromName, $this->ToName, $this->Subject);
  286.  
  287.         if(fputs($this->Socket, "DATA\r\n")<0){
  288.             return false;
  289.         }
  290.         $this->Line = fgets($this->Socket, 1024);
  291.  
  292.         $this->Status["LASTRESULT"] = substr($this->Line, 0, 1);
  293.         $this->Status["LASTRESULTTXT"] = substr($this->Line, 0, 1024);
  294.  
  295.         if ($this->Status["LASTRESULT"] <> "3") return false;
  296.         
  297.         if(fputs($this->Socket, $buffer)<0){
  298.             return false;
  299.         }
  300.  
  301.  
  302.         if ($this->Attachment == null){
  303.  
  304.             if(fputs($this->Socket, "MIME-Version: 1.0\r\nContent-Type: text/plain; charset=ISO-8859-1\r\nContent-Transfer-Encoding: 7bit\r\n\r\n")<0){
  305.                 return false;
  306.             }
  307.             if(fputs($this->Socket, "$this->Body\r\n\r\n")<0){
  308.                 return false;
  309.             }
  310.     
  311.             if(fputs($this->Socket, ".\r\n")<0){
  312.                 return false;
  313.             }
  314.  
  315.             $this->Line = fgets($this->Socket, 1024);
  316.             if (substr($this->Line, 0, 1) <> "2"){
  317.                 return false; 
  318.             }else{
  319.                 return true;
  320.             }
  321.         }else{
  322.             if(fputs($this->Socket,"MIME-Version: 1.0\r\nContent-Type: multipart/mixed; boundary=\"----=_NextPart_000_01BCFA61.A3697360\"\r\n".
  323.                 "Content-Transfer-Encoding: 7bit\r\n\r\n".
  324.                 "This is a multi-part message in MIME format.\r\n".
  325.                 "\r\n------=_NextPart_000_01BCFA61.A3697360\r\n".
  326.                 "Content-Type: text/plain; charset=ISO-8859-1\r\n".
  327.                 "Content-Transfer-Encoding: 7bit\r\n".
  328.                 "\r\n")<0){
  329.                 return false;
  330.             }
  331.     
  332.  
  333.             /* output the body file */
  334.             if(fputs($this->Socket, "$this->Body\r\n\r\n")<0){
  335.                 return false;
  336.             }
  337.  
  338.             if ( fputs($this->Socket,"\r\n------=_NextPart_000_01BCFA61.A3697360\r\n")<0){
  339.                 return false;
  340.             }
  341.             $FileSize = filesize($this->Attachment);
  342.             if ($FileSize == false){
  343.                 return false;
  344.             }
  345.             if (($fp = fopen($this->Attachment,"r"))== false) {
  346.                 return false;
  347.             }else{
  348.                 $Attachment = fread($fp,$FileSize);    
  349.             }
  350.  
  351.             // we don't want any of the directory in the attachment
  352.             if (($AttachName = strrchr($this->Attachment,'/')) == false){
  353.  
  354.                 // no directory so just copy the name in
  355.                 $AttachName = $this->Attachment;
  356.             }
  357.  
  358.             if( fputs($this->Socket,
  359.                 "Content-Type: application/octet-stream; \r\nname=\"$AttachName\"\r\n".
  360.                 "Content-Transfer-Encoding: quoted-printable\r\n".
  361.                 "Content-Description: $AttachName\r\n".
  362.                 "Content-Disposition: attachment; \r\n\tfilename=\"$AttachName\"\r\n".
  363.                 "\r\n")<0){
  364.                 return false;
  365.             }
  366.     
  367.             /* output the attachment file */
  368.             if( fputs($this->Socket, $Attachment)<0){
  369.                 return false;
  370.             }
  371.             if ( fputs($this->Socket,"\r\n\r\n------=_NextPart_000_01BCFA61.A3697360--\r\n")<0){
  372.                 return false;
  373.             }
  374.  
  375.             if( fputs($this->Socket,".\r\n")<0){
  376.                 return false;
  377.             }
  378.  
  379.             $this->Line = fgets($this->Socket, 1024);
  380.             if (substr($this->Line, 0, 1) <> "2")
  381.                 return false; 
  382.  
  383.             return true;
  384.  
  385.         }
  386.     }
  387.  
  388.     function Quit()
  389.     {
  390.  
  391.         if(fputs($this->Socket, "QUIT\r\n")<0){
  392.             return false;
  393.         }
  394.         $this->Line = fgets($this->Socket, 1024);
  395.  
  396.         $this->Status["LASTRESULT"] = substr($this->Line, 0, 1);
  397.         $this->Status["LASTRESULTTXT"] = substr($this->Line, 0, 1024);
  398.  
  399.         if ($this->Status["LASTRESULT"] <> "2") return 0;
  400.  
  401.         return 1;
  402.     } 
  403.     function Close()
  404.     {
  405.         fclose($this->Socket);
  406.     }
  407. }
  408. /*
  409.  
  410. Example how to use
  411.  
  412. $MailTo = new Smtp();
  413. $MailTo->SmtpMail("Dave@micro-automation.net","Dave Cramer",
  414.            "Dave@micro-automation.net","David",
  415.            "Test Mail",$MailMessage,"service.tab",0);
  416. $MailTo->Close();
  417. $MailTo=null;
  418.  
  419. */
  420. /*
  421.  $pop3 = pop3_open("localhost", "110");
  422.  if (!$pop3) {
  423.                 printf("[ERROR] Failed to connect to localhost<BR>\n");
  424.                 return 0;
  425.  }
  426.  
  427.  if (!pop3_user($pop3, "unk")) {
  428.                 printf("[ERROR] Username failed!<BR>\n");
  429.                 return 0;
  430.  }
  431.  
  432.  if (!pop3_pass($pop3, "secret")) {
  433.                 printf("[ERROR] PASS failed!<BR>\n");
  434.                 return 0;
  435.  }
  436.  
  437.  $articles = pop3_list($pop3);
  438.  if (!$articles) {
  439.                 printf("[ERROR] LIST failed!<BR>\n");
  440.                 return 0;
  441.  }
  442.  
  443.  for ($i = 1; $i < $articles ["count"] + 1; $i++)
  444.  {
  445.                 printf("i=$i<BR>\n");
  446.                 $data = pop3_retr($pop3,$i);
  447.                 if (!$data) {
  448.                                 printf("data goes wrong on '$i'<BR>\n");
  449.                                 return 0;
  450.                 }
  451.  
  452.                 for ($j = 0; $j < $data["count"]; $j++)
  453.                 {
  454.                                 printf("$data[$j]<BR>\n");
  455.                 }
  456.  }
  457. */
  458. ?>
  459.  
  460.