2006-09-11 20:06:01 +00:00
|
|
|
<?php
|
|
|
|
/* SVN FILE: $Id$ */
|
|
|
|
/**
|
|
|
|
* Short description for file.
|
|
|
|
*
|
|
|
|
* Long description for file
|
|
|
|
*
|
|
|
|
* PHP versions 4 and 5
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* CakePHP : Rapid Development Framework <http://www.cakephp.org/>
|
|
|
|
* Copyright (c) 2006, Cake Software Foundation, Inc.
|
|
|
|
* 1785 E. Sahara Avenue, Suite 490-204
|
|
|
|
* Las Vegas, Nevada 89104
|
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
|
|
|
* @filesource
|
|
|
|
* @copyright Copyright (c) 2006, Cake Software Foundation, Inc.
|
|
|
|
* @link http://www.cakefoundation.org/projects/info/cakephp CakePHP Project
|
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.libs.controller.components
|
|
|
|
* @since CakePHP v 1.2.0.3467
|
|
|
|
* @version $Revision$
|
|
|
|
* @modifiedby $LastChangedBy$
|
|
|
|
* @lastmodified $Date$
|
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Short description for file.
|
|
|
|
*
|
|
|
|
* Long description for file
|
|
|
|
*
|
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.libs.controller.components
|
|
|
|
*
|
|
|
|
*/
|
2006-09-28 20:29:04 +00:00
|
|
|
class EmailComponent extends Object {
|
2006-09-11 20:37:13 +00:00
|
|
|
var $to = null;
|
|
|
|
var $from = null;
|
2006-09-12 00:26:50 +00:00
|
|
|
var $replyTo = null;
|
|
|
|
var $return = null;
|
|
|
|
var $cc = array();
|
|
|
|
var $bcc = array();
|
2006-09-11 20:37:13 +00:00
|
|
|
var $subject = null;
|
|
|
|
var $template = 'default';
|
|
|
|
var $sendAs = 'text';
|
|
|
|
var $delivery = 'mail';
|
2006-09-12 00:26:50 +00:00
|
|
|
var $charset = 'ISO-8859-15';
|
|
|
|
var $attachments = array();
|
2006-09-23 23:35:22 +00:00
|
|
|
var $xMailer = 'CakePHP EmailComponent';
|
2006-09-12 00:26:50 +00:00
|
|
|
var $filePaths = array();
|
2006-09-11 20:37:13 +00:00
|
|
|
|
|
|
|
var $_debug = false;
|
|
|
|
var $_error = false;
|
2006-09-21 08:49:18 +00:00
|
|
|
var $_newLine = "\r\n";
|
2006-09-11 20:37:13 +00:00
|
|
|
var $_lineLength = 75;
|
2006-09-11 20:06:01 +00:00
|
|
|
|
2006-09-12 00:26:50 +00:00
|
|
|
var $__header = null;
|
|
|
|
var $__boundary = null;
|
2006-09-12 01:55:32 +00:00
|
|
|
var $__message = null;
|
2006-09-12 00:26:50 +00:00
|
|
|
|
|
|
|
|
2006-09-12 01:55:32 +00:00
|
|
|
function send($content){
|
2006-09-12 00:26:50 +00:00
|
|
|
if($this->template === null) {
|
2006-09-12 01:55:32 +00:00
|
|
|
if(is_array($content)){
|
|
|
|
$message = null;
|
|
|
|
foreach ($content as $key => $value){
|
|
|
|
$message .= $value . $this->_newLine;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$message = $content;
|
|
|
|
}
|
2006-09-12 00:26:50 +00:00
|
|
|
} else {
|
2006-09-12 01:55:32 +00:00
|
|
|
$message = $this->__renderTemplate($content);
|
2006-09-12 00:26:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->__createBoundary();
|
|
|
|
$this->__createHeader();
|
|
|
|
$this->__formatMessage($message);
|
|
|
|
|
2006-09-21 08:49:18 +00:00
|
|
|
if(!empty($this->attachments)) {
|
2006-09-12 00:26:50 +00:00
|
|
|
$this->__attachFiles();
|
|
|
|
}
|
|
|
|
$__method = '__'.$this->delivery;
|
|
|
|
$this->$__method();
|
|
|
|
}
|
|
|
|
|
2006-09-23 23:35:22 +00:00
|
|
|
function reset() {
|
|
|
|
$this->to = null;
|
|
|
|
$this->from = null;
|
|
|
|
$this->replyTo = null;
|
|
|
|
$this->return = null;
|
|
|
|
$this->cc = array();
|
|
|
|
$this->bcc = array();
|
|
|
|
$this->subject = null;
|
|
|
|
$this->__header = null;
|
|
|
|
$this->__boundary = null;
|
|
|
|
$this->__message = null;
|
|
|
|
}
|
|
|
|
|
2006-09-12 00:26:50 +00:00
|
|
|
function __createBoundary(){
|
|
|
|
$this->__boundary = md5(uniqid(time()));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function __createHeader(){
|
|
|
|
$this->__header .= 'From: ' . $this->from . $this->_newLine;
|
|
|
|
$this->__header .= 'Reply-To: ' . $this->replyTo . $this->_newLine;
|
|
|
|
$this->__header .= 'Return-Path: ' . $this->return . $this->_newLine;
|
|
|
|
|
|
|
|
$addresses = null;
|
|
|
|
if(!empty($this->cc)) {
|
|
|
|
foreach ($this->cc as $cc) {
|
|
|
|
$addresses .= $cc . ', ';
|
|
|
|
}
|
|
|
|
$this->__header .= 'cc: ' . $addresses . $this->_newLine;
|
|
|
|
$this->to .= ', ' . $addresses;
|
|
|
|
}
|
|
|
|
if(!empty($this->bcc)) {
|
|
|
|
foreach ($this->bcc as $bcc) {
|
|
|
|
$addresses .= $bcc . ', ';
|
|
|
|
}
|
|
|
|
$this->__header .= 'Bcc: ' . $addresses . $this->_newLine;
|
|
|
|
$this->to .= ', ' . $addresses;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->__header .= 'X-Mailer: ' . $this->xMailer . $this->_newLine;
|
|
|
|
|
2006-09-21 08:49:18 +00:00
|
|
|
if(!empty($this->attachments) && $this->sendAs === 'text') {
|
2006-09-12 00:26:50 +00:00
|
|
|
$this->__header .= 'MIME-Version: 1.0' . $this->_newLine;
|
|
|
|
$this->__header .= 'Content-Type: multipart/mixed; boundary=' . $this->__boundary . $this->_newLine;
|
2006-09-21 08:49:18 +00:00
|
|
|
} elseif(!empty($this->attachments) && $this->sendAs === 'html') {
|
2006-09-12 00:26:50 +00:00
|
|
|
$this->__header .= 'MIME-Version: 1.0' . $this->_newLine;
|
|
|
|
$this->__header .= 'Content-Type: multipart/related; boundary=' . $this->__boundary . $this->_newLine;
|
|
|
|
} elseif($this->sendAs === 'html') {
|
|
|
|
$this->__header .= 'MIME-Version: 1.0' . $this->_newLine;
|
|
|
|
$this->__header .= 'Content-Type: multipart/alternative; boundary=' . $this->__boundary . $this->_newLine;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function __formatMessage($message){
|
2006-09-12 01:55:32 +00:00
|
|
|
$message = $this->__wrap($message);
|
2006-09-12 00:26:50 +00:00
|
|
|
|
|
|
|
if($this->sendAs === 'html'){
|
2006-09-21 08:49:18 +00:00
|
|
|
$this->__message = '--' . $this->__boundary . $this->_newLine;
|
|
|
|
$this->__message .= 'Content-Type: text/plain; charset=' . $this->charset . $this->_newLine;
|
|
|
|
$this->__message .= 'Content-Transfer-Encoding: 8bit' . $this->_newLine;
|
|
|
|
$this->__message .= 'If you are seeing this is because you may need to change your'.$this->_newLine;
|
|
|
|
$this->__message .= 'preferred message format from HTML to plain text.'.$this->_newLine.$this->_newLine;
|
2006-09-12 01:55:32 +00:00
|
|
|
$this->__message .= strip_tags($message) . $this->_newLine;
|
2006-09-21 08:49:18 +00:00
|
|
|
|
2006-09-12 01:55:32 +00:00
|
|
|
$this->__message = '--' . $this->__boundary . $this->_newLine;
|
|
|
|
$this->__message .= 'Content-Type: text/html; charset=' . $this->charset . $this->_newLine;
|
|
|
|
$this->__message .= 'Content-Transfer-Encoding: 8bit' . $this->_newLine;
|
|
|
|
$this->__message .= $message . $this->_newLine;
|
|
|
|
$this->__message .= $this->_newLine . $this->_newLine;
|
2006-09-12 00:26:50 +00:00
|
|
|
} else {
|
2006-09-12 01:55:32 +00:00
|
|
|
$this->__message .= $message . $this->_newLine;
|
2006-09-12 00:26:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function __attachFiles(){
|
|
|
|
foreach($this->attachments as $attachment) {
|
|
|
|
$files[] = $this->__findFiles($attachment);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach($files as $file) {
|
|
|
|
$handle = fopen($file, 'rb');
|
|
|
|
$data = fread($handle, filesize($file));
|
|
|
|
$data = chunk_split(base64_encode($data)) ;
|
|
|
|
$filetype = mime_content_type($file);
|
|
|
|
|
2006-09-12 01:55:32 +00:00
|
|
|
$this->__message .= '--' . $this->__boundary . $this->_newLine;
|
|
|
|
$this->__message .= 'Content-Type: ' . $filetype . '; name="' . $file . '"' . $this->_newLine;
|
|
|
|
$this->__message .= 'Content-Transfer-Encoding: base64' . $this->_newLine;
|
|
|
|
$this->__message .= 'Content-Disposition: attachment; filename="' .$file. '"' . $this->_newLine . $this->_newLine;
|
|
|
|
$this->__message .= $data . $this->_newLine . $this->_newLine;
|
2006-09-12 00:26:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function __findFiles($attachment){
|
|
|
|
foreach($this->filePaths as $path) {
|
|
|
|
if (file_exists($path . DS . $attachment)) {
|
|
|
|
$file = $path . DS . $attachment;
|
|
|
|
return $file;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-09-12 01:55:32 +00:00
|
|
|
function __wrap($message) {
|
2006-09-21 09:32:05 +00:00
|
|
|
$message = str_replace(array('\r','\n'), '\n', $message);
|
|
|
|
$words = explode('\n', $message);
|
2006-09-21 08:49:18 +00:00
|
|
|
$formated = null;
|
2006-09-12 01:55:32 +00:00
|
|
|
foreach ($words as $word) {
|
2006-09-21 09:32:05 +00:00
|
|
|
$formated .= wordwrap($word, $this->_lineLength, ' ', 1);
|
|
|
|
$formated .= "\n";
|
2006-09-12 01:55:32 +00:00
|
|
|
}
|
|
|
|
return $formated;
|
|
|
|
}
|
|
|
|
|
2006-09-12 00:26:50 +00:00
|
|
|
function __mail(){
|
2006-09-12 01:55:32 +00:00
|
|
|
return @mail($this->to, $this->subject, $this->__message, $this->__header);
|
|
|
|
}
|
|
|
|
|
|
|
|
function __smtp(){
|
|
|
|
|
2006-09-12 00:26:50 +00:00
|
|
|
}
|
2006-09-11 20:06:01 +00:00
|
|
|
}
|
|
|
|
?>
|