Wall posté le Mercredi 16 janvier 2008


...


  1. <?php
  2. /* ######################################################################################### */  
  3. /* DECODE DES SUJETS DE MAILS (AVEC DES ?ISO=.. */  
  4. /* ######################################################################################### */  
  5.  
  6.  
  7. function encodeHeader($input) { 
  8.    return mb_encode_mimeheader($input, "UTF-8", "B");  
  9. }  
  10.  
  11. function decodeHeader($header) { 
  12.    return mb_decode_mimeheader($header);  
  13. }  
  14.  
  15. function decodeSubject($string) { 
  16.  
  17.    $elements = imap_mime_header_decode($string); 
  18.  
  19.    $out = ''; 
  20.    for($i=0; $i<count($elements); $i++) { 
  21.       if ($elements[$i]->charset == 'default' || $elements[$i]->charset == 'UNKNOWN') { 
  22.          $out .= $elements[$i]->text; 
  23.       } 
  24.       else { 
  25.          $out .= iconv($elements[$i]->charset, 'UTF-8//TRANSLIT', $elements[$i]->text); 
  26.       } 
  27.    } 
  28.  
  29.    return $out;  
  30. }  
  31.  
  32. function decodeBodyMail ($body, $charset) { 
  33.    restore_error_handler(); 
  34.    error_reporting(0); 
  35.  
  36.    if ($charset == NULL) { 
  37.       if ($body != @iconv("ISO-8859-15", "UTF-8//TRANSLIT", $body)) $body = @iconv("ISO-8859-15", "UTF-8//IGNORE", $body); 
  38.       else $body = @iconv("ISO-8859-15", "UTF-8//TRANSLIT", $body); 
  39.    } 
  40.    else { 
  41.       if ($body != @iconv($charset, "UTF-8//TRANSLIT", $body)) $body = @iconv($charset, "UTF-8//IGNORE", $body); 
  42.       else $body = @iconv($charset, "UTF-8//TRANSLIT", $body); 
  43.  
  44.    } 
  45.  
  46.    $error_handler = set_error_handler("userErrorHandler"); 
  47.  
  48.    return $body;  
  49. }  
  50.  
  51. /* ######################################################################################### */  
  52. /* CLASSE QUI FORGE LES MAILS */  
  53. /* ######################################################################################### */  
  54.  
  55. function clean_input ($string) { 
  56.    $string = str_replace("\r", '', $string); 
  57.    $string = str_replace("\n", '', $string); 
  58.    $string = str_replace('"', '', $string); 
  59.    $string = str_replace("\x03", '', $string); 
  60.  
  61.    return $string;  
  62. }  
  63.  
  64. class send_mail { 
  65.  
  66.    var $headers = ''; 
  67.    var $importance = false; 
  68.    var $subject = null; 
  69.  
  70.    var $from = ''; 
  71.    var $Tfrom = array(); 
  72.    var $reply_to = ''; 
  73.    var $Treply_to = array(); 
  74.    var $to = ''; 
  75.    var $Tto = array(); 
  76.    var $cc = ''; 
  77.    var $Tcc = array(); 
  78.    var $cci = ''; 
  79.    var $Tcci = array(); 
  80.  
  81.    var $body = false; 
  82.    var $files = array(); 
  83.    var $size = 0; 
  84.    var $error = array(); 
  85.  
  86.    function send_mail () { 
  87.       return true; 
  88.    } 
  89.  
  90.    function addSubject ($sujet) { 
  91.       if (strlen($sujet)>255) { 
  92.          $this->error[] = 'Le sujet du mail fait plus que 255 caractères'; 
  93.          return false; 
  94.       } 
  95.  
  96.       $this->subject = $sujet; 
  97.  
  98.       return true; 
  99.    } 
  100.  
  101.    function addFrom ($from) { 
  102.       $from = clean_input($from); 
  103.       $temp_from = $from; 
  104.  
  105.       // on fait passer l'adresse email dans le test de la mort 
  106.       $from = internet_address_parse_string($from); 
  107.  
  108.       if (!$from || $from->diff > 0) { 
  109.          $this->error[] = 'L\'adresse du champ FROM est invalide'; 
  110.          return false; 
  111.       } 
  112.  
  113.       if (strlen($from->addrlist[0]['name']) == 0) { 
  114.          $this->from = $from->addrlist[0]['addr']; 
  115.       } 
  116.       else { 
  117.          $this->from = encodeHeader($from->addrlist[0]['name']).' <'.$from->addrlist[0]['addr'].'>'; 
  118.       } 
  119.  
  120.       $this->Tfrom[] = array('name' => str_replace('"', ' ', $from->addrlist[0]['name']), 'addr' => $from->addrlist[0]['addr']); 
  121.  
  122.       return true; 
  123.    } 
  124.  
  125.    function addReplyTo ($reply_to) { 
  126.       $reply_to = clean_input($reply_to); 
  127.       $temp_reply_to = $reply_to; 
  128.  
  129.       // on fait passer l'adresse email dans le test de la mort 
  130.       $reply_to = internet_address_parse_string($reply_to); 
  131.  
  132.       if (!$reply_to || $reply_to->diff > 0) { 
  133.          $this->error[] = 'L\'adresse du champ REPLYTO est invalide'; 
  134.          return false; 
  135.       } 
  136.  
  137.       if (strlen($reply_to->addrlist[0]['name']) == 0) { 
  138.          $this->reply_to = $reply_to->addrlist[0]['addr']; 
  139.       } 
  140.       else { 
  141.          $this->reply_to = encodeHeader($reply_to->addrlist[0]['name']).' <'.$reply_to->addrlist[0]['addr'].'>'; 
  142.       } 
  143.  
  144.       // on set un tableau qui va bien pour emdb 
  145.       $this->Treply_to[] = array('name' => $reply_to->addrlist[0]['name'], 'addr' => $reply_to->addrlist[0]['addr']); 
  146.       return true; 
  147.    } 
  148.  
  149.    function addTo ($to, $qui = 'to') { 
  150.       $to = clean_input($to); 
  151.       $temp_to = $to; 
  152.  
  153.       if (strlen($temp_to)>0) { 
  154.          $to = internet_address_parse_string($to); 
  155.  
  156.          switch ($qui) { 
  157.             case 'to': 
  158.                if (!$to || $to->diff > 0) { 
  159.                   $this->error[] = 'L\'adresse du champ TO est invalide'; 
  160.                   return false; 
  161.                } 
  162.                if (isset($to->addrlist) && is_array($to->addrlist)) { 
  163.                   foreach ($to->addrlist AS $email) { 
  164.                      if (strlen($email['name']) == 0) { 
  165.                         $this->to = $this->to.$email['addr'].', '; 
  166.                      } 
  167.                      else { 
  168.                         $this->to = $this->to.encodeHeader($email['name']).' <'.$email['addr'].'>, '; 
  169.                      } 
  170.  
  171.                      $this->Tto[] = array('name' => $email['name'], 'addr' => $email['addr']); 
  172.                   } 
  173.                   $this->to = substr($this->to, 0, -2); 
  174.                } 
  175.                break; 
  176.  
  177.             case 'cc': 
  178.                if (!$to || $to->diff > 0) { 
  179.                   $this->error[] = 'L\'adresse du champ CC est invalide'; 
  180.                   return false; 
  181.                } 
  182.                if (isset($to->addrlist) && is_array($to->addrlist)) { 
  183.                   foreach ($to->addrlist AS $email) { 
  184.                      if (strlen($email['name']) == 0) { 
  185.                         $this->cc = $this->cc.$email['addr'].', '; 
  186.                      } 
  187.                      else { 
  188.                         $this->cc = $this->cc.encodeHeader($email['name']).' <'.$email['addr'].'>, '; 
  189.                      } 
  190.  
  191.                      $this->Tcc[] = array('name' => $email['name'], 'addr' => $email['addr']); 
  192.                   } 
  193.                   $this->cc = substr($this->cc, 0, -2); 
  194.                } 
  195.                break; 
  196.  
  197.             case 'cci': 
  198.                if (!$to || $to->diff > 0) { 
  199.                   $this->error[] = 'L\'adresse du champ CCI est invalide'; 
  200.                   return false; 
  201.                } 
  202.                if (isset($to->addrlist) && is_array($to->addrlist)) { 
  203.                   foreach ($to->addrlist AS $email) { 
  204.                      if (strlen($email['name']) == 0) { 
  205.                         $this->cci = $this->cci.$email['addr'].', '; 
  206.                      } 
  207.                      else { 
  208.                         $this->cci = $this->cci.encodeHeader($email['name']).' <'.$email['addr'].'>, '; 
  209.                      } 
  210.  
  211.                      $this->Tcci[] = array('name' => $email['name'], 'addr' => $email['addr']); 
  212.                   } 
  213.                   $this->cci = substr($this->cci, 0, -2); 
  214.                } 
  215.                break; 
  216.          } 
  217.       } 
  218.  
  219.       return true; 
  220.    } 
  221.  
  222.    function addContent($content, $type) { 
  223.  
  224.          if ($type == 'text') { 
  225.             $this->text = $content; 
  226.          } 
  227.          elseif ($type == 'html') { 
  228.             $this->html = $content; 
  229.          } 
  230.          else { 
  231.             $this->error[] = 'Le contenu du mail n\'est ni du texte ni de l\'html'; 
  232.             return false; 
  233.          } 
  234.          return true; 
  235.  
  236.    } 
  237.  
  238.    function addFile ($nom, $size, $type, $data, $cid = '') { 
  239.       $tmp = (object) NULL; 
  240.       $tmp->nom = encodeHeader($nom); 
  241.       $tmp->size = $size; 
  242.       $tmp->type = $type; 
  243.       $tmp->file = $data; 
  244.       $tmp->cid = $cid; 
  245.  
  246.       if ($cid != '') $this->inline = true; 
  247.       else $this->attachments = true; 
  248.  
  249.       $this->files[] = $tmp; 
  250.    } 
  251.  
  252.    function boundary () { 
  253.       return '--------------'.md5(microtime()*mt_rand(0, 999)); 
  254.    } 
  255.  
  256.    function importance ($imp = 'Normal') { 
  257.       if (!( ($imp=='Normal') || ($imp=='High') || ($imp=='Low') )) $imp = 'Normal'; 
  258.       if ($imp=="Normal") $this->importance = "X-Priority: 3 (Normal)\nImportance: " . $imp; 
  259.       if ($imp=="High") $this->importance = "X-Priority: 1 (Highest)\nImportance: " . $imp; 
  260.       if ($imp=="Low") $this->importance = "X-Priority: 5 (Lowest)\nImportance: " . $imp; 
  261.  
  262.       return true; 
  263.    } 
  264.  
  265.    function headers () { 
  266.       $this->headers .= "User-Agent: Le PHP Facile Mailer\n"; 
  267.  
  268.       return true; 
  269.    } 
  270.  
  271.    function checkIntegrityMail() { 
  272.       if (count($this->error) == 0) { 
  273.          if (count($this->Tcci) == 0 && count($this->Tcc) == 0 && count($this->Tto) == 0) { 
  274.             $this->error[] = 'Le mail ne contient aucun destinataire'; 
  275.             return false; 
  276.          } 
  277.       } 
  278.       return true; 
  279.    } 
  280.  
  281.    function build_mail () { 
  282.       $ret = false; 
  283.  
  284.       $this->importance(); 
  285.       $this->headers(); 
  286.  
  287.       if ( ($this->importance != '') && (strlen($this->from)>0) ) { 
  288.  
  289.          // headers 
  290.          $this->body .= $this->headers; 
  291.          // priorité 
  292.          $this->body .= $this->importance . "\n"; 
  293.  
  294.          $this->body .= "Date: ".date('r', time())."\n"; 
  295.  
  296.          $this->body .= "From: " . $this->from . "\n"; 
  297.  
  298.          // on ajoute les To, Cc, Cci: 
  299.          if (strlen($this->to)>0) { 
  300.             $this->body .= "To: ".$this->to."\n"; 
  301.          } 
  302.  
  303.          if (strlen($this->cc)>0) { 
  304.             $this->body .= "Cc: ".$this->cc."\n"; 
  305.          } 
  306.  
  307.          if (strlen($this->cci)>0) { 
  308.             $this->body .= "Bcc: ".$this->cci."\n"; 
  309.          } 
  310.  
  311.          if (strlen($this->reply_to) != 0) { 
  312.             $this->body .= "Reply-To: " . $this->reply_to . "\n"; 
  313.          } 
  314.  
  315.       &nb