00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
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.'
00080 $this->subject
00081 )
00082 )."?=";
00083
00084 $body = iconv(
00085 $siteEncoding,
00086 $encoding.'
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 ?>