/lib/mail/cfEMail.class.php
PHP | 610 lines | 235 code | 52 blank | 323 comment | 32 complexity | c0c3eaaa4d4fb0746f91a36e6e79eb84 MD5 | raw file
Possible License(s): LGPL-3.0
- <?php
- /**
- -----------------------------------------------------------------------------
- * eMAIL BASE SENDER
- *
- * Based on PEAR::Mail this class provides you to generate and send an email over sendmail or smtp.
- * @link http://pear.php.net/manual/en/package.mail.mail.intro.php
- * @link http://pear.php.net/manual/en/package.mail.mail-mime.php
- * @link http://www.faqs.org/rfcs/rfc822 RFC822
- *
- -----------------------------------------------------------------------------
- -----------------------------------------------------------------------------
- * @copyright (C) 2011 Cyberfox Software Solutions e.U.
- * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License version 3 (LGPLv3)
- * @author Christian Graf <christian.graf@cyberfox.at>
- -----------------------------------------------------------------------------
- -----------------------------------------------------------------------------
- * @package redfox
- * @subpackage messaging
- * @category communication
- -----------------------------------------------------------------------------
- -----------------------------------------------------------------------------
- * @version $Id: cfEMail.class.php 105 2012-06-28 06:55:45Z cgraf $
- * @date $Date: 2012-06-28 08:55:45 +0200 (Do, 28 Jun 2012) $
- * @svnauthor $Author: cgraf $
- -----------------------------------------------------------------------------
- */
- /**
- * Include the PEAR::Mail class to work with.
- * @link http://pear.php.net/manual/en/package.mail.mail.php
- */
- require_once('Mail.php'); //PEAR CLASS
- /**
- * Include the PEAR::Mail_mime class to work with.
- * @link http://pear.php.net/manual/en/package.mail.mail-mime.php
- */
- require_once('Mail/mime.php'); //PEAR CLASS
- class cfEMail
- {
- /**
- * CRLF = Carriage Return Line Feed
- *
- * @var char
- * @access public
- */
- const CRLF = "\n";
- /**
- * The current mime object
- * @link http://pear.php.net/manual/en/package.mail.mail-mime.php
- *
- * @var Mail_mime
- * @access private
- */
- private $mimeObj = null;
- /**
- * The current mailer object
- * @link http://pear.php.net/manual/en/package.mail.mail.php
- *
- * @var Mail
- * @access private
- */
- private $mailObj = null;
- /**
- * The current factory (sending type).
- * Only sendmail or smtp currently supported
- *
- * @var string
- * @access private
- */
- private $factory = null;
- /**
- * List of supported sending types.
- * Only sendmail or smtp currently supported
- *
- * @var array
- * @access private
- */
- private $availableFactories = array('sendmail', 'smtp', 'mail', 'null');
- /**
- * List of available recipients.
- * Add it with the methods addBcc() or addCc()
- *
- * @var array
- * @access private
- */
- private $recipients = array();
- /**
- * Constructor
- *
- * Creates a new instance from PEAR::Mail_mime
- * @link http://pear.php.net/manual/en/package.mail.mail-mime.php
- *
- * @return void
- * @access public
- */
- public function __construct()
- {
- $this->mimeObj = new Mail_mime(self::CRLF);
- $this->factory = 'sendmail';
- }
- /**
- * setFactory()
- *
- * Set the "sending" type of the email.
- * Only supported factories are allowed. Current there are sending via sendmail or smtp
- *
- * @return void
- * @access public
- */
- public function SetFactory($Factory)
- {
- if(!empty($Factory) && in_array(strtolower($Factory), $this->availableFactories) )
- $this->factory = strtolower($Factory);
- return;
- }
- /**
- * GetFactory()
- *
- * Returns the current set factory
- *
- * @return string The current set factory
- * @access public
- */
- public function GetFactory()
- {
- if(empty($this->factory)) return 'null';
- return $this->factory;
- }
- /**
- * getAvailableFactories()
- *
- * Returns the list of all supported email factories
- *
- * @return array Assoc array of all supported email factories
- * @access public
- */
- public function GetAvailableFactories()
- {
- if(empty($this->availableFactories)) return array('null');
- return $this->availableFactories;
- }
- /**
- * GetMimeObj()
- *
- * Get the current mime object as reference.
- * @link http://pear.php.net/manual/en/package.mail.mail-mime.mail-mime.php
- *
- * @return Mail_mime The current mime object as reference
- * @access public
- */
- public function &GetMimeObj()
- {
- return $this->mimeObj;
- }
- /**
- * GetMailObj()
- *
- * Get the current mailer object as reference.
- * @link http://pear.php.net/manual/en/package.mail.mail.factory.php
- *
- * @return Mail The current mailer object as reference.
- * @access public
- */
- public function &GetMailObj()
- {
- if(is_null($this->mailObj))
- {
- switch($this->GetFactory())
- {
- case 'smtp':
- {
- $settings = $this->GetSmtpSettings();
- $this->mailObj =& Mail::factory('smtp', $settings);
- }
- break;
- case 'sendmail':
- {
- $settings = $this->GetSendmailSettings();
- $this->mailObj =& Mail::factory('sendmail', $settings);
- }
- break;
- case 'mail':
- {
- $this->mailObj =& Mail::factory('mail', array());
- }
- default:
- {
- $this->mailObj =& Mail::factory('null', array());
- }
- break;
- }
- }
- return $this->mailObj;
- }
- /**
- * setFrom()
- *
- * Set the "from" part of the eMail.
- *
- * @param string $From The email address where the email is came from.
- * @return void
- * @access public
- */
- public function SetFrom($From)
- {
- $this->mimeObj->setFrom($From);
- }
- /**
- * addCc()
- *
- * Add an "carbon copy (cc)" receptient to the eMail.
- *
- * Wrapper for Mail_mime::addCc()
- * @link http://pear.php.net/package/Mail_Mime/docs/latest/Mail_Mime/Mail_mime.html#methodaddCc
- *
- * Note: No email syntax verification implemented yet.
- *
- * @param string $Email The email address of the receptient
- * @return void
- * @access public
- */
- public function AddCc($Email)
- {
- $this->mimeObj->addCc($Email);
- $this->recipients[] = $Email;
- }
- /**
- * addBcc()
- *
- * Add an "blind carbon copy (bcc)" receptient to the eMail.
- *
- * Wrapper for Mail_mime::addBcc()
- * @link http://pear.php.net/package/Mail_Mime/docs/latest/Mail_Mime/Mail_mime.html#methodaddBcc
- *
- * Note: No email syntax verification implemented yet.
- *
- * @param string $Email The email address of the receptient
- * @return void
- * @access public
- */
- public function AddBcc($Email)
- {
- $this->mimeObj->addBcc($Email);
- $this->recipients[] = $Email;
- }
- /**
- * setSubject()
- *
- * Set the "subject" part of the eMail.
- *
- * Wrapper for Mail_mime::setSubject()
- * @link http://pear.php.net/package/Mail_Mime/docs/latest/Mail_Mime/Mail_mime.html#methodsetSubject
- *
- * @param string $Subject The designated subject of the email
- * @return void
- * @access public
- */
- public function SetSubject($Subject)
- {
- $this->mimeObj->setSubject($Subject);
- }
- /**
- * setTextBody()
- *
- * Set the plain text part of the eMail.
- *
- * Wrapper for Mail_mime::setTXTBody()
- * @link http://pear.php.net/manual/en/package.mail.mail-mime.settxtbody.php
- *
- * If there is an error an exception will be thrown.
- *
- * @param string $Body The text to set or, if $Isfile is TRUE a valid filename. An URL as argument is not allowed.
- * @param bool $IsFile If TRUE, the content of given file $Body is used as message text.
- * @param bool $Append If TRUE, the content will be appended to the existing one.
- * @return bool TRUE if the body could be set successfully
- * @access public
- */
- public function SetTextBody($Body, $IsFile = false, $Append = false)
- {
- $status = $this->mimeObj->setTXTBody($Body, $IsFile, $Append);
- if(PEAR::isError($status))
- throw new cfException(__METHOD__ .': Could not set the mail body (text) => ' .$status->getMessage());
- return $status;
- }
- /**
- * setHtmlBody()
- *
- * Set the html part of the eMail
- *
- * Wrapper for Mail_mime::setHTMLBody()
- * @link http://pear.php.net/manual/en/package.mail.mail-mime.sethtmlbody.php
- *
- * @param string $Body The text to set or, if $Isfile is TRUE a valid filename. An URL as argument is not allowed.
- * @param bool $IsFile If TRUE, the content of given file $Body is used as message text.
- * @param bool $Append If TRUE, the content will be appended to the existing one.
- * @return bool TRUE if the body could be set successfully
- * @access public
- */
- public function SetHtmlBody($Body, $IsFile = false, $Append = false)
- {
- $status = $this->mimeObj->setHTMLBody($Body, $IsFile, $Append);
- if(PEAR::isError($status))
- throw new cfException(__METHOD__ .': Could not set the mail body (html) => ' .$status->getMessage());
- return $status;
- }
- /**
- * addHeaders()
- *
- * Build an array with the headers needed to prepend to the email (MIME-Version and Content-Type).
- *
- * Wrapper for Mail_mime::headers()
- * @link http://pear.php.net/manual/en/package.mail.mail-mime.headers.php
- *
- * @param array $Headers Additional headers, the format is $Headers["header-name"] = "header-value"
- * @param boolen $Override Overwrite already existing headers. When FALSE, the values already set are kept.
- * @return void
- * @access public
- */
- public function AddHeaders($Headers)
- {
- $this->mimeObj->headers($Headers, false);
- }
- /**
- * getHeaders()
- *
- * Returns an array with the headers needed to prepend to the email (MIME-Version and Content-Type).
- *
- * Wrapper for Mail_mime::headers()
- * @link http://pear.php.net/manual/en/package.mail.mail-mime.headers.php
- *
- * @return array The current defined header lines
- * @access public
- */
- public function GetHeaders()
- {
- return $this->mimeObj->headers();
- }
- /**
- * getPreparedHeaders()
- *
- * Returns an array with the headers needed to prepend to the email (MIME-Version and Content-Type).
- *
- * @return array The current defined header lines
- * @access public
- * @deprecated
- */
- public function GetPreparedHeaders($Headers = null)
- {
- if(empty($Headers)) $Headers = $this->getHeaders();
- $pHeaders = $this->GetMailObj()->prepareHeaders($Headers);
- if(!is_array($pHeaders) || sizeof($pHeaders) != 2)
- throw new cfException(__METHOD__ .': Could not prepare headers.');
- return $pHeaders;
- }
- /**
- * addAttachment()
- *
- * Adds a file to the list of attachments.
- *
- * Wrapper for Mail_mime::addAttachment()
- * @link http://pear.php.net/package/Mail_Mime/docs/latest/Mail_Mime/Mail_mime.html#methodaddAttachment
- *
- * @param string $File The file name of the file to attach OR the file contents itself
- * @param string $CType The content type. Defaults to "application/octet-stream"
- * @param string $Name The filename of the attachment. Only use if $File is the contents
- * @param bool $IsFile Whether $File is a filename or not. Defaults to true
- * @param string $Encoding The type of encoding to use. Defaults to base64. Possible values: 7bit, 8bit, base64 or quoted-printable.
- * @param string $Charset The character set used in the filename of this attachment.
- * @return bool Status
- * @access public
- */
- public function AddAttachment($File, $CType = 'application/octet-stream', $Name = '', $IsFile = true, $Encoding = 'base64', $Charset = '')
- {
- $status = $this->mimeObj->addAttachment($File, $CType, $Name, $IsFile, $Encoding, '', $Charset);
- if(PEAR::isError($status))
- throw new cfException(__METHOD__ .': Could not add attachment => ' .$status->getMessage());
- return $status;
- }
- /**
- * addHTMLImage()
- *
- * Adds an image to the list of embedded images.
- *
- * Wrapper for Mail_mime::addHTMLImage()
- * @link http://pear.php.net/package/Mail_Mime/docs/latest/Mail_Mime/Mail_mime.html#methodaddHTMLImage
- *
- * @param string $File The file name of the file to attach OR the file contents itself
- * @param string $CType The content type. Defaults to "application/octet-stream"
- * @param string $Name The filename of the attachment. Only use if $File is the contents
- * @param bool $IsFile Whether $File is a filename or not. Defaults to true
- * @return bool Status
- * @access public
- */
- public function AddHTMLImage($File, $CType = 'application/octet-stream', $Name = '', $IsFile = true)
- {
- $status = $this->mimeObj->addHTMLImage($File, $CType, $Name, $IsFile);
- if(PEAR::isError($status))
- throw new cfException(__METHOD__ .': Could not add HTML image => ' .$status->getMessage());
- return $status;
- }
- /**
- * GetSmtpSettings()
- *
- * Get SMPT settings from the config or set it to defaults to send via localhost.
- *
- * @return array Assoc array of the current defined settings
- * @access private
- */
- public function GetSmtpSettings()
- {
- $settings=array(
- 'host' => 'localhost',
- 'port' => 25,
- 'auth' => true,
- 'username' => '',
- 'password' => '',
- 'localhost' => 'localhost'
- );
- if(cfConfig::IsEntrySet('smtp_host', 'application'))
- {
- $settings['host'] = cfConfig::GetEntry('smtp_host', 'application');
- }
- if(cfConfig::IsEntrySet('smtp_port', 'application'))
- {
- $settings['port'] = cfConfig::GetEntry('smtp_port', 'application');
- }
- if(cfConfig::IsEntrySet('smtp_auth', 'application'))
- {
- $settings['auth'] = cfConfig::GetEntry('smtp_auth', 'application');
- }
- if(cfConfig::IsEntrySet('smtp_username', 'application'))
- {
- $settings['username'] = cfConfig::GetEntry('smtp_username', 'application');
- }
- if(cfConfig::IsEntrySet('smtp_password', 'application'))
- {
- $settings['password'] = cfConfig::GetEntry('smtp_password', 'application');
- }
- if(cfConfig::IsEntrySet('smtp_localhost', 'application'))
- {
- $settings['localhost'] = cfConfig::GetEntry('smtp_localhost', 'application');
- }
- if(empty($settings['username']) && empty($settings['password']) && ($settings['host'] == 'localhost' || $settings['host'] == '127.0.0.1'))
- {
- $settings['auth'] = false;
- }
- return $settings;
- }
- /**
- * GetSendmailSettings()
- *
- * Get sendmail settings from the config or set it to defaults.
- *
- * @return array Assoc array of the current defined settings
- * @access private
- */
- public function GetSendmailSettings()
- {
- $settings=array(
- 'sendmail_path' => '/usr/sbin/sendmail',
- 'sendmail_args' => '-i'
- );
- if(cfConfig::IsEntrySet('sendmail_path', 'application'))
- {
- $settings['sendmail_path'] = cfConfig::GetEntry('sendmail_path', 'application');
- }
- if(cfConfig::IsEntrySet('sendmail_args', 'application'))
- {
- $settings['sendmail_args'] = cfConfig::GetEntry('sendmail_args', 'application');
- }
- return $settings;
- }
- /**
- * getBody()
- *
- * Builds the multipart message and returns the mime content.
- *
- * Wrapper for Mail_mime::get()
- * @link http://pear.php.net/package/Mail_Mime/docs/latest/Mail_Mime/Mail_mime.html#methodget
- *
- * @return string The MIME message content string
- * @access public
- */
- public function GetBody()
- {
- $options = array(
- 'text_charset' => 'utf-8',
- 'html_charset' => 'utf-8',
- 'head_charset' => 'utf-8'
- );
- return $this->GetMimeObj()->get($options);
- }
- /**
- * After_send()
- *
- * OVERLOAD ME: Here you are able to put some additional actions after sending a mail.
- *
- * @param bool $Status Send status
- * @param Mail $MailObj The current mailer object
- * @param array $Options Additional options
- * @return void
- * @access protected
- */
- protected function After_send($Status, &$MailObj, $Options = array())
- {
- return;
- }
- /**
- * send()
- *
- * Send the eMail.
- *
- * If a error occured, an exception will be thrown.
- *
- * @param string $To eMail address of the receiptient. Format name@domain.com or "My Name <name@domain.com>"
- * @param string $Factory Send over sendmail or smtp
- * @param array $Options (optional) Additional options
- * @return bool Send status
- * @access public
- */
- public function Send($To, $Factory = null, $Options = array())
- {
- if(empty($To))
- throw new cfException(__METHOD__ .': Could not send the email => Invalid eMail address "' .$To .'"');
- if(!empty($Factory))
- $this->SetFactory(trim($Factory));
- $returnTo = '';
- $replyTo = '';
- if(isset($Options['return_to']))
- {
- $returnTo = $Options['return_to'];
- }
- if(isset($Options['reply_to']))
- {
- $replyTo = $Options['reply_to'];
- }
- $this->AddHeaders(array('Return-Path' => $returnTo, 'Reply-To' => $replyTo, 'To' => $To));
- $mailObj =& $this->GetMailObj();
- //SEND MAIL
- $recipients = array();
- $recipients = explode(',', $To);
- $recipients = array_merge($recipients, $this->recipients);
- $eMailBody = $this->GetBody();
- $eMailHeaders = $this->GetHeaders();
- cfLogger::Log('cfFramework')->Info('Try to send a email to: ' .print_r($recipients, true));
- $status = $mailObj->Send($recipients, $eMailHeaders, $eMailBody, $Options);
- if(PEAR::isError($status))
- {
- cfLogger::Log('cfFramework')->Error('Unable to send the email => ' .$status->getMessage());
- throw new cfException(__METHOD__ .': Unable to send the email => ' .$status->getMessage());
- }
- else
- cfLogger::Log('cfFramework')->Info('Send-Status: ' .print_r($status, true));
- $this->After_send($status, $this, $Options); //for additional actions
- return $status;
- }
- }
- ?>