# 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
    00015     class MailException extends BaseException {/*_*/};
    00016
    00020     class MailNotSentException extends MailException {/*_*/};
    00021
    00029     final class Mail
    00030     {
    00031         private $to             = null;
    00032         private $cc             = null;
    00033         private $text           = null;
    00034         private $subject        = null;
    00035         private $from           = null;
    00036         private $encoding       = null;
    00037         private $contentType    = null;
    00038         private $returnPath     = null;
    00039
    00040         private $sendmailAdditionalArgs = null;
    00041
    00045         public static function create()
    00046         {
    00047             return new self;
    00048         }
    00049
    00053         public function send()
    00054         {
    00055             if ($this->to == null)
    00056                 throw new WrongArgumentException("mail to: is not specified");
    00057
    00058             $siteEncoding = mb_get_info('internal_encoding');
    00059
    00060             if (!$this->encoding
    00061                 || $this->encoding == $siteEncoding
    00062             ) {
    00063                 $encoding = $siteEncoding;
    00064                 $to = $this->to;
    00065                 $from = $this->from;
    00066                 $subject =
    00067                     "=?".$encoding."?B?"
    00068                     .base64_encode($this->subject)
    00069                     ."?=";
    00070                 $body = $this->text;
    00071                 $returnPath = $this->returnPath;
    00072             } else {
    00073                 $encoding = $this->encoding;
    00074                 $to = mb_convert_encoding($this->to, $encoding);
    00075
    00076                 if ($this->from)
    00077                     $from = mb_convert_encoding($this->from, $encoding);
    00078                 else
    00079                     $from = null;
    00080
    00081                 if ($this->returnPath)
    00082                     $returnPath = mb_convert_encoding($this->returnPath, $encoding);
    00083                 else
    00084                     $returnPath = null;
    00085
    00086                 $subject =
    00087                     "=?".$encoding."?B?"
    00088                     .base64_encode(
    00089                         iconv(
    00090                             $siteEncoding,
    00091                             $encoding.'//TRANSLIT',
    00092                             $this->subject
    00093                         )
    00094                     )."?=";
    00095
    00096                 $body = iconv(
    00097                     $siteEncoding,
    00098                     $encoding.'//TRANSLIT',
    00099                     $this->text
    00100                 );
    00101             }
    00102
    00103             $headers = null;
    00104
    00105             $returnPathAtom =
    00106                 $returnPath !== null
    00107                     ? $returnPath
    00108                     : $from;
    00109
    00110             if ($from != null) {
    00111                 $headers .= "From: ".$from."\n";
    00112                 $headers .= "Return-Path: ".$returnPathAtom."\n";
    00113             }
    00114
    00115             if ($this->cc != null)
    00116                 $headers .= "Cc: ".$this->cc."\n";
    00117
    00118             if ($this->contentType === null)
    00119                 $this->contentType = 'text/plain';
    00120
    00121             $headers .=
    00122                 "Content-type: ".$this->contentType
    00123                 ."; charset=".$encoding."\n";
    00124
    00125             $headers .= "Content-Transfer-Encoding: 8bit\n";
    00126             $headers .= "Date: ".date('r')."\n";
    00127
    00128             if (
    00129                 !mail(
    00130                     $to, $subject, $body, $headers,
    00131                     $this->getSendmailAdditionalArgs()
    00132                 )
    00133             )
    00134                 throw new MailNotSentException();
    00135
    00136             return $this;
    00137         }
    00138
    00142         public function setTo($to)
    00143         {
    00144             $this->to = $to;
    00145             return $this;
    00146         }
    00147
    00151         public function setCc($cc)
    00152         {
    00153             $this->cc = $cc;
    00154             return $this;
    00155         }
    00156
    00160         public function setSubject($subject)
    00161         {
    00162             $this->subject = $subject;
    00163             return $this;
    00164         }
    00165
    00169         public function setText($text)
    00170         {
    00171             $this->text = $text;
    00172             return $this;
    00173         }
    00174
    00178         public function setFrom($from)
    00179         {
    00180             $this->from = $from;
    00181             return $this;
    00182         }
    00183
    00187         public function setEncoding($encoding)
    00188         {
    00189             $this->encoding = $encoding;
    00190             return $this;
    00191         }
    00192
    00193         public function getContentType()
    00194         {
    00195             return $this->contentType;
    00196         }
    00197
    00201         public function setContentType($contentType)
    00202         {
    00203             $this->contentType = $contentType;
    00204             return $this;
    00205         }
    00206
    00207         public function getSendmailAdditionalArgs()
    00208         {
    00209             return $this->sendmailAdditionalArgs;
    00210         }
    00211
    00215         public function setSendmailAdditionalArgs($sendmailAdditionalArgs)
    00216         {
    00217             $this->sendmailAdditionalArgs = $sendmailAdditionalArgs;
    00218             return $this;
    00219         }
    00220
    00221         public function getReturnPath()
    00222         {
    00223             return $this->returnPath;
    00224         }
    00225
    00229         public function setReturnPath($returnPath)
    00230         {
    00231             $this->returnPath = $returnPath;
    00232             return $this;
    00233         }
    00234     }
    00235 ?>