Mail.class.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************************
00003  *   Copyright (C) 2004-2007 by Anton E. Lebedevich                        *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU Lesser General Public License as        *
00007  *   published by the Free Software Foundation; either version 3 of the    *
00008  *   License, or (at your option) any later version.                       *
00009  *                                                                         *
00010  ***************************************************************************/
00011 /* $Id: Mail.class.php 4687 2007-12-09 18:57:18Z voxus $ */
00012 
00016     class MailException extends BaseException {/*_*/};
00017     
00021     class MailNotSentException extends MailException {/*_*/};
00022     
00026     final class Mail
00027     {
00028         private $to             = null;
00029         private $cc             = null;
00030         private $text           = null;
00031         private $subject        = null;
00032         private $from           = null;
00033         private $encoding       = null;
00034         private $contentType    = null;
00035         
00039         public static function create()
00040         {
00041             return new self;
00042         }
00043         
00047         public function send()
00048         {
00049             if ($this->to == null)
00050                 throw new WrongArgumentException("mail to: is not specified");
00051             
00052             $siteEncoding = mb_get_info('internal_encoding');
00053             
00054             if (!$this->encoding
00055                 || $this->encoding == $siteEncoding
00056             ) {
00057                 $encoding = $siteEncoding;
00058                 $to = $this->to;
00059                 $from = $this->from;
00060                 $subject =
00061                     "=?".$encoding."?B?"
00062                     .base64_encode($this->subject)
00063                     ."?=";
00064                 $body = $this->text;
00065             } else {
00066                 $encoding = $this->encoding;
00067                 $to = mb_convert_encoding($this->to, $encoding);
00068                 
00069                 if ($this->from)
00070                     $from = mb_convert_encoding($this->from, $encoding);
00071                 else
00072                     $from = null;
00073                 
00074                 $subject =
00075                     "=?".$encoding."?B?"
00076                     .base64_encode(
00077                         iconv(
00078                             $siteEncoding,
00079                             $encoding.'//TRANSLIT',
00080                             $this->subject
00081                         )
00082                     )."?=";
00083                 
00084                 $body = iconv(
00085                     $siteEncoding,
00086                     $encoding.'//TRANSLIT',
00087                     $this->text
00088                 );
00089             }
00090             
00091             $headers = null;
00092             
00093             if ($from != null) {
00094                 $headers .= "From: ".$from."\n";
00095                 $headers .= "Return-Path: ".$from."\n";
00096             }
00097             
00098             if ($this->cc != null)
00099                 $headers .= "Cc: ".$this->cc."\n";
00100             
00101             if ($this->contentType === null)
00102                 $this->contentType = 'text/plain';
00103             
00104             $headers
00105                 .= "Content-type: ".$this->contentType
00106                 ."; charset=".$encoding."\n";
00107             
00108             $headers .= "Content-Transfer-Encoding: 8bit\n";
00109             $headers .= "Date: ".date('r')."\n";
00110             
00111             if (!mail($to, $subject, $body, $headers))
00112                 throw new MailNotSentException();
00113             
00114             return $this;
00115         }
00116         
00120         public function setTo($to)
00121         {
00122             $this->to = $to;
00123             return $this;
00124         }
00125         
00129         public function setCc($cc)
00130         {
00131             $this->cc = $cc;
00132             return $this;
00133         }
00134         
00138         public function setSubject($subject)
00139         {
00140             $this->subject = $subject;
00141             return $this;
00142         }
00143         
00147         public function setText($text)
00148         {
00149             $this->text = $text;
00150             return $this;
00151         }
00152         
00156         public function setFrom($from)
00157         {
00158             $this->from = $from;
00159             return $this;
00160         }
00161         
00165         public function setEncoding($encoding)
00166         {
00167             $this->encoding = $encoding;
00168             return $this;
00169         }
00170         
00171         public function getContentType()
00172         {
00173             return $this->contentType;
00174         }
00175         
00179         public function setContentType($contentType)
00180         {
00181             $this->contentType = $contentType;
00182             return $this;
00183         }
00184     }
00185 ?>

Generated on Sun Dec 9 21:56:24 2007 for onPHP by  doxygen 1.5.4