load->..." and * "$this->email->from..."): * * - http://codeigniter.com/user_guide/libraries/email.html * * * Usage example: * -------------- * setMailType('html'); * * $geekMail->from('noreply@geekology.co.za', 'Geekology'); * * $geekMail->to('willem@geekology.co.za'); * //$geekMail->cc('willem@geekology.co.za'); * //$geekMail->bcc('willem@geekology.co.za'); * * $geekMail->subject('Example subject'); * * $geekMail->message('Example message.'); * * $geekMail->attach('/home/willem/file1.txt'); * $geekMail->attach('/home/willem/file2.zip'); * * if (!$geekMail->send()) * { * $errors = $geekMail->getDebugger(); * print_r($errors); * } * * ?> * * * Adapted by: Willem van Zyl * willem@geekology.co.za * http://www.geekology.co.za/blog/ * * * --- * As with CodeIgniter itself, this code is free and Open Source, but * EllisLab, Inc. (the creators of CodeIgniter) require that this license * agreement be included in all adaptations: * * http://codeigniter.com/user_guide/license.html * --- */ class ctf_geekMail { var $_altBoundary = ''; var $_altMessage = ''; var $_atcBoundary = ''; var $_attachDisp = array(); var $_attachName = array(); var $_attachType = array(); var $_baseCharsets = array('us-ascii', 'iso-2022-'); var $_bccArray = array(); var $_bccBatchMode = false; var $_bccBatchSize = 200; var $_bitDepths = array('7bit', '8bit'); var $_body = ''; var $_ccArray = array(); var $_charSet = 'utf-8'; var $_crlf = "\n"; var $_debugMsg = array(); var $_encoding = '8bit'; var $_finalBody = ''; var $_headerStr = ''; var $_headers = array(); var $_ip = false; var $_log = array(); var $_mailPath = '/usr/sbin/sendmail'; var $_mailType = 'text'; var $_multipart = 'mixed'; var $_newLine = "\n"; var $_priorities = array('1 (Highest)', '2 (High)', '3 (Normal)', '4 (Low)', '5 (Lowest)'); var $_priority = '3'; var $_protocol = 'mail'; var $_protocols = array('mail', 'sendmail', 'smtp'); var $_recipients = array(); var $_replytoFlag = false; var $_safeMode = false; var $_sendMultipart = true; var $_smtpAuth = false; var $_smtpConnect = ''; var $_smtpHost = ''; var $_smtpPass = ''; var $_smtpPort = '25'; var $_smtpTimeout = 5; var $_smtpUser = ''; var $_subject = ''; var $_userAgent = 'WP geekMail'; var $_validate = false; var $_wordWrap = false; var $_wrapChars = '76'; /** * Class constructor */ function __construct() { //should smtp authenticated be used? $this->_smtpAuth = (($this->_smtpUser == '') and ($this->_smtpPass == '')) ? false : true; //has safe mode been enabled in the PHP configuration file? $this->_safeMode = ((boolean)@ini_get("safe_mode") === false) ? false : true; $this->_logMessage('debug', 'geekMail Class Initialized'); } /** * Logging methods to store and retrieve messages. */ function _logMessage($type, $message) { //check that a valid log type was specified: switch ($type) { case 'debug': break; case 'error': break; case 'info': break; default: $type = 'debug'; } //log the message: $this->_log[] = array('date' => date('Y-m-d H:i:s'), 'type' => $type, 'message' => $message ); } function getLog() { return $this->_log; } /** * Initialize the email data. */ function _clear($clearAttachments = false) { $this->_subject = ''; $this->_body = ''; $this->_finalBody = ''; $this->_headerStr = ''; $this->_replytoFlag = false; $this->_recipients = array(); $this->_headers = array(); $this->_debugMsg = array(); $this->_setHeader('User-Agent', $this->_userAgent); $this->_setHeader('Date', $this->_setDate()); if ($clearAttachments !== false) { $this->_attachName = array(); $this->_attachType = array(); $this->_attachDisp = array(); } } /** * Set the From address. */ function from($from, $name = '') { //set the from address if contained in brackets (e.g. ""): if (preg_match( '/\<(.*)\>/', $from, $match)) { $from = $match['1']; } //validate the email address if needed: if ($this->_validate) { $this->_validateEmail($this->_strToArray($from)); } //prepare the display name: if ($name != '') { //only use Q Encoding if there are characters that require it: if (!preg_match('/[\200-\377]/', $name)) { //add slashes for non-printing characters, slashes and double quotes, //and surround them in double quotes: $name = '"' . addcslashes($name, "\0..\37\177'\"\\") . '"'; } else { $name = $this->_prepQEncoding($name, true); } } $this->_setHeader("From", "{$name} <{$from}>"); if( !isset($this->_headers['Return-Path']) ) // mchallis added settable Return-Path $this->_setHeader("Return-Path", "<{$from}>"); } /** * Set the Reply-to address. */ function _replyTo($replyTo, $name = '') { //set the reply-to address if contained in brackets (e.g. ""): if (preg_match( '/\<(.*)\>/', $replyTo, $match)) { $replyTo = $match['1']; } //validate the email address if needed: if ($this->_validate) { $this->_validateEmail($this->_strToArray($replyTo)); } //prepare the display name: if ($name == '') { $name = $replyTo; } if (strncmp($name, '"', 1) != 0) { $name = '"' . $name . '"'; } $this->_setHeader("Reply-To", "{$name} <{$replyTo}>"); $this->_replytoFlag = true; } /** * Set the Recipient(s). */ function to($to) { $to = $this->_strToArray($to); $to = $this->_cleanEmail($to); //validate the email address(es) if needed: if ($this->_validate) { $this->_validateEmail($to); } if ($this->_getProtocol() != 'mail') { $this->_setHeader('To', implode(", ", $to)); } switch ($this->_getProtocol()) { case 'smtp': $this->_recipients = $to; break; case 'sendmail': $this->_recipients = implode(", ", $to); break; case 'mail': $this->_recipients = implode(", ", $to); break; } } /** * Set the CC(s). */ function cc($cc) { $cc = $this->_strToArray($cc); $cc = $this->_cleanEmail($cc); //validate the email address(es) if needed: if ($this->_validate) { $this->_validateEmail($cc); } $this->_setHeader('Cc', implode(", ", $cc)); if ($this->_getProtocol() == 'smtp') { $this->_ccArray = $cc; } } /** * Set the BCC(s). */ function bcc($bcc, $limit = '') { //should a batch limit be set? if (($limit != '') && (is_numeric($limit))) { $this->_bccBatchMode = true; $this->_bccBatchSize = $limit; } $bcc = $this->_strToArray($bcc); $bcc = $this->_cleanEmail($bcc); if ($this->_validate) { $this->_validateEmail($bcc); } if (($this->_getProtocol() == 'smtp') or (($this->_bccBatchMode) && ((count($bcc)) > $this->_bccBatchSize))) { $this->_bccArray = $bcc; } else { $this->_setHeader('Bcc', implode(", ", $bcc)); } } /** * Set the Subject. */ function subject($subject) { $subject = $this->_prepQEncoding($subject); $this->_setHeader('Subject', $subject); } /** * Set the X-Sender. mchallis added */ function x_sender($sender) { $this->_setHeader('X-Sender', $sender); } /** * Set the Return-Path. mchallis added */ function return_path($sender) { $this->_setHeader('Return-Path', $sender); } /** * Set the Body. */ function message($body) { $this->_body = stripslashes(rtrim(str_replace("\r", "", $body))); } /** * Set the file attachment(s). */ function attach($filename, $disposition = 'attachment'/*'inline'*/) { $this->_attachName[] = $filename; $this->_attachType[] = $this->_mimeTypes(next(explode('.', basename($filename)))); $this->_attachDisp[] = $disposition; } /** * Add a Header item. */ function _setHeader($header, $value) { $this->_headers[$header] = $value; } /** * Convert a String to an Array. */ function _strToArray($email) { if (!is_array($email)) { if (strpos($email, ',') !== false) { $email = preg_split('/[\s,]/', $email, -1, PREG_SPLIT_NO_EMPTY); } else { $email = trim($email); settype($email, 'array'); } } return $email; } /** * Set the Multipart Value. */ function _setaltMessage($str = '') { $this->_altMessage = ($str == '') ? '' : $str; } /** * Set the Mailtype. */ function _setmailtype($type = 'text') { $this->_mailType = ($type == 'html') ? 'html' : 'text'; } function setMailType($type = 'text') { $this->_mailType = ($type == 'html') ? 'html' : 'text'; } /** * Set Wordwrapping. */ function _setwordwrap($wordwrap = true) { $this->_wordWrap = ($wordwrap === false) ? false : true; } /** * Set the Protocol. */ function _setprotocol($protocol = 'mail') { $this->_protocol = (!in_array($protocol, $this->_protocols, true)) ? 'mail' : strtolower($protocol); } /** * Set the Priority. */ function _setpriority($n = 3) { if (!is_numeric($n)) { $this->_priority = 3; return; } if (($n < 1) or ($n > 5)) { $this->_priority = 3; return; } $this->_priority = $n; } /** * Set the Newline Character. */ function _setnewLine($newLine = "\n") { if (($newLine != "\n") and ($newLine != "\r\n") and ($newLine != "\r")) { $this->_newLine = "\n"; return; } $this->_newLine = $newLine; } /** * Set the charSet. */ function _setcharSet($charSet = "utf-8") { $this->_charSet = $charSet; } /** * Set CRLF. */ function _setcrlf($crlf = "\n") { if (($crlf != "\n") and ($crlf != "\r\n") and ($crlf != "\r")) { $this->_crlf = "\n"; return; } $this->_crlf = $crlf; } /** * Set the Message Boundary. */ function _setBoundaries() { //set the multipart/alternative boundary: $this->_altBoundary = "B_ALT_".uniqid(''); //set the attachment boundary: $this->_atcBoundary = "B_ATC_".uniqid(''); // attachment boundary } /** * Get the Message ID. */ function _getMessageId() { $from = $this->_headers['Return-Path']; $from = str_replace('>', '', $from); $from = str_replace('<', '', $from); return '<' . uniqid('') . strstr($from, '@') . '>'; } /** * Get the Mail Protocol */ function _getProtocol($return = true) { $this->_protocol = strtolower($this->_protocol); $this->_protocol = (!in_array($this->_protocol, $this->_protocols, true)) ? 'mail' : $this->_protocol; if ($return == true) { return $this->_protocol; } } /** * Get the Mail Encoding. */ function _getEncoding($return = true) { $this->_encoding = (!in_array($this->_encoding, $this->_bitDepths)) ? '8bit' : $this->_encoding; foreach ($this->_baseCharsets as $charset) { if (strncmp($charset, $this->_charSet, strlen($charset)) == 0) { $this->_encoding = '7bit'; } } if ($return == true) { return $this->_encoding; } } /** * Get the content type (text / html / attachment). */ function _getContentType() { if (($this->_mailType == 'html') && (count($this->_attachName) == 0)) { return 'html'; } else if (($this->_mailType == 'html') && (count($this->_attachName) > 0)) { return 'html-attach'; } else if (($this->_mailType == 'text') && (count($this->_attachName) > 0)) { return 'plain-attach'; } else { return 'plain'; } } /** * Set RFC 822 Date. */ function _setDate() { $timezone = date('Z'); $operator = (strncmp($timezone, '-', 1) == 0) ? '-' : '+'; $timezone = abs($timezone); $timezone = floor($timezone/3600) * 100 + ($timezone % 3600 ) / 60; return sprintf("%s %s%04d", date("D, j M Y H:i:s"), $operator, $timezone); } /** * Get a Mime message. */ function _getMimeMessage() { return "This is a multi-part message in MIME format." . $this->_newLine . "Your email application may not support this format."; } /** * Validate an array of email addresses. */ function _validateEmail($email) { if (!is_array($email)) { $this->_setErrorMessage('email_must_be_array'); return false; } foreach ($email as $val) { if (!$this->_validEmail($val)) { $this->_setErrorMessage('email_invalid_address', $val); return false; } } return true; } /** * Email validation. */ function _validEmail($address) { return (!preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $address)) ? false : true; } /** * Clean extended email address (e.g. "Willem van Zyl "). */ function _cleanEmail($email) { if (!is_array($email)) { if (preg_match('/\<(.*)\>/', $email, $match)) { return $match['1']; } else { return $email; } } $cleanEmail = array(); foreach ($email as $address) { if (preg_match( '/\<(.*)\>/', $address, $match)) { $cleanEmail[] = $match['1']; } else { $cleanEmail[] = $address; } } return $cleanEmail; } /** * Build alternative plain-text message by stripping HTML if no text version was supplied. */ function _getAltMessage() { if ($this->_altMessage != '') { return $this->_wordWrap($this->_altMessage, '76'); } if (preg_match('/\(.*)\<\/body\>/si', $this->_body, $match)) { $body = $match['1']; } else { $body = $this->_body; } $body = trim(strip_tags($body)); $body = preg_replace( '#