]>
code.delx.au - gnu-emacs/blob - test/manual/etags/php-src/sendmail.php
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.
10 final class sendMail
{
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.";
18 private static $messageProperties=array(
28 "content-transfer-encoding" => array(
38 private $attachmentProperties=array(
39 "content-type" => array(
40 "modifiable" => false,
42 "application/octet-stream"
45 "content-transfer-encoding" => array(
46 "modifiable" => false,
51 "content-disposition" => array(
60 private static $relatedProperties=array(
61 "content-transfer-encoding" => array(
62 "modifiable" => false,
75 public static function valid_eMailAddress($eMailAddress) {
76 if(ereg("^[^@ ]+@[^@ ]+\.[^@ ]+$", $eMailAddress))
82 public static function validContentId($contentId) {
83 if(ereg("^[a-zA-Z0-9]+$", $contentId))
89 public static function validContentKey($contentKey) {
90 if(ereg("^[a-zA-Z0-9]+$", $contentKey))
96 public static function mime_content_type($filename) {
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',
109 return $mime[strrchr($filename, '.')];
118 private $headerMessage;
119 private $bodyMessage;
123 public function __construct($from, $to, $subject=self
::defaultSubject
) {
126 if(!self
::valid_eMailAddress($from))
127 die(self
::eMailAddressErrorMessage
);
132 if(!self
::valid_eMailAddress($to))
133 die(self
::eMailAddressErrorMessage
);
138 $this->subject
=$subject;
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]
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]
158 // set related and attachments
159 $this->related
=array();
160 $this->attachments
=array();
162 // set finalizater counter
163 $this->finalized
=false;
165 $this->headerMessage
="";
166 $this->bodyMessage
="";
168 $this->boundaries
=array(
169 "multipart/alternative" => md5(uniqid(microtime())),
170 "multipart/related" => md5(uniqid(microtime())),
171 "multipart/mixed" => md5(uniqid(microtime()))
176 public function setTo($to, &$errorString) {
177 if(self
::valid_eMailAddress($to)) {
181 $errorString=eMailAddressErrorMessage
;
186 public function setFrom($from, &$errorString) {
187 if(self
::valid_eMailAddress($from)) {
191 $errorString=eMailAddressErrorMessage
;
196 public function setSubject($subject=self
::defaultSubject
) {
197 $this->subject
=$subject;
200 public function setTextMessage($textMessage=self
::defaultTextMessage
) {
201 $this->text
["message"]=$textMessage;
204 public function setTextMessageProperty($key, $value, &$errorString) {
206 $key=strtolower($key);
207 $value=strtolower($value);
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;
215 $errorString="Il valore della propriet? indicata non ? modificabile.";
219 $errorString="Il valore indicato per questa propriet? non ? valido.";
223 $errorString="Non esiste questa propriet? per i messaggi html.";
228 public function setHtmlMessage($htmlMessage=self
::defaultHtmlMessage
) {
229 $this->html
["message"]=$htmlMessage;
232 public function setHtmlMessageProperty($key, $value, &$errorString) {
234 $key=strtolower($key);
235 $value=strtolower($value);
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;
243 $errorString="Il valore della propriet? indicata non ? modificabile.";
247 $errorString="Il valore indicato per questa propriet? non ? valido.";
251 $errorString="Non esiste questa propriet? per i messaggi html.";
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
269 "source" => base64_encode(
270 fread($fileHandle, filesize($fileName))
275 $errorString="L' id specificato non ? valido.";
279 $errorString="La chiave specificata ? gi? associata ad un altro related.";
283 $errorString="La chiave specificata per il related non ? valida.";
287 $errorString="Non ? possibile aprire il file indicato.";
291 $errorString="Il nome del file indicato non ? valido.";
296 public function setRelatedProperty($relatedKey, $key, $value, &$errorString) {
298 $key=strtolower($key);
299 $value=strtolower($value);
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;
308 $errorString="Il related indicato non esiste.";
312 $errorString="Il valore della propriet? indicata non ? modificabile.";
316 $errorString="Il valore indicato per questa propriet? non ? valido.";
320 $errorString="Non esiste questa propriet? per i related.";
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]
337 "source" => base64_encode(
338 fread($fileHandle, filesize($fileName))
343 $errorString="La chiave specificata ? gi? associata ad un altro allegato.";
347 $errorString="La chiave specificata per l'allegato non ? valida.";
351 $errorString="Non ? possibile aprire il file indicato.";
355 $errorString="Il nome del file indicato non ? valido.";
360 public function setAttachmentProperty($attachmentKey, $key, $value, &$errorString) {
362 $key=strtolower($key);
363 $value=strtolower($value);
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;
372 $errorString="L'allegato indicato non esiste.";
376 $errorString="Il valore della propriet? indicata non ? modificabile.";
380 $errorString="Il valore indicato per questa propriet? non ? valido.";
384 $errorString="Non esiste questa propriet? per gli allegati.";
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";
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";
401 $this->bodyMessage
="--".($this->boundaries
["multipart/mixed"])."\n";
403 if(($countRelated=count($this->related
))>0) {
404 $this->bodyMessage
.="content-type: multipart/related; type=\"multipart/alternative\"; boundary=\"".($this->boundaries
["multipart/related"])."\"\n\n";
406 $this->bodyMessage
.="--".($this->boundaries
["multipart/related"])."\n";
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";
412 // aggiungere i related e chiudere
415 while(list($key,)=each($this->related
)) {
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";
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";
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";
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";
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";
449 while(list($key,)=each($this->related
)) {
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";
457 $this->headerMessage
.="content-type: multipart/alternative; boundary=\"".($this->boundaries
["multipart/alternative"])."\"\n\n";
458 $this->headerMessage
.=self
::defaultHeaderMessage
;
459 $this->headerMessage
.="\n\n";
461 $this->createMultipartAlternativeMessage();
462 $this->bodyMessage
.="--".($this->boundaries
["multipart/alternative"])."--";
466 $this->finalized
=true;
469 $errorString="Al momento non ? possibile finalizzare.";
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";
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";
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";
506 public function getSource(&$errorString) {
507 if($this->finalized
) {
508 return ($this->headerMessage
).($this->bodyMessage
);
510 $errorString="Ancora non ? avvenuta la finalizzazione.";
515 public function sendMail(&$errorString) {
516 if($this->finalized
) {
517 mail($this->to
, $this->subject
, $this->bodyMessage
, $this->headerMessage
);
518 $this->finalized
=false;
521 $errorString="Ancora non ? avvenuta la finalizzazione.";