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-12 00:26:50 +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();
|
|
|
|
var $xMailer = 'CakePHP EmailComponent $Revision$';
|
|
|
|
var $filePaths = array();
|
2006-09-11 20:37:13 +00:00
|
|
|
|
|
|
|
var $_debug = false;
|
|
|
|
var $_error = false;
|
|
|
|
var $_newLine = '\n';
|
|
|
|
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);
|
|
|
|
|
|
|
|
if(!empty($this->$attachments)) {
|
|
|
|
$this->__attachFiles();
|
|
|
|
}
|
|
|
|
$__method = '__'.$this->delivery;
|
|
|
|
$this->$__method();
|
|
|
|
}
|
|
|
|
|
|
|
|
function __createBoundary(){
|
|
|
|
$this->__boundary = md5(uniqid(time()));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function __createHeader(){
|
|
|
|
$this->__header .= 'To: ' . $this->to . $this->_newLine;
|
|
|
|
$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;
|
|
|
|
|
|
|
|
if(!empty($this->$attachments) && $this->sendAs === 'text') {
|
|
|
|
$this->__header .= 'MIME-Version: 1.0' . $this->_newLine;
|
|
|
|
$this->__header .= 'Content-Type: multipart/mixed; boundary=' . $this->__boundary . $this->_newLine;
|
|
|
|
} elseif(!empty($this->$attachments) && $this->sendAs === 'html') {
|
|
|
|
$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);
|
|
|
|
$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 .= strip_tags($message) . $this->_newLine;
|
2006-09-12 00:26:50 +00:00
|
|
|
|
|
|
|
if($this->sendAs === 'html'){
|
2006-09-12 01:55:32 +00:00
|
|
|
$this->__message .= strip_tags($message) . $this->_newLine;
|
|
|
|
$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) {
|
|
|
|
$message = str_replace("\r\n", "\n ", $message);
|
|
|
|
$words = explode(" ", $message);
|
|
|
|
foreach ($words as $word) {
|
|
|
|
$formated .= chunk_split($word, $this->_lineLength, " ") . " ";
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
?>
|