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

  1. POP3, SMTP, MAIL PHP 
  2.  
  3. POP3 and SMTP functions. Well, you could call it a Mail library :) 
  4.  
  5.  
  6.  
  7. <?
  8.   /* Pop3 Functions.
  9.      Smtp Functions. [Added later, going to split]
  10.  
  11.      For a real example take a look at:
  12.      http://www.triple-it.nl/pop3/
  13.  
  14.      I have just started to explore the world of PHP3,
  15.      so excuse (and correct :) me if I'm doing something wrong.
  16.  
  17.      Unk. (rgroesb_garbage@triple-it_garbage.nl)
  18.    */
  19.  
  20.   function pop3_open($server, $port)  
  21.   {
  22.         global $POP3_GLOBAL_STATUS;
  23.  
  24.         $pop3 = fsockopen($server, $port);
  25.         if ($pop3 <= 0) return 0;
  26.  
  27.         $line = fgets($pop3, 1024);
  28.         $POP3_GLOBAL_STATUS[$pop3]["LASTRESULT"] = substr($line, 0, 1);
  29.         $POP3_GLOBAL_STATUS[$pop3]["LASTRESULTTXT"] = substr($line, 0, 1024);
  30.  
  31.         if ($POP3_GLOBAL_STATUS[$pop3]["LASTRESULT"] <> "+") return 0;
  32.  
  33.         return $pop3;
  34.   }
  35.  
  36.   function pop3_user($pop3, $user)
  37.   {
  38.         global $POP3_GLOBAL_STATUS;
  39.  
  40.         fputs($pop3, "USER $user\r\n");
  41.         $line = fgets($pop3, 1024);
  42.         $POP3_GLOBAL_STATUS[$pop3]["LASTRESULT"] = substr($line, 0, 1);
  43.         $POP3_GLOBAL_STATUS[$pop3]["LASTRESULTTXT"] = substr($line, 0, 1024);
  44.  
  45.         if ($POP3_GLOBAL_STATUS[$pop3]["LASTRESULT"] <> "+") return 0;
  46.  
  47.         return 1;
  48.   }
  49.  
  50.   function pop3_pass($pop3, $pass)
  51.   {
  52.         global $POP3_GLOBAL_STATUS;
  53.  
  54.         fputs($pop3, "PASS $pass\r\n");
  55.         $line = fgets($pop3, 1024);
  56.         $POP3_GLOBAL_STATUS[$pop3]["LASTRESULT"] = substr($line, 0, 1);
  57.         $POP3_GLOBAL_STATUS[$pop3]["LASTRESULTTXT"] = substr($line, 0, 1024);
  58.  
  59.         if ($POP3_GLOBAL_STATUS[$pop3]["LASTRESULT"] <> "+") return 0;
  60.  
  61.         return 1;
  62.   }
  63.   
  64.   function pop3_stat($pop3)
  65.   {
  66.         global $POP3_GLOBAL_STATUS;
  67.  
  68.         fputs($pop3, "STAT\r\n");
  69.         $line = fgets($pop3, 1024);
  70.         $POP3_GLOBAL_STATUS[$pop3]["LASTRESULT"] = substr($line, 0, 1);
  71.         $POP3_GLOBAL_STATUS[$pop3]["LASTRESULTTXT"] = substr($line, 0, 1024);
  72.  
  73.         if ($POP3_GLOBAL_STATUS[$pop3]["LASTRESULT"] <> "+") return 0;
  74.  
  75.         if (!eregi("+OK (.*) (.*)", $line, $regs)) 
  76.                 return 0;
  77.  
  78.         return $regs[1];
  79.   }
  80.  
  81.   function pop3_list($pop3)
  82.   {
  83.         global $POP3_GLOBAL_STATUS;
  84.   
  85.         fputs($pop3, "LIST\r\n");
  86.         $line = fgets($pop3, 1024);
  87.         $POP3_GLOBAL_STATUS[$pop3]["LASTRESULT"] = substr($line, 0, 1);
  88.         $POP3_GLOBAL_STATUS[$pop3]["LASTRESULTTXT"] = substr($line, 0, 1024);
  89.  
  90.         if ($POP3_GLOBAL_STATUS[$pop3]["LASTRESULT"] <> "+") return 0;
  91.  
  92.         $i = 0;
  93.         while  (substr($line  =  fgets($pop3, 1024),  0,  1)  <>  ".")
  94.         {
  95.                 $articles[$i] = $line;
  96.                 $i++;
  97.         }
  98.         $articles["count"] = $i;
  99.  
  100.         return $articles;
  101.   }
  102.  
  103.   function pop3_retr($pop3, $nr)
  104.   {
  105.         global $POP3_GLOBAL_STATUS;
  106.   
  107.         fputs($pop3, "RETR $nr\r\n");
  108.         $line = fgets($pop3, 1024);
  109.         $POP3_GLOBAL_STATUS[$pop3]["LASTRESULT"] = substr($line, 0, 1);
  110.         $POP3_GLOBAL_STATUS[$pop3]["LASTRESULTTXT"] = substr($line, 0, 1024);
  111.  
  112.         if ($POP3_GLOBAL_STATUS[$pop3]["LASTRESULT"] <> "+") return 0;
  113.  
  114.         while  (substr($line  =  fgets($pop3, 1024),  0,  1)  <>  ".")
  115.         {
  116.                 $data[$i] = $line;
  117.                 $i++;
  118.         }
  119.         $data["count"] = $i;
  120.  
  121.         return $data;
  122.   }
  123.  
  124.   function pop3_dele($pop3, $nr)
  125.   {
  126.         global $POP3_GLOBAL_STATUS;
  127.  
  128.         fputs($pop3, "DELE $nr\r\n");
  129.         $line = fgets($pop3, 1024);
  130.         $POP3_GLOBAL_STATUS[$pop3]["LASTRESULT"] = substr($line, 0, 1);
  131.         $POP3_GLOBAL_STATUS[$pop3]["LASTRESULTTXT"] = substr($line, 0, 1024);
  132.  
  133.         if ($POP3_GLOBAL_STATUS[$pop3]["LASTRESULT"] <> "+") return 0;
  134.  
  135.  
  136.         return 1;
  137.   }
  138.   function pop3_quit($pop3)
  139.   {
  140.         global $POP3_GLOBAL_STATUS;
  141.  
  142.         fputs($pop3, "QUIT\r\n");
  143.         $line = fgets($pop3, 1024);
  144.         $POP3_GLOBAL_STATUS[$pop3]["LASTRESULT"] = substr($line, 0, 1);
  145.         $POP3_GLOBAL_STATUS[$pop3]["LASTRESULTTXT"] = substr($line, 0, 1024);
  146.  
  147.         if ($POP3_GLOBAL_STATUS[$pop3]["LASTRESULT"] <> "+") return 0;
  148.  
  149.         return 1;
  150.   }
  151.  
  152.   function smtp_open($server, $port)
  153.   {
  154.         global $SMTP_GLOBAL_STATUS;
  155.  
  156.         $smtp = fsockopen($server, $port);
  157.         if ($smtp < 0) return 0;
  158.  
  159.         $line = fgets($smtp, 1024);
  160.  
  161.         $SMTP_GLOBAL_STATUS[$smtp]["LASTRESULT"] = substr($line, 0, 1);
  162.         $SMTP_GLOBAL_STATUS[$smtp]["LASTRESULTTXT"] = substr($line, 0, 1024);
  163.  
  164.         if ($SMTP_GLOBAL_STATUS[$smtp]["LASTRESULT"] <> "2") return 0;
  165.  
  166.         return $smtp;
  167.   }
  168.  
  169.  
  170.   function smtp_helo($smtp)
  171.   {
  172.         global $SMTP_GLOBAL_STATUS;
  173.  
  174.         /* 'localhost' always works [Unk] */
  175.         fputs($smtp, "helo localhost\r\n");
  176.         $line = fgets($smtp, 1024);
  177.         $SMTP_GLOBAL_STATUS[$smtp]["LASTRESULT"] = substr($line, 0, 1);
  178.         $SMTP_GLOBAL_STATUS[$smtp]["LASTRESULTTXT"] = substr($line, 0, 1024);
  179.  
  180.         if ($SMTP_GLOBAL_STATUS[$smtp]["LASTRESULT"] <> "2") return 0;
  181.  
  182.         return 1;  }
  183.  
  184.   function smtp_ehlo($smtp)
  185.   {
  186.         global $SMTP_GLOBAL_STATUS;
  187.  
  188.         /* Well, let's use "helo" for now.. Until we need the
  189.            extra func's   [Unk]
  190.          */
  191.         fputs($smtp, "helo localhost\r\n");
  192.         $line = fgets($smtp, 1024);
  193.         $SMTP_GLOBAL_STATUS[$smtp]["LASTRESULT"] = substr($line, 0, 1);
  194.         $SMTP_GLOBAL_STATUS[$smtp]["LASTRESULTTXT"] = substr($line, 0, 1024);
  195.  
  196.         if ($SMTP_GLOBAL_STATUS[$smtp]["LASTRESULT"] <> "2") return 0;
  197.  
  198.         return 1;
  199.   }
  200.  
  201.  
  202.   function smtp_mail_from($smtp, $from)
  203.   {
  204.         global $SMTP_GLOBAL_STATUS;
  205.  
  206.         fputs($smtp, "MAIL FROM: <$from>\r\n");
  207.         $line = fgets($smtp, 1024);
  208.         $SMTP_GLOBAL_STATUS[$smtp]["LASTRESULT"] = substr($line, 0, 1);
  209.         $SMTP_GLOBAL_STATUS[$smtp]["LASTRESULTTXT"] = substr($line, 0, 1024);
  210.  
  211.         if ($SMTP_GLOBAL_STATUS[$smtp]["LASTRESULT"] <> "2") return 0;
  212.  
  213.         return 1;
  214.   }
  215.  
  216.   function smtp_rcpt_to($smtp, $to)
  217.   {
  218.         global $SMTP_GLOBAL_STATUS;
  219.  
  220.         fputs($smtp, "RCPT TO: <$to>\r\n");
  221.         $line = fgets($smtp, 1024);
  222.         $SMTP_GLOBAL_STATUS[$smtp]["LASTRESULT"] = substr($line, 0, 1);
  223.         $SMTP_GLOBAL_STATUS[$smtp]["LASTRESULTTXT"] = substr($line, 0, 1024);
  224.  
  225.         if ($SMTP_GLOBAL_STATUS[$smtp]["LASTRESULT"] <> "2") return 0;
  226.  
  227.  
  228.         return 1;
  229.   }
  230.  
  231.   function smtp_data($smtp, $subject, $data)
  232.   {
  233.         global $SMTP_GLOBAL_STATUS;
  234.  
  235.         fputs($smtp, "DATA\r\n");
  236.         $line = fgets($smtp, 1024);
  237.         $SMTP_GLOBAL_STATUS[$smtp]["LASTRESULT"] = substr($line, 0, 1);
  238.         $SMTP_GLOBAL_STATUS[$smtp]["LASTRESULTTXT"] = substr($line, 0, 1024);
  239.  
  240.         if ($SMTP_GLOBAL_STATUS[$smtp]["LASTRESULT"] <> "3") return 0;
  241.  
  242.         fputs($smtp, "Mime-Version: 1.0\r\n");
  243.         fputs($smtp, "Subject: $subject\r\n");
  244.         fputs($smtp, "$data\r\n\r\n");
  245.         fputs($smtp, ".\r\n");
  246.         $line = fgets($smtp, 1024);
  247.         if (substr($line, 0, 1) <> "2")
  248.                 return 0; 
  249.  
  250.         return 1;
  251.   }
  252.  
  253.   function smtp_quit($smtp)
  254.   {
  255.         global $SMTP_GLOBAL_STATUS;
  256.  
  257.         fputs($smtp, "QUIT\r\n");
  258.         $line = fgets($smtp, 1024);
  259.         $SMTP_GLOBAL_STATUS[$smtp]["LASTRESULT"] = substr($line, 0, 1);
  260.         $SMTP_GLOBAL_STATUS[$smtp]["LASTRESULTTXT"] = substr($line, 0, 1024);
  261.  
  262.         if ($SMTP_GLOBAL_STATUS[$smtp]["LASTRESULT"] <> "2") return 0;
  263.  
  264.         return 1;
  265.   } 
  266.   
  267.  
  268.   
  269. /*
  270.  $pop3 = pop3_open("localhost", "110");
  271.  if (!$pop3) {
  272.         printf("[ERROR] Failed to connect to localhost<BR>\n");
  273.         return 0;
  274.  }
  275.  
  276.  if (!pop3_user($pop3, "unk")) {
  277.         printf("[ERROR] Username failed!<BR>\n");
  278.         return 0;
  279.  }
  280.  
  281.  if (!pop3_pass($pop3, "secret")) {
  282.         printf("[ERROR] PASS failed!<BR>\n");
  283.         return 0;
  284.  }
  285.  
  286.  $articles = pop3_list($pop3);
  287.  if (!$articles) {
  288.         printf("[ERROR] LIST failed!<BR>\n");
  289.         return 0;
  290.  }
  291.  
  292.  for ($i = 1; $i < $articles ["count"] + 1; $i++)
  293.  {
  294.         printf("i=$i<BR>\n");
  295.         $data = pop3_retr($pop3,$i);
  296.         if (!$data) {
  297.                 printf("data goes wrong on '$i'<BR>\n");
  298.                 return 0;
  299.         }
  300.  
  301.         for ($j = 0; $j < $data["count"]; $j++)
  302.         {
  303.                 printf("$data[$j]<BR>\n");
  304.         }
  305.  }
  306.  */
  307.  
  308.  
  309. ?>
  310.  
  311.