/* Enjoy!!! Dino Ciuffetti - dam2k@users.sourceforge.net
*
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; version 2 of the License.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program (COPYING); if not, go to http://www.fsf.org/ or write
* to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
* MA 02111-1307, USA.
*
*/
/* MAIL */
/**
* This is the cmsdAm email class. Here you can only send email messages!! It is not possible to
* receive email messages using this class because is out of the design of cmsdAm and ESMTP handling
* incoming email messages.
* You can use as many attachments as you want, even binary.
* Here is how cmsdAm create and handle outgoing mail messages:
* We use MIME 1.0 mechanism to encapsulate mail message and attachments.
* Mail message and attachments are all encoded with base64.
* Attachments Content-Type are all setted to "application/octet-stream", so your attachments can
* only be downloaded by your mail client, and not automatically opened or handled by Softwares.
* This make attachment handling more robust and simple.
* Mail Message Content-Type are all setted to "text/plain" with charset "iso-8859-1". May by this
* will change in the future.
* Mail clients successfully tested: M$ Outlook, M$ Outlook Express, Squirrel WebMail, kmail.
* Class instance: $cms->mails
* @author dAm2K (Dino Ciuffetti) <dam2k@users.sourceforge.net>
*/
class cmsdam_mails
{
/**
* You should not use this variable from your section.
* HTTP class instance.
* @access private
*/
var $http;
/**
* You should not use this variable from your section.
* Configuration class instance.
* @access private
*/
var $conf;
/**
* You should not use this variable from your section.
* Users class instance.
* @access private
*/
var $users;
/**
* You should not use this variable from your section.
* Logs class instance.
* @access private
*/
var $logs;
/**
* You should not use this variable from your section.
* Debug class instance.
* @access private
*/
var $debug;
/**
* Private attachments filename stack
* Don't use from your sections!!
* @access private
*/
var $_attfilenameStack;
/**
* Private attachments filedata stack
* Don't use from your sections!!
* @access private
*/
var $_attfiledataStack;
/**
* Adds a new attachment to the mail. Multiple files with the same name are permitted.
* This method must be called first of send_mail if you want to add attachments to your mail.
* @param str attstream Binary string that represent the raw attachment file stream
* @param str attname Attachment file name
* @return bool False on errors, True on success
* @author dAm2K (Dino Ciuffetti) <dam2k@users.sourceforge.net>
*/
function add_attach ($attstream, $attname)
{ // Development in progess...
$this->_attfilenameStack[] = $attname;
$this->_attfiledataStack[] = $attstream;
return true;
}
/**
* Return the array with the list of the attached file names.
* This method must be called first of send_mail if you want to add attachments to your mail.
* @return array The list of the attached files
* @author dAm2K (Dino Ciuffetti) <dam2k@users.sourceforge.net>
*/
function list_attach ()
{ // Development in progess...
return $this->_attfilenameStack;
}
/**
* Return the (positional or keyname) attachment data string.
* @return str The attachment data string
* @author dAm2K (Dino Ciuffetti) <dam2k@users.sourceforge.net>
*/
function get_attach ($attname)
{
return $this->_attfiledataStack[$attname];
}
/**
* Delete the given attach file name from the attached files.
* This method must be called first of send_mail if you want to add attachments to your mail.
* @param str attname Attachment file name
* @return bool True on success, False on errors
* @author dAm2K (Dino Ciuffetti) <dam2k@users.sourceforge.net>
*/
function delete_attach ($attname)
{ // Development in progess...
$key = array_search ($attname, $this->_attfilenameStack);
if ($key === false) { // Attachment file name non valid
return false;
}
// Delete the array element
unset ($this->_attfilenameStack[$key]);
// Delete the array element
unset ($this->_attfiledataStack[$key]);
// Reindex the array
$this->_attfilenameStack = array_values($this->_attfilenameStack);
// Reindex the array
$this->_attfiledataStack = array_values($this->_attfiledataStack);
return true;
}
/**
* This method sends a email message.
* You can use "$to" to define the destination mail address, "$message" is the mail message body,
* "$subject" is the mail subject, and from is the mail message you want the mail comes from.
* Mail attachments support is just working with MIME message encapsulation, and you can use it
* calling [add_|list_|get_|delete_]attach methods.
* NOTE: Due to the ESMTP and PHP design this method is not capable to trap all possible errors,
* so it's still possible that you send a mail message throw this method, the mail doesn't arrive
* but this method return True.
* @param str to Destination mail address
* @param str message Mail message body
* @param str subject Mail subject
* @param str from Sender email address
* @return bool False on errors, True on success
* @author dAm2K (Dino Ciuffetti) <dam2k@users.sourceforge.net>
*/
function send_mail ($to, $message, $subject="", $from="")
{ /* This method sends a mail.
*/
$mime_boundary = 'Boundary-00=_94D4/fNonpwbXHw';
$User = $this->users->logged_user;
$client_ip = $this->http->client_ip;
$cmsdam_admin_email = $this->conf->cmsdam_admin_email;
// Some check!!
if (($from == "") || (strlen($from)<=0)) { // From is NOT passed
// Set cmsdam_admin_email as from address
$from = $cmsdam_admin_email;
}
if (($message == "") || (strlen($message)<=0)) { // Message NOT passed
return false;
}
if ($User == "") {
$tmpUser = "anonymous";
} else {
$tmpUser = $User;
}
$_msg = "\r\n--$mime_boundary\r\n";
$_msg .="Content-Type: text/plain;\r\n";
$_msg .=" charset=\"iso-8859-1\"\r\n";
$_msg .="Content-Transfer-Encoding: base64\r\n";
$_msg .="Content-Disposition: inline\r\n";
$_msg .="\r\n";
$_msg .= base64_encode($message);
$_msg .="\r\n";
$_msg .="\r\n";
if (count($this->list_attach()) > 0) { // Mail have attachments
// Add all attachments and its mimes to the message
for ($j=0; $jlist_attach()); $j++) {
// Attachments cycle
$attachment_arr = $this->list_attach();
$attachment = $attachment_arr[$j];
$attachment_file = $this->get_attach($j);
$_msg .= "--$mime_boundary\r\n";
$_msg .= "Content-Type: application/octet-stream;\r\n";
$_msg .= " name=\"" . $attachment . "\"\r\n";
$_msg .= "Content-Transfer-Encoding: base64\r\n";
$_msg .= "Content-Disposition: attachment;\r\n";
$_msg .= "\tfilename=\"" . $attachment . "\"\r\n";
$_msg .="\r\n";
$_msg .= base64_encode($attachment_file);
$_msg .="\r\n";
$_msg .="\r\n";
}
$mime_terminator = "--" . $mime_boundary . "--";
} else {
$mime_terminator = "--" . $mime_boundary . "--";
}
// We use MIME 1.0 mechanism to encapsulate mail message and attachments.
// Mail message and attachments are encoded with base64.
// Attachments Content-Type are setted to "application/octet-stream"
// Mail Message Content-Type are setted to "text/plain", charset: "iso-8859-1"
// This is the message header.
$_header = "From: $from\r\n"
."Reply-To: $from\r\n"
."User-Agent: " . $this->conf->cmsdam_longversion . "\r\n"
."MIME-Version: 1.0\r\n"
."Content-Type: Multipart/Mixed;\r\n"
." boundary=\"" . $mime_boundary . "\"\r\n"
."X-Mailer: " . $this->conf->cmsdam_longversion . "\r\n"
."X-Sender: " . $client_ip . "\r\n"
."X-cmsdAmUser: " . $tmpUser;
$_msg .= $mime_terminator;
// Send the mail message!!
$email = mail($to, $subject, $_msg, $_header);
if ($email == true) { // Email successfully sent!
$this->debug->debug_write(6, "Email \"$subject\" successfully sent to \"$to\"");
$this->logs->log_note(6, "Email \"$subject\" successfully sent to \"$to\"");
return true;
} else { // Email failures!
$this->debug->debug_write(6, "Errors sending email \"$subject\" to \"$to\"");
$this->logs->log_note(6, "Errors sending email \"$subject\" to \"$to\"");
return false;
}
}
}
?>