Advertisement
Guest User

michi7x7

a guest
May 3rd, 2009
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.87 KB | None | 0 0
  1. <?php
  2.  
  3. class mail
  4.  
  5. {
  6.  
  7.     private $smtp_settings=array();
  8.  
  9.    
  10.  
  11.     private $trenner;
  12.  
  13.    
  14.  
  15.     private $use_html;
  16.  
  17.    
  18.  
  19.     private $files=array();
  20.  
  21.    
  22.  
  23.     private $header="";
  24.  
  25.    
  26.  
  27.     public function __construct($from_name,$from_mail, $use_html=true,$reply="")
  28.  
  29.     {
  30.  
  31.         $this->use_html=$use_html;
  32.  
  33.        
  34.  
  35.         $this->smtp_settings=array("from"=> $from_name."<".$from_mail.">");
  36.  
  37.        
  38.  
  39.         $this->trenner  = md5(uniqid(time()));
  40.  
  41.         $this->header   = "From: $from_name <$from_mail>\n";
  42.  
  43.         if($reply=="")
  44.  
  45.             $reply=$from_mail;
  46.  
  47.        
  48.  
  49.         $this->header .= "Reply-To: ".$from_name." <".$reply.">\n";
  50.  
  51.        
  52.  
  53.         $this->header .= "MIME-Version: 1.0";
  54.  
  55.         $this->header .= "\n";
  56.  
  57.         $this->header .= "Content-Type: multipart/mixed; boundary=".$this->trenner;
  58.  
  59.         $this->header .= "\n\n";
  60.  
  61.         $this->header .= "This is a multi-part message in MIME format";
  62.  
  63.         $this->header .= "\n";
  64.  
  65.         $this->header .= "--".$this->trenner;
  66.  
  67.         $this->header .= "\n";
  68.  
  69.         if($use_html)
  70.  
  71.             $this->header .= "Content-Type: text/html";
  72.  
  73.         else
  74.  
  75.             $this->header .= "Content-Type: text/plain";
  76.  
  77.         $this->header .= "\n";
  78.  
  79.         $this->header .= "Content-Transfer-Encoding: 8bit";
  80.  
  81.         $this->header .= "\n\n";
  82.  
  83.     }
  84.  
  85.    
  86.  
  87.     public function text($text)
  88.  
  89.     {
  90.  
  91.         if(!preg_match("/(.*)\n/m",$text))
  92.  
  93.             $text=$text."\n";
  94.  
  95.        
  96.  
  97.         if($this->use_html)
  98.  
  99.         {
  100.  
  101.             $text=htmlspecialchars($text);
  102.  
  103.             $text=str_replace(array("\r\n","\n","\r"),"<br>",$text);
  104.  
  105.         }
  106.  
  107.        
  108.  
  109.         $this->header.=$text;
  110.  
  111.     }
  112.  
  113.    
  114.  
  115.    
  116.  
  117.     function html($text)
  118.  
  119.     {
  120.  
  121.         if(!$this->use_html)
  122.  
  123.         {
  124.  
  125.             $text=str_replace(array("\r\n","\n","\r"),"",$text);
  126.  
  127.             $text=str_replace(array("<br>","</p>"),"\n",$text);
  128.  
  129.             $text=preg_replace("/<(.*?)>/i","",$text);
  130.  
  131.            
  132.  
  133.             if(!preg_match("/(.*)\n/m",$text))
  134.  
  135.                 $text=$text."\n";
  136.  
  137.            
  138.  
  139.             $this->header.=$text;
  140.  
  141.         }
  142.  
  143.         else
  144.  
  145.         {
  146.  
  147.             if(!preg_match("/(.*)(<br>|<\/p>)/m",$text))
  148.  
  149.                 $text=$text."<br>";
  150.  
  151.            
  152.  
  153.             $this->header.=$text;
  154.  
  155.         }
  156.  
  157.     }
  158.  
  159.  
  160.  
  161.     public function file($file_name,$file_path)
  162.  
  163.     {
  164.  
  165.         if(isset($this->files[$file_name]))     return false;
  166.  
  167.        
  168.  
  169.         $this->files[$file_name]=$file_path;
  170.  
  171.        
  172.  
  173.     }
  174.  
  175.    
  176.  
  177.    
  178.  
  179.     public function send($empfaenger,$betreff)
  180.  
  181.     {
  182.  
  183.         $this->header .= "\n";
  184.  
  185.        
  186.  
  187.         foreach($this->files as $name => $path)
  188.  
  189.         {
  190.  
  191.        
  192.  
  193.             $Dateiinhalt = fread(fopen($path, "r"), filesize($path));
  194.  
  195.        
  196.  
  197.             $this->header .= "--".$this->trenner;
  198.  
  199.             $this->header .= "\n";
  200.  
  201.             $this->header .= "Content-Type: application/octet-stream; name=$name";
  202.  
  203.             $this->header .= "\n";
  204.  
  205.             $this->header .= "Content-Transfer-Encoding: base64";
  206.  
  207.             $this->header .= "\n";
  208.  
  209.             $this->header .= "Content-Disposition: attachment; filename=$name";
  210.  
  211.             $this->header .= "\n\n";
  212.  
  213.            
  214.  
  215.             $this->header .= chunk_split(base64_encode($Dateiinhalt));
  216.  
  217.             $this->header .= "\n";
  218.  
  219.         }
  220.  
  221.         $this->header .= "--".$this->trenner."--";
  222.  
  223.        
  224.  
  225.         if(isset($this->smtp_settings["host"]))
  226.  
  227.             $this->smtp_mail($empfaenger,$betreff);
  228.  
  229.         else
  230.  
  231.             mail ($empfaenger,$betreff,"",$this->header);
  232.  
  233.     }
  234.  
  235.  
  236.  
  237.     public function set_smtp($host,$username,$password)
  238.  
  239.     {
  240.  
  241.         $this->smtp_settings["host"]=$host;
  242.  
  243.         $this->smtp_settings["username"]=$username;
  244.  
  245.         $this->smtp_settings["password"]=$password;
  246.  
  247.     }
  248.  
  249.    
  250.  
  251.     function smtp_mail($to,$subject)
  252.  
  253.     {
  254.  
  255.         $smtp_server = $this->smtp_settings["host"];
  256.  
  257.         $port = 25;
  258.  
  259.         $mydomain = "Domain";
  260.  
  261.         $username = $this->smtp_settings["username"];
  262.  
  263.         $password = $this->smtp_settings["password"];
  264.  
  265.         $sender = $this->smtp_settings["from"];
  266.  
  267.         $recipient = $to;
  268.  
  269.         $subject = $subject;
  270.  
  271.         $content = $this->header;
  272.  
  273.  
  274.  
  275.         // Initiate connection with the SMTP server
  276.  
  277.         $handle = fsockopen($smtp_server,$port,$errno,$errstr);
  278.  
  279.        
  280.  
  281.         if(!$handle)
  282.  
  283.             echo "Konnte Verbindung nicht aufbauen: Fehler($errno): $errstr<br>\r\n";
  284.  
  285.        
  286.  
  287.         fputs($handle, "EHLO ".$mydomain."\n");
  288.  
  289.  
  290.  
  291.         // SMTP authorization
  292.  
  293.         fputs($handle, "AUTH LOGIN\n");
  294.  
  295.         fputs($handle, base64_encode($username)."\n");
  296.  
  297.         fputs($handle, base64_encode($password)."\n");
  298.  
  299.         // Send out the e-mail
  300.  
  301.         fputs($handle, "MAIL FROM: ".$sender."\n");
  302.  
  303.         fputs($handle, "RCPT TO: ".$recipient."\n");
  304.  
  305.         fputs($handle, "DATA\n");
  306.  
  307.         fputs($handle, "To: ".$recipient."\n");
  308.  
  309.         fputs($handle, "Subject: ".$subject ."\n");
  310.  
  311.         fputs($handle, $content."\n");
  312.  
  313.         fputs($handle, ".\n");
  314.  
  315.  
  316.  
  317.         // Close connection to SMTP server
  318.  
  319.         fputs($handle, "QUIT\n");
  320.  
  321.  
  322.  
  323.         $responce_codes=array(220,250,250,250,250,250,250,250,334,334,235,250,250,354,250,221);
  324.  
  325.  
  326.  
  327.         for($i=0;$i<count($responce_codes);$i++)
  328.  
  329.         {
  330.  
  331.                     $responce=fgets($handle);
  332.  
  333.                     if (substr($responce, 0, 3) != $responce_codes[$i])
  334.  
  335.                     {
  336.  
  337.                         if(substr($responce, 0, 3) != $responce_codes[($i+1)])
  338.  
  339.                             echo "Probleme beim Versand: $responce<br>\r\n";
  340.  
  341.                         else
  342.  
  343.                             $i++;
  344.  
  345.                     }
  346.  
  347.         }
  348.  
  349.     }
  350.  
  351.  
  352.  
  353. }
  354.  
  355.  
  356.  
  357. ?>
  358.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement