Questo sito utilizza cookies, anche di terze parti, per mostrare pubblicità e servizi in linea con il tuo account. Leggi l'informativa sui cookies.
Username: Password: oppure
PHP - sendMail.class.php
Forum - PHP - sendMail.class.php

Avatar
joker_ (Ex-Member)
Pro


Messaggi: 182
Iscritto: 14/02/2005

Segnala al moderatore
Postato alle 15:51
Domenica, 20/11/2005
Pensavo che nella cartella off-topic tutti si affollassero e affannassero per sapere cosa avevo fatto, ma non ? stato cosi. Per favore datemi consigli e suggerimenti su questa classe:

Codice sorgente - presumibilmente C#

  1. <?php
  2.  
  3.  /*
  4.   Classe creata da Santoro Diego.
  5.   Per aiuti nella programmazione in PHP, PERL, C e ECMAScript contattatemi
  6.   e-Mail vincenza.tralice@tiscali.it oppure santoro.diego@3000.it
  7.   La classe ? ancora in fase beta.
  8.  */
  9.  
  10.  final class sendMail {
  11.  
  12.   const eMailAddressErrorMessage="L' e-Mail indicata non rispetta un formato valido.";
  13.   const defaultSubject="This is the subject.";
  14.   const defaultTextMessage="This is text message.";
  15.   const defaultHtmlMessage="This is html message.";
  16.   const defaultHeaderMessage="This is a multi-part message in MIME format.";
  17.  
  18.   private static $messageProperties=array(
  19.         "charset" => array(
  20.                 "modifiable" => true,
  21.                 "values" => array(
  22.                         "iso-8859-1",
  23.                         "iso-8859-15",
  24.                         "utf-8",
  25.                         "utf-16"
  26.                 )
  27.         ),
  28.         "content-transfer-encoding" => array(
  29.                 "modifiable" => true,
  30.                 "values" => array(
  31.                         "7bit",
  32.                         "8bit",
  33.                         "quoted-printable"
  34.                 )
  35.         )
  36.   );
  37.  
  38.   private static $attachmentProperties=array(
  39.         "content-type" => array(
  40.                 "modifiable" => false,
  41.                  "values" => array(
  42.                         "application/octet-stream"
  43.                 )
  44.         ),
  45.         "content-transfer-encoding" => array(
  46.                 "modifiable" => false,
  47.                 "values" => array(
  48.                         "base64"
  49.                 )
  50.         ),
  51.         "content-disposition" => array(
  52.                 "modifiable" => true,
  53.                 "values" => array(
  54.                         "attachment",
  55.                         "inline"
  56.                 )
  57.         )
  58.   );
  59.  
  60.   private static $relatedProperties=array(
  61.         "content-transfer-encoding" => array(
  62.                 "modifiable" => false,
  63.                 "values" => array(
  64.                         "base64"
  65.                 )
  66.         )
  67.   );
  68.  
  69.   private $html;
  70.   private $text;
  71.  
  72.   private $related;
  73.   private $attachments;
  74.  
  75.   public static function valid_eMailAddress($eMailAddress) {
  76.    if(ereg("^[^@ ]+@[^@ ]+\.[^@ ]+$", $eMailAddress))
  77.         return true;
  78.    else
  79.         return false;
  80.   }
  81.  
  82.   public static function validContentId($contentId) {
  83.    if(ereg("^[a-zA-Z0-9]+$", $contentId))
  84.         return true;
  85.    else
  86.         return false;
  87.   }
  88.  
  89.   public static function validContentKey($contentKey) {
  90.    if(ereg("^[a-zA-Z0-9]+$", $contentKey))
  91.         return true;
  92.    else
  93.         return false;
  94.   }
  95.  
  96.   public static function mime_content_type($filename) {
  97.    $mime=array(
  98.         '.3dmf' => 'x-world/x-3dmf',
  99.         '.a' => 'application/octet-stream',
  100.         '.aab' => 'application/x-authorware-bin',
  101.         '.xwd' => 'image/x-xwd',
  102.         '.xyz' => 'chemical/x-pdb',
  103.         '.z' => 'application/x-compressed',
  104.         '.zip' => 'application/x-zip-compressed',
  105.         '.zoo' => 'application/octet-stream',
  106.         '.zsh' => 'text/x-script.zsh',
  107.         '.css' => 'text/css'
  108.    );
  109.    return $mime[strrchr($filename, '.')];
  110.   }
  111.  
  112.   private $from;
  113.   private $to;
  114.   private $subject;
  115.  
  116.   private $finalized;
  117.  
  118.   private $headerMessage;
  119.   private $bodyMessage;
  120.  
  121.   private $boundaries;
  122.  
  123.   public function __construct($from, $to, $subject=self::defaultSubject) {
  124.  
  125.    // set from
  126.    if(!self::valid_eMailAddress($from))
  127.         die(self::eMailAddressErrorMessage);
  128.    else
  129.         $this->from=$from;
  130.  
  131.    // set to
  132.    if(!self::valid_eMailAddress($to))
  133.         die(self::eMailAddressErrorMessage);
  134.    else
  135.         $this->to=$to;
  136.  
  137.    // set subject
  138.    $this->subject=$subject;
  139.  
  140.    // set text
  141.    $this->text=array(
  142.         "message" => self::defaultTextMessage,
  143.         "properties" => array(
  144.                 "charset" => self::$messageProperties["charset"]["values"][0],
  145.                 "content-transfer-encoding" => self::$messageProperties["content-transfer-encoding"]["values"][0]
  146.         )
  147.    );
  148.  
  149.    // set html
  150.    $this->html=array(
  151.         "message" => self::defaultHtmlMessage,
  152.         "properties" => array(
  153.                 "charset" => self::$messageProperties["charset"]["values"][0],
  154.                 "content-transfer-encoding" => self::$messageProperties["content-transfer-encoding"]["values"][1]
  155.         )
  156.    );
  157.  
  158.    // set related and attachments
  159.    $this->related=array();
  160.    $this->attachments=array();
  161.  
  162.    // set finalizater counter
  163.    $this->finalized=false;
  164.  
  165.    $this->headerMessage="";
  166.    $this->bodyMessage="";
  167.  
  168.    $this->boundaries=array(
  169.         "multipart/alternative" => md5(uniqid(microtime())),
  170.         "multipart/related" => md5(uniqid(microtime())),
  171.         "multipart/mixed" => md5(uniqid(microtime()))
  172.    );
  173.  
  174.   }
  175.  
  176.   public function setTo($to, &$errorString) {
  177.    if(self::valid_eMailAddress($to)) {
  178.     $this->to=$to;
  179.     return true;
  180.    } else {
  181.     $errorString=eMailAddressErrorMessage;
  182.     return false;
  183.    }
  184.   }
  185.  
  186.   public function setFrom($from, &$errorString) {
  187.    if(self::valid_eMailAddress($from)) {
  188.     $this->from=$from;
  189.     return true;
  190.    } else {
  191.     $errorString=eMailAddressErrorMessage;
  192.     return false;
  193.    }
  194.   }
  195.  
  196.   public function setSubject($subject=self::defaultSubject) {
  197.    $this->subject=$subject;
  198.   }
  199.  
  200.   public function setTextMessage($textMessage=self::defaultTextMessage) {
  201.    $this->text["message"]=$textMessage;
  202.   }
  203.  
  204.   public function setTextMessageProperty($key, $value, &$errorString) {
  205.  
  206.    $key=strtolower($key);
  207.    $value=strtolower($value);
  208.  
  209.    if(isset(self::$messageProperties[$key])) {
  210.     if(in_array($value, self::$messageProperties[$key]["values"])) {
  211.      if(self::$messageProperties[$key]["modifiable"]) {
  212.        $this->text["properties"][$key]=$value;
  213.        return true;
  214.      } else {
  215.       $errorString="Il valore della propriet? indicata non ? modificabile.";
  216.       return false;
  217.      }
  218.     } else {
  219.      $errorString="Il valore indicato per questa propriet? non ? valido.";
  220.      return false;
  221.     }
  222.    } else {
  223.     $errorString="Non esiste questa propriet? per i messaggi html.";
  224.     return false;
  225.    }
  226.   }
  227.  
  228.   public function setHtmlMessage($htmlMessage=self::defaultHtmlMessage) {
  229.    $this->html["message"]=$htmlMessage;
  230.   }
  231.  
  232.   public function setHtmlMessageProperty($key, $value, &$errorString) {
  233.  
  234.    $key=strtolower($key);
  235.    $value=strtolower($value);
  236.  
  237.    if(isset(self::$messageProperties[$key])) {
  238.     if(in_array($value, self::$messageProperties[$key]["values"])) {
  239.      if(self::$messageProperties[$key]["modifiable"]) {
  240.       $this->html["properties"][$key]=$value;
  241.       return true;
  242.      } else {
  243.       $errorString="Il valore della propriet? indicata non ? modificabile.";
  244.       return false;
  245.      }
  246.     } else {
  247.      $errorString="Il valore indicato per questa propriet? non ? valido.";
  248.      return false;
  249.     }
  250.    } else {
  251.     $errorString="Non esiste questa propriet? per i messaggi html.";
  252.     return false;
  253.    }
  254.   }
  255.  
  256.   public function addRelated($fileName, $relatedKey, $contentId, &$errorString) {
  257.    if(is_file($fileName)) {
  258.     if($fileHandle=fopen($fileName, "r")) {
  259.      if(self::validContentId($contentId)) {
  260.       if(!isset($this->related[$relatedKey])) {
  261.        if(self::validContentKey($relatedKey)) {
  262.         $this->related[$relatedKey]=array(
  263.         "fileName" => basename($fileName),
  264.         "properties" => array(
  265.                 "content-type" => self::mime_content_type($fileName),
  266.                 "content-transfer-encoding" => self::$relatedProperties["content-transfer-encoding"]["values"][0],
  267.                 "content-id" => $contentId
  268.         ),
  269.         "source" => base64_encode(
  270.                 fread($fileHandle, filesize($fileName))
  271.         )
  272.         );
  273.         return true;
  274.        } else {
  275.         $errorString="L' id specificato non ? valido.";
  276.         return false;
  277.        }
  278.       } else {
  279.        $errorString="La chiave specificata ? gi? associata ad un altro related.";
  280.        return false;
  281.       }
  282.      } else {
  283.       $errorString="La chiave specificata per il related non ? valida.";
  284.       return false;
  285.      }
  286.     } else {
  287.      $errorString="Non ? possibile aprire il file indicato.";
  288.      return false;
  289.     }
  290.    } else {
  291.     $errorString="Il nome del file indicato non ? valido.";
  292.     return false;
  293.    }
  294.   }
  295.  
  296.   public function setRelatedProperty($relatedKey, $key, $value, &$errorString) {
  297.  
  298.    $key=strtolower($key);
  299.    $value=strtolower($value);
  300.    
  301.    if(isset(self::$relatedProperties[$key])) {
  302.     if(in_array($value, self::$relatedProperties[$key]["values"])) {
  303.      if(self::$relatedProperties[$key]["modifiable"]) {
  304.       if(isset($this->related[$relatedKey])) {
  305.        $this->related[$relatedKey]["properties"][$key]=$value;
  306.        return true;
  307.       } else {
  308.        $errorString="Il related indicato non esiste.";
  309.        return false;
  310.       }
  311.      } else {
  312.       $errorString="Il valore della propriet? indicata non ? modificabile.";
  313.       return false;
  314.      }
  315.     } else {
  316.      $errorString="Il valore indicato per questa propriet? non ? valido.";
  317.      return false;
  318.     }
  319.    } else {
  320.     $errorString="Non esiste questa propriet? per i related.";
  321.     return false;
  322.    }
  323.   }
  324.  
  325.   public function addAttachment($fileName, $attachmentKey, &$errorString) {
  326.    if(is_file($fileName)) {
  327.     if($fileHandle=fopen($fileName, "r")) {
  328.      if(self::validContentKey($attachmentKey)) {
  329.       if(!isset($this->attachments[$attachmentKey])) {
  330.        $this->attachments[$attachmentKey]=array(
  331.         "fileName" => basename($fileName),
  332.         "properties" => array(
  333.                 "content-type" => self::$attachmentProperties["content-type"]["values"][0],
  334.                 "content-disposition" => self::$attachmentProperties["content-disposition"]["values"][0],
  335.                 "content-transfer-encoding" => self::$attachmentProperties["content-transfer-encoding"]["values"][0]
  336.         ),
  337.         "source" => base64_encode(
  338.                 fread($fileHandle, filesize($fileName))
  339.         )
  340.        );
  341.        return true;
  342.       } else {
  343.        $errorString="La chiave specificata ? gi? associata ad un altro allegato.";
  344.        return false;
  345.       }
  346.      } else {
  347.       $errorString="La chiave specificata per l'allegato non ? valida.";
  348.       return false;
  349.      }
  350.     } else {
  351.      $errorString="Non ? possibile aprire il file indicato.";
  352.      return false;
  353.     }
  354.    } else {
  355.     $errorString="Il nome del file indicato non ? valido.";
  356.     return false;
  357.    }
  358.   }
  359.  
  360.   public function setAttachmentProperty($attachmentKey, $key, $value, &$errorString) {
  361.  
  362.    $key=strtolower($key);
  363.    $value=strtolower($value);
  364.  
  365.    if(isset(self::$attachmentProperties[$key])) {
  366.     if(in_array($value, self::$attachmentProperties[$key]["values"])) {
  367.      if(self::$attachmentProperties[$key]["modifiable"]) {
  368.       if(isset($this->attachments[$attachmentKey])) {
  369.        $this->attachments[$attachmentKey]["properties"][$key]=$value;
  370.        return true;
  371.       } else {
  372.        $errorString="L'allegato indicato non esiste.";
  373.        return false;
  374.       }
  375.      } else {
  376.       $errorString="Il valore della propriet? indicata non ? modificabile.";
  377.       return false;
  378.      }
  379.     } else {
  380.      $errorString="Il valore indicato per questa propriet? non ? valido.";
  381.      return false;
  382.     }
  383.    } else {
  384.     $errorString="Non esiste questa propriet? per gli allegati.";
  385.     return false;
  386.    }
  387.   }
  388.  
  389.   public function finalize(&$errorString) {
  390.    if(!$this->finalized) {
  391.     $this->headerMessage="from: ".($this->from)."\n";
  392.     $this->headerMessage.="to: ".($this->to)."\n";
  393.     $this->headerMessage.="subject: ".($this->subject)."\n";
  394.     $this->headerMessage.="mime-version: 1.0\n";
  395.  
  396.     if(($countAttachments=count($this->attachments))>0) {
  397.      $this->headerMessage.="content-type: multipart/mixed; boundary=\"".($this->boundaries["multipart/mixed"])."\"\n\n";
  398.      $this->headerMessage.=self::defaultHeaderMessage;
  399.      $this->headerMessage.="\n\n";
  400.  
  401.      $this->bodyMessage="--".($this->boundaries["multipart/mixed"])."\n";
  402.  
  403.      if(($countRelated=count($this->related))>0) {
  404.       $this->bodyMessage.="content-type: multipart/related; type=\"multipart/alternative\"; boundary=\"".($this->boundaries["multipart/related"])."\"\n\n";
  405.  
  406.       $this->bodyMessage.="--".($this->boundaries["multipart/related"])."\n";
  407.  
  408.       $this->bodyMessage.="content-type: multipart/alternative; boundary=\"".($this->boundaries["multipart/alternative"])."\"\n\n";
  409.       $this->createMultipartAlternativeMessage($this->boundaries["multipart/alternative"]);
  410.       $this->bodyMessage.="--".($this->boundaries["multipart/alternative"])."--\n\n";
  411.  
  412.       // aggiungere i related e chiudere
  413.  
  414.       $relatedCounter=0;
  415.       while(list($key,)=each($this->related)) {
  416.        $relatedCounter++;
  417.        
  418.        $this->bodyMessage.="--".$this->boundaries["multipart/related"]."\n";
  419.        $this->createMultipartRelatedMessage($key);
  420.        if($relatedCounter!=$countRelated) $this->bodyMessage.="--".($this->boundaries["multipart/related"])."\n";
  421.        else $this->bodyMessage.="--".($this->boundaries["multipart/related"])."--\n\n";
  422.       }
  423.      } else {
  424.       $this->bodyMessage.="content-type: multipart/alternative; boundary=\"".($this->boundaries["multipart/alternative"])."\"\n\n";
  425.       $this->createMultipartAlternativeMessage();
  426.       $this->bodyMessage.="--".($this->boundaries["multipart/alternative"])."--\n\n";
  427.      }
  428.  
  429.      $attachmentsCounter=0;
  430.      while(list($key,)=each($this->attachments)) {
  431.       $attachmentsCounter++;
  432.       $this->bodyMessage.="--".($this->boundaries["multipart/mixed"])."\n";
  433.       $this->createMultipartMixedMessage($key);
  434.       if($attachmentsCounter!=$countAttachments) $this->bodyMessage.="--".($this->boundaries["multipart/mixed"])."\n";
  435.       else $this->bodyMessage.="--".($this->boundaries["multipart/mixed"])."--\n\n";
  436.      }
  437.     } else {
  438.      if(($countRelated=count($this->related))>0) {
  439.       $this->headerMessage.="content-type: multipart/related; type=\"multipart/alternative\"; boundary=\"".($this->boundaries["multipart/related"])."\"\n\n";
  440.       $this->headerMessage.=self::defaultHeaderMessage;
  441.       $this->headerMessage.="\n\n";
  442.  
  443.       $this->bodyMessage="--".($this->boundaries["multipart/related"])."\n";
  444.       $this->bodyMessage.="content-type: multipart/alternative; boundary=\"".($this->boundaries["multipart/alternative"])."\"\n\n";
  445.       $this->createMultipartAlternativeMessage();
  446.       $this->bodyMessage.="--".($this->boundaries["multipart/alternative"])."--\n\n";
  447.  
  448.       $relatedCounter=0;
  449.       while(list($key,)=each($this->related)) {
  450.        $relatedCounter++;
  451.        $this->bodyMessage.="--".$this->boundaries["multipart/related"]."\n";
  452.        $this->createMultipartRelatedMessage($key);
  453.        if($relatedCounter!=$countRelated) $this->bodyMessage.="--".($this->boundaries["multipart/related"])."\n";
  454.        else $this->bodyMessage.="--".($this->boundaries["multipart/related"])."--\n\n";
  455.       }
  456.      } else {
  457.       $this->headerMessage.="content-type: multipart/alternative; boundary=\"".($this->boundaries["multipart/alternative"])."\"\n\n";
  458.       $this->headerMessage.=self::defaultHeaderMessage;
  459.       $this->headerMessage.="\n\n";
  460.  
  461.       $this->createMultipartAlternativeMessage();
  462.       $this->bodyMessage.="--".($this->boundaries["multipart/alternative"])."--";
  463.  
  464.      }
  465.     }
  466.     $this->finalized=true;
  467.     return true;
  468.    } else {
  469.     $errorString="Al momento non ? possibile finalizzare.";
  470.     return false;
  471.    }
  472.   }
  473.  
  474.   private function createMultipartAlternativeMessage() {
  475.    $multipartAlternativeBoundary=$this->boundaries["multipart/alternative"];
  476.    $this->bodyMessage.="--$multipartAlternativeBoundary\n";
  477.    $this->bodyMessage.="content-type: text/plain; charset=\"".($this->text["properties"]["charset"])."\"\n";
  478.    $this->bodyMessage.="content-transfer-encoding: ".($this->text["properties"]["content-transfer-encoding"])."\n\n";
  479.    $this->bodyMessage.=$this->text["message"];
  480.    $this->bodyMessage.="\n\n";
  481.    $this->bodyMessage.="--$multipartAlternativeBoundary\n";
  482.    $this->bodyMessage.="content-type: text/html; charset=\"".($this->html["properties"]["charset"])."\"\n";
  483.    $this->bodyMessage.="content-transfer-encoding: ".($this->html["properties"]["content-transfer-encoding"])."\n\n";
  484.    $this->bodyMessage.=$this->html["message"];
  485.    $this->bodyMessage.="\n\n";
  486.   }
  487.  
  488.   private function createMultipartRelatedMessage($key) {
  489.    $obj=$this->related[$key];
  490.    $this->bodyMessage.="content-type: ".($obj["properties"]["content-type"])."; name=\"".($obj["fileName"])."\"\n";
  491.    $this->bodyMessage.="content-transfer-encoding: ".($obj["properties"]["content-transfer-encoding"])."\n";
  492.    $this->bodyMessage.="content-id: <".($obj["properties"]["content-id"]).">\n\n";
  493.    $this->bodyMessage.=$obj["source"];
  494.    $this->bodyMessage.="\n\n";
  495.   }
  496.  
  497.   private function createMultipartMixedMessage($key) {
  498.    $obj=$this->attachments[$key];
  499.    $this->bodyMessage.="content-type: ".($obj["properties"]["content-type"])."; name=\"".($obj["fileName"])."\"\n";
  500.    $this->bodyMessage.="content-transfer-encoding: ".($obj["properties"]["content-transfer-encoding"])."\n";
  501.    $this->bodyMessage.="content-disposition: ".($obj["properties"]["content-disposition"])."; filename=\"".($obj["fileName"])."\"\n\n";
  502.    $this->bodyMessage.=$obj["source"];
  503.    $this->bodyMessage.="\n\n";
  504.   }
  505.  
  506.   public function getSource(&$errorString) {
  507.    if($this->finalized) {
  508.     return ($this->headerMessage).($this->bodyMessage);
  509.    } else {
  510.     $errorString="Ancora non ? avvenuta la finalizzazione.";
  511.     return false;
  512.    }
  513.   }
  514.  
  515.   public function sendMail(&$errorString) {
  516.    if($this->finalized) {
  517.     mail($this->to, $this->subject, $this->bodyMessage, $this->headerMessage);
  518.     $this->finalized=false;
  519.     return true;
  520.    } else {
  521.     $errorString="Ancora non ? avvenuta la finalizzazione.";
  522.     return false;
  523.    }
  524.   }
  525.  }
  526.  
  527. ?>


PM Quote
Avatar
andriy88 (Ex-Member)
Pro


Messaggi: 118
Iscritto: 04/11/2005

Segnala al moderatore
Postato alle 17:47
Domenica, 20/11/2005
ciao joker_.. Complimenti per la classe.
Non ho avuto ancora tempo per esaminare tutto il codice ma ho dato uno sguardo e l'ho provato, sembra che funziona...

:k::k::k::k:


---
PM Quote
Avatar
joker_ (Ex-Member)
Pro


Messaggi: 182
Iscritto: 14/02/2005

Segnala al moderatore
Postato alle 19:08
Domenica, 20/11/2005
Nuovo codice, con alcuni miglioramenti

Codice sorgente - presumibilmente C#

  1. <?php
  2.  
  3.  /*
  4.   Classe creata da Santoro Diego.
  5.   Per aiuti nella programmazione in PHP, PERL, C e ECMAScript contattatemi
  6.   e-Mail vincenza.tralice@tiscali.it oppure santoro.diego@3000.it
  7.   La classe ? ancora in fase beta.
  8.  */
  9.  
  10.  final class sendMail {
  11.  
  12.   const eMailAddressErrorMessage="L' e-Mail indicata non rispetta un formato valido.";
  13.   const defaultSubject="This is the subject.";
  14.   const defaultTextMessage="This is text message.";
  15.   const defaultHtmlMessage="This is html message.";
  16.   const defaultHeaderMessage="This is a multi-part message in MIME format.";
  17.  
  18.   private static $messageProperties=array(
  19.         "charset" => array(
  20.                 "modifiable" => true,
  21.                 "values" => array(
  22.                         "iso-8859-1",
  23.                         "iso-8859-15",
  24.                         "utf-8",
  25.                         "utf-16"
  26.                 )
  27.         ),
  28.         "content-transfer-encoding" => array(
  29.                 "modifiable" => true,
  30.                 "values" => array(
  31.                         "7bit",
  32.                         "8bit",
  33.                         "quoted-printable"
  34.                 )
  35.         )
  36.   );
  37.  
  38.   private static $attachmentProperties=array(
  39.         "content-type" => array(
  40.                 "modifiable" => false,
  41.                  "values" => array(
  42.                         "application/octet-stream"
  43.                 )
  44.         ),
  45.         "content-transfer-encoding" => array(
  46.                 "modifiable" => false,
  47.                 "values" => array(
  48.                         "base64"
  49.                 )
  50.         ),
  51.         "content-disposition" => array(
  52.                 "modifiable" => true,
  53.                 "values" => array(
  54.                         "attachment",
  55.                         "inline"
  56.                 )
  57.         )
  58.   );
  59.  
  60.   private static $relatedProperties=array(
  61.         "content-transfer-encoding" => array(
  62.                 "modifiable" => false,
  63.                 "values" => array(
  64.                         "base64"
  65.                 )
  66.         )
  67.   );
  68.  
  69.   private $html;
  70.   private $text;
  71.  
  72.   private $related;
  73.   private $attachments;
  74.  
  75.   public static function valid_eMailAddress($eMailAddress) {
  76.    if(ereg("^[^@ ]+@[^@ ]+\.[^@ ]+$", $eMailAddress))
  77.         return true;
  78.    else
  79.         return false;
  80.   }
  81.  
  82.   public static function validContentId($contentId) {
  83.    if(ereg("^[a-zA-Z0-9]+$", $contentId))
  84.         return true;
  85.    else
  86.         return false;
  87.   }
  88.  
  89.   public static function validContentKey($contentKey) {
  90.    if(ereg("^[a-zA-Z0-9]+$", $contentKey))
  91.         return true;
  92.    else
  93.         return false;
  94.   }
  95.  
  96.   public static function mime_content_type($filename) {
  97.    $mime=array(
  98.         '.3dmf' => 'x-world/x-3dmf',
  99.         '.a' => 'application/octet-stream',
  100.         '.aab' => 'application/x-authorware-bin',
  101.         '.xwd' => 'image/x-xwd',
  102.         '.xyz' => 'chemical/x-pdb',
  103.         '.z' => 'application/x-compressed',
  104.         '.zip' => 'application/x-zip-compressed',
  105.         '.zoo' => 'application/octet-stream',
  106.         '.zsh' => 'text/x-script.zsh',
  107.         '.css' => 'text/css'
  108.    );
  109.    return $mime[strrchr($filename, '.')];
  110.   }
  111.  
  112.   public static function sendSimpleMail($from, $to, $subject=defaultSubject, $message=defaultTextMessage) {
  113.  
  114.    if(!self::valid_eMailAddress($from))
  115.         die(self::eMailAddressErrorMessage);
  116.  
  117.    if(!self::valid_eMailAddress($to))
  118.         die(self::eMailAddressErrorMessage);
  119.  
  120.    $headerMessage="from: $from\n";
  121.    $headerMessage.="to: $to\n";
  122.    $headerMessage.="subject: $subject\n";
  123.    $headerMessage.="content-type: text/plain; charset=\"iso-8859-1\"\n";
  124.    $headerMessage.="content-transfer-encoding: 7bit\n\n";
  125.    $bodyMessage=$message;
  126.    mail($to, $subject, $bodyMessage, $headerMessage);
  127.    return true;
  128.   }
  129.  
  130.   private $from;
  131.   private $to;
  132.   private $subject;
  133.  
  134.   private $finalized;
  135.  
  136.   private $headerMessage;
  137.   private $bodyMessage;
  138.  
  139.   private $boundaries;
  140.  
  141.   public function __construct($from, $to, $subject=self::defaultSubject) {
  142.  
  143.    // set from
  144.    if(!self::valid_eMailAddress($from))
  145.         die(self::eMailAddressErrorMessage);
  146.    else
  147.         $this->from=$from;
  148.  
  149.    // set to
  150.    if(!self::valid_eMailAddress($to))
  151.         die(self::eMailAddressErrorMessage);
  152.    else
  153.         $this->to=$to;
  154.  
  155.    // set subject
  156.    $this->subject=$subject;
  157.  
  158.    // set text
  159.    $this->text=array(
  160.         "message" => self::defaultTextMessage,
  161.         "properties" => array(
  162.                 "charset" => self::$messageProperties["charset"]["values"][0],
  163.                 "content-transfer-encoding" => self::$messageProperties["content-transfer-encoding"]["values"][0]
  164.         )
  165.    );
  166.  
  167.    // set html
  168.    $this->html=array(
  169.         "message" => self::defaultHtmlMessage,
  170.         "properties" => array(
  171.                 "charset" => self::$messageProperties["charset"]["values"][0],
  172.                 "content-transfer-encoding" => self::$messageProperties["content-transfer-encoding"]["values"][1]
  173.         )
  174.    );
  175.  
  176.    // set related and attachments
  177.    $this->related=array();
  178.    $this->attachments=array();
  179.  
  180.    // set finalizater counter
  181.    $this->finalized=false;
  182.  
  183.    $this->headerMessage="";
  184.    $this->bodyMessage="";
  185.  
  186.    $this->boundaries=array(
  187.         "multipart/alternative" => md5(uniqid(microtime())),
  188.         "multipart/related" => md5(uniqid(microtime())),
  189.         "multipart/mixed" => md5(uniqid(microtime()))
  190.    );
  191.  
  192.   }
  193.  
  194.   public function setTo($to, &$errorString) {
  195.    if(self::valid_eMailAddress($to)) {
  196.     $this->to=$to;
  197.     if($this->finalized) $this->reFinalize();
  198.     return true;
  199.    } else {
  200.     $errorString=eMailAddressErrorMessage;
  201.     return false;
  202.    }
  203.   }
  204.  
  205.   public function setFrom($from, &$errorString) {
  206.    if(self::valid_eMailAddress($from)) {
  207.     $this->from=$from;
  208.     if($this->finalized) $this->reFinalize();
  209.     return true;
  210.    } else {
  211.     $errorString=eMailAddressErrorMessage;
  212.     return false;
  213.    }
  214.   }
  215.  
  216.   public function setSubject($subject=self::defaultSubject) {
  217.    $this->subject=$subject;
  218.    if($this->finalized) $this->reFinalize();
  219.   }
  220.  
  221.   public function setTextMessage($textMessage=self::defaultTextMessage) {
  222.    $this->text["message"]=$textMessage;
  223.    if($this->finalized) $this->reFinalize();
  224.   }
  225.  
  226.   public function setTextMessageProperty($key, $value, &$errorString) {
  227.  
  228.    $key=strtolower($key);
  229.    $value=strtolower($value);
  230.  
  231.    if(isset(self::$messageProperties[$key])) {
  232.     if(in_array($value, self::$messageProperties[$key]["values"])) {
  233.      if(self::$messageProperties[$key]["modifiable"]) {
  234.        $this->text["properties"][$key]=$value;
  235.        if($this->finalized) $this->reFinalize();
  236.        return true;
  237.      } else {
  238.       $errorString="Il valore della propriet? indicata non ? modificabile.";
  239.       return false;
  240.      }
  241.     } else {
  242.      $errorString="Il valore indicato per questa propriet? non ? valido.";
  243.      return false;
  244.     }
  245.    } else {
  246.     $errorString="Non esiste questa propriet? per i messaggi html.";
  247.     return false;
  248.    }
  249.   }
  250.  
  251.   public function setHtmlMessage($htmlMessage=self::defaultHtmlMessage) {
  252.    $this->html["message"]=$htmlMessage;
  253.    if($this->finalized) $this->reFinalized();
  254.   }
  255.  
  256.   public function setHtmlMessageProperty($key, $value, &$errorString) {
  257.  
  258.    $key=strtolower($key);
  259.    $value=strtolower($value);
  260.  
  261.    if(isset(self::$messageProperties[$key])) {
  262.     if(in_array($value, self::$messageProperties[$key]["values"])) {
  263.      if(self::$messageProperties[$key]["modifiable"]) {
  264.       $this->html["properties"][$key]=$value;
  265.       if($this->finalized) $this->reFinalized();
  266.       return true;
  267.      } else {
  268.       $errorString="Il valore della propriet? indicata non ? modificabile.";
  269.       return false;
  270.      }
  271.     } else {
  272.      $errorString="Il valore indicato per questa propriet? non ? valido.";
  273.      return false;
  274.     }
  275.    } else {
  276.     $errorString="Non esiste questa propriet? per i messaggi html.";
  277.     return false;
  278.    }
  279.   }
  280.  
  281.   public function addRelated($fileName, $relatedKey, $contentId, &$errorString) {
  282.    if(is_file($fileName)) {
  283.     if($fileHandle=fopen($fileName, "r")) {
  284.      if(self::validContentId($contentId)) {
  285.       if(!isset($this->related[$relatedKey])) {
  286.        if(self::validContentKey($relatedKey)) {
  287.         $this->related[$relatedKey]=array(
  288.         "fileName" => basename($fileName),
  289.         "properties" => array(
  290.                 "content-type" => self::mime_content_type($fileName),
  291.                 "content-transfer-encoding" => self::$relatedProperties["content-transfer-encoding"]["values"][0],
  292.                 "content-id" => $contentId
  293.         ),
  294.         "source" => base64_encode(
  295.                 fread($fileHandle, filesize($fileName))
  296.         )
  297.         );
  298.         if($this->finalized) $this->reFinalize();
  299.         return true;
  300.        } else {
  301.         $errorString="L' id specificato non ? valido.";
  302.         return false;
  303.        }
  304.       } else {
  305.        $errorString="La chiave specificata ? gi? associata ad un altro related.";
  306.        return false;
  307.       }
  308.      } else {
  309.       $errorString="La chiave specificata per il related non ? valida.";
  310.       return false;
  311.      }
  312.     } else {
  313.      $errorString="Non ? possibile aprire il file indicato.";
  314.      return false;
  315.     }
  316.    } else {
  317.     $errorString="Il nome del file indicato non ? valido.";
  318.     return false;
  319.    }
  320.   }
  321.  
  322.   public function setRelatedProperty($relatedKey, $key, $value, &$errorString) {
  323.  
  324.    $key=strtolower($key);
  325.    $value=strtolower($value);
  326.    
  327.    if(isset(self::$relatedProperties[$key])) {
  328.     if(in_array($value, self::$relatedProperties[$key]["values"])) {
  329.      if(self::$relatedProperties[$key]["modifiable"]) {
  330.       if(isset($this->related[$relatedKey])) {
  331.        $this->related[$relatedKey]["properties"][$key]=$value;
  332.        if($this->finalized) $this->reFinalize();
  333.        return true;
  334.       } else {
  335.        $errorString="Il related indicato non esiste.";
  336.        return false;
  337.       }
  338.      } else {
  339.       $errorString="Il valore della propriet? indicata non ? modificabile.";
  340.       return false;
  341.      }
  342.     } else {
  343.      $errorString="Il valore indicato per questa propriet? non ? valido.";
  344.      return false;
  345.     }
  346.    } else {
  347.     $errorString="Non esiste questa propriet? per i related.";
  348.     return false;
  349.    }
  350.   }
  351.  
  352.   public function addAttachment($fileName, $attachmentKey, &$errorString) {
  353.    if(is_file($fileName)) {
  354.     if($fileHandle=fopen($fileName, "r")) {
  355.      if(self::validContentKey($attachmentKey)) {
  356.       if(!isset($this->attachments[$attachmentKey])) {
  357.        $this->attachments[$attachmentKey]=array(
  358.         "fileName" => basename($fileName),
  359.         "properties" => array(
  360.                 "content-type" => self::$attachmentProperties["content-type"]["values"][0],
  361.                 "content-disposition" => self::$attachmentProperties["content-disposition"]["values"][0],
  362.                 "content-transfer-encoding" => self::$attachmentProperties["content-transfer-encoding"]["values"][0]
  363.         ),
  364.         "source" => base64_encode(
  365.                 fread($fileHandle, filesize($fileName))
  366.         )
  367.        );
  368.        if($this->finalized) $this->reFinalize();
  369.        return true;
  370.       } else {
  371.        $errorString="La chiave specificata ? gi? associata ad un altro allegato.";
  372.        return false;
  373.       }
  374.      } else {
  375.       $errorString="La chiave specificata per l'allegato non ? valida.";
  376.       return false;
  377.      }
  378.     } else {
  379.      $errorString="Non ? possibile aprire il file indicato.";
  380.      return false;
  381.     }
  382.    } else {
  383.     $errorString="Il nome del file indicato non ? valido.";
  384.     return false;
  385.    }
  386.   }
  387.  
  388.   public function setAttachmentProperty($attachmentKey, $key, $value, &$errorString) {
  389.  
  390.    $key=strtolower($key);
  391.    $value=strtolower($value);
  392.  
  393.    if(isset(self::$attachmentProperties[$key])) {
  394.     if(in_array($value, self::$attachmentProperties[$key]["values"])) {
  395.      if(self::$attachmentProperties[$key]["modifiable"]) {
  396.       if(isset($this->attachments[$attachmentKey])) {
  397.        $this->attachments[$attachmentKey]["properties"][$key]=$value;
  398.        if($this->finalized) $this->reFinalize();
  399.        return true;
  400.       } else {
  401.        $errorString="L'allegato indicato non esiste.";
  402.        return false;
  403.       }
  404.      } else {
  405.       $errorString="Il valore della propriet? indicata non ? modificabile.";
  406.       return false;
  407.      }
  408.     } else {
  409.      $errorString="Il valore indicato per questa propriet? non ? valido.";
  410.      return false;
  411.     }
  412.    } else {
  413.     $errorString="Non esiste questa propriet? per gli allegati.";
  414.     return false;
  415.    }
  416.   }
  417.  
  418.   public function finalize(&$errorString) {
  419.    if(!$this->finalized) {
  420.     $this->headerMessage="from: ".($this->from)."\n";
  421.     $this->headerMessage.="to: ".($this->to)."\n";
  422.     $this->headerMessage.="subject: ".($this->subject)."\n";
  423.     $this->headerMessage.="mime-version: 1.0\n";
  424.  
  425.     if(($countAttachments=count($this->attachments))>0) {
  426.      $this->headerMessage.="content-type: multipart/mixed; boundary=\"".($this->boundaries["multipart/mixed"])."\"\n\n";
  427.      $this->headerMessage.=self::defaultHeaderMessage;
  428.      $this->headerMessage.="\n\n";
  429.  
  430.      $this->bodyMessage="--".($this->boundaries["multipart/mixed"])."\n";
  431.  
  432.      if(($countRelated=count($this->related))>0) {
  433.       $this->bodyMessage.="content-type: multipart/related; type=\"multipart/alternative\"; boundary=\"".($this->boundaries["multipart/related"])."\"\n\n";
  434.  
  435.       $this->bodyMessage.="--".($this->boundaries["multipart/related"])."\n";
  436.  
  437.       $this->bodyMessage.="content-type: multipart/alternative; boundary=\"".($this->boundaries["multipart/alternative"])."\"\n\n";
  438.       $this->createMultipartAlternativeMessage($this->boundaries["multipart/alternative"]);
  439.       $this->bodyMessage.="--".($this->boundaries["multipart/alternative"])."--\n\n";
  440.  
  441.       // aggiungere i related e chiudere
  442.  
  443.       $relatedCounter=0;
  444.       while(list($key,)=each($this->related)) {
  445.        $relatedCounter++;
  446.        
  447.        $this->bodyMessage.="--".$this->boundaries["multipart/related"]."\n";
  448.        $this->createMultipartRelatedMessage($key);
  449.        if($relatedCounter!=$countRelated) $this->bodyMessage.="--".($this->boundaries["multipart/related"])."\n";
  450.        else $this->bodyMessage.="--".($this->boundaries["multipart/related"])."--\n\n";
  451.       }
  452.      } else {
  453.       $this->bodyMessage.="content-type: multipart/alternative; boundary=\"".($this->boundaries["multipart/alternative"])."\"\n\n";
  454.       $this->createMultipartAlternativeMessage();
  455.       $this->bodyMessage.="--".($this->boundaries["multipart/alternative"])."--\n\n";
  456.      }
  457.  
  458.      $attachmentsCounter=0;
  459.      while(list($key,)=each($this->attachments)) {
  460.       $attachmentsCounter++;
  461.       $this->bodyMessage.="--".($this->boundaries["multipart/mixed"])."\n";
  462.       $this->createMultipartMixedMessage($key);
  463.       if($attachmentsCounter!=$countAttachments) $this->bodyMessage.="--".($this->boundaries["multipart/mixed"])."\n";
  464.       else $this->bodyMessage.="--".($this->boundaries["multipart/mixed"])."--\n\n";
  465.      }
  466.     } else {
  467.      if(($countRelated=count($this->related))>0) {
  468.       $this->headerMessage.="content-type: multipart/related; type=\"multipart/alternative\"; boundary=\"".($this->boundaries["multipart/related"])."\"\n\n";
  469.       $this->headerMessage.=self::defaultHeaderMessage;
  470.       $this->headerMessage.="\n\n";
  471.  
  472.       $this->bodyMessage="--".($this->boundaries["multipart/related"])."\n";
  473.       $this->bodyMessage.="content-type: multipart/alternative; boundary=\"".($this->boundaries["multipart/alternative"])."\"\n\n";
  474.       $this->createMultipartAlternativeMessage();
  475.       $this->bodyMessage.="--".($this->boundaries["multipart/alternative"])."--\n\n";
  476.  
  477.       $relatedCounter=0;
  478.       while(list($key,)=each($this->related)) {
  479.        $relatedCounter++;
  480.        $this->bodyMessage.="--".$this->boundaries["multipart/related"]."\n";
  481.        $this->createMultipartRelatedMessage($key);
  482.        if($relatedCounter!=$countRelated) $this->bodyMessage.="--".($this->boundaries["multipart/related"])."\n";
  483.        else $this->bodyMessage.="--".($this->boundaries["multipart/related"])."--\n\n";
  484.       }
  485.      } else {
  486.       $this->headerMessage.="content-type: multipart/alternative; boundary=\"".($this->boundaries["multipart/alternative"])."\"\n\n";
  487.       $this->headerMessage.=self::defaultHeaderMessage;
  488.       $this->headerMessage.="\n\n";
  489.  
  490.       $this->createMultipartAlternativeMessage();
  491.       $this->bodyMessage.="--".($this->boundaries["multipart/alternative"])."--";
  492.  
  493.      }
  494.     }
  495.     $this->finalized=true;
  496.     return true;
  497.    } else {
  498.     $errorString="Al momento non ? possibile finalizzare.";
  499.     return false;
  500.    }
  501.   }
  502.  
  503.   public function reFinalize() {
  504.    $this->headerMessage="";
  505.    $this->bodyMessage="";
  506.    $this->finalized=false;
  507.    $this->finalize($errorString);
  508.   }
  509.  
  510.   private function createMultipartAlternativeMessage() {
  511.    $multipartAlternativeBoundary=$this->boundaries["multipart/alternative"];
  512.    $this->bodyMessage.="--$multipartAlternativeBoundary\n";
  513.    $this->bodyMessage.="content-type: text/plain; charset=\"".($this->text["properties"]["charset"])."\"\n";
  514.    $this->bodyMessage.="content-transfer-encoding: ".($this->text["properties"]["content-transfer-encoding"])."\n\n";
  515.    $this->bodyMessage.=$this->text["message"];
  516.    $this->bodyMessage.="\n\n";
  517.    $this->bodyMessage.="--$multipartAlternativeBoundary\n";
  518.    $this->bodyMessage.="content-type: text/html; charset=\"".($this->html["properties"]["charset"])."\"\n";
  519.    $this->bodyMessage.="content-transfer-encoding: ".($this->html["properties"]["content-transfer-encoding"])."\n\n";
  520.    $this->bodyMessage.=$this->html["message"];
  521.    $this->bodyMessage.="\n\n";
  522.   }
  523.  
  524.   private function createMultipartRelatedMessage($key) {
  525.    $obj=$this->related[$key];
  526.    $this->bodyMessage.="content-type: ".($obj["properties"]["content-type"])."; name=\"".($obj["fileName"])."\"\n";
  527.    $this->bodyMessage.="content-transfer-encoding: ".($obj["properties"]["content-transfer-encoding"])."\n";
  528.    $this->bodyMessage.="content-id: <".($obj["properties"]["content-id"]).">\n\n";
  529.    $this->bodyMessage.=$obj["source"];
  530.    $this->bodyMessage.="\n\n";
  531.   }
  532.  
  533.   private function createMultipartMixedMessage($key) {
  534.    $obj=$this->attachments[$key];
  535.    $this->bodyMessage.="content-type: ".($obj["properties"]["content-type"])."; name=\"".($obj["fileName"])."\"\n";
  536.    $this->bodyMessage.="content-transfer-encoding: ".($obj["properties"]["content-transfer-encoding"])."\n";
  537.    $this->bodyMessage.="content-disposition: ".($obj["properties"]["content-disposition"])."; filename=\"".($obj["fileName"])."\"\n\n";
  538.    $this->bodyMessage.=$obj["source"];
  539.    $this->bodyMessage.="\n\n";
  540.   }
  541.  
  542.   public function getSource(&$errorString) {
  543.    if($this->finalized) {
  544.     return ($this->headerMessage).($this->bodyMessage);
  545.    } else {
  546.     $errorString="Ancora non ? avvenuta la finalizzazione.";
  547.     return false;
  548.    }
  549.   }
  550.  
  551.   public function sendMail(&$errorString) {
  552.    if($this->finalized) {
  553.     mail($this->to, $this->subject, $this->bodyMessage, $this->headerMessage);
  554.     return true;
  555.    } else {
  556.     $errorString="Ancora non ? avvenuta la finalizzazione.";
  557.     return false;
  558.    }
  559.   }
  560.  }
  561.  
  562. ?>


PM Quote
Avatar
buba (Ex-Member)
Pro


Messaggi: 103
Iscritto: 14/04/2005

Segnala al moderatore
Postato alle 17:36
Domenica, 27/11/2005
per il futuro sarebbe meglio nn pastare direttamente il codice nel forum, ma in qualche sito (tipo phpfi.com) e poi pastare qui il link.

ciao!

PM Quote