00001 <?php
00008 class Mailer {
00009
00010 protected $message = "";
00011 protected $subject = "";
00012 protected $replyTo = "";
00013 protected $attachments = array();
00014 private $recipient = array();
00015 protected $readyRecipients;
00016 protected $from = "From: Shodor \r\n";
00017
00023 public function setDevRecipient($email){
00024 $this->devRecipient = $email;
00025 }
00026
00033 public function setMessage($message) {
00034 $this->message = $message;
00035 }
00036
00041 public function setSubject($subject) {
00042 $this->subject = $subject;
00043 }
00044
00049 public function setReplyTo($address) {
00050 $this->replyTo = "Reply-To: " . $address . "\r\n";
00051 }
00052
00057 public function setFrom($from) {
00058 $this->from = "From: " . $from . "\r\n";
00059 }
00060
00065 public function addRecipient($recipient) {
00066 if($recipient == "") return false;
00067 if(is_array($recipient)) {
00068
00069 $this->recipient = array_merge($this->recipient, $recipient);
00070 } else {
00071
00072 array_push($this->recipient, $recipient);
00073 }
00074 }
00075
00079 public function clearRecipients() {
00080 $this->recipient = array();
00081 }
00082
00090 public function addAttachment($path, $name, $type="octet-stream") {
00091
00092 if(!file_exists($path) || preg_match("#[a-zA-Z0-9]{1,10}#", $type) == 0) {
00093
00094 $this->attachments[] = 'Content-Type: text/plain; charset=US-ASCII; format=flowed
00095 Content-Transfer-Encoding: 7bit
00096
00097
00098 ERROR: File Not found or Invalid MIME type.
00099 ';
00100 return FALSE;
00101 }
00102
00103 $attachmentDump = chunk_split(base64_encode(file_get_contents($path)));
00104
00105 $disallowedCharacters = array("\n", "\r", '"', "'");
00106
00107 $this->attachments[] = 'Content-Disposition: attachment;
00108 filename=' . str_replace($disallowedCharacters, '', $name) . '
00109 Content-Type: application/'. $type .';
00110 name="' . str_replace($disallowedCharacters, '', $name) . '"
00111 Content-Transfer-Encoding: base64
00112
00113 ' . $attachmentDump;
00114 return TRUE;
00115 }
00116
00123 public function sendMail() {
00124
00125 if(count($this->recipient) == 0 || $this->subject == "" || $this->message == "") return false;
00126
00127
00128 $this->processRecipients();
00129
00130
00131 $headers = '';
00132
00133 if (count($this->attachments) > 0) {
00134
00135 $splitText = rand();
00136 $headers = 'Content-Type: multipart/mixed; boundary='.$splitText.'
00137 Mime-Version: 1.0;';
00138 $emailBody = '--' . $splitText . '
00139 Content-Type: text/plain; charset=US-ASCII; format=flowed
00140 Content-Transfer-Encoding: 7bit
00141
00142 ' . $this->message;
00143 foreach($this->attachments as $attachment) {
00144 $emailBody .= '
00145 --' . $splitText . "\n" . $attachment;
00146 }
00147
00148
00149 } else $emailBody = $this->message;
00150
00151
00152 return mail($this->readyRecipients, $this->subject, $emailBody, $this->from . $this->replyTo . $headers);
00153
00154 }
00155
00162 protected function processRecipients() {
00163 $this->readyRecipients = implode(", ", $this->recipient);
00164 }
00165 }
00166
00167 ?>