Location: PHPKode > projects > Moc10 PHP Library > library/Moc10/Mail.php
<?php
/**
 * Moc10 Library
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.TXT.
 * It is also available through the world-wide-web at this URL:
 * http://www.moc10phplibrary.com/LICENSE.TXT
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to hide@address.com so we can send you a copy immediately.
 *
 * @category   Moc10
 * @package    Moc10_Mail
 * @author     Nick Sagona, III <hide@address.com>
 * @copyright  Copyright (c) 2009-2011 Moc 10 Media, LLC. (http://www.moc10media.com)
 * @license    http://www.moc10phplibrary.com/LICENSE.TXT     New BSD License
 */

/**
 * Moc10_Mail
 *
 * @category   Moc10
 * @package    Moc10_Mail
 * @author     Nick Sagona, III <hide@address.com>
 * @copyright  Copyright (c) 2009-2011 Moc 10 Media, LLC. (http://www.moc10media.com)
 * @license    http://www.moc10phplibrary.com/LICENSE.TXT     New BSD License
 * @version    1.9.7
 */

class Moc10_Mail
{

    /**
     * Sending queue
     * @var array
     */
    protected $_queue = array();

    /**
     * Subject
     * @var string
     */
    protected $_subject = null;

    /**
     * Message body
     * @var string
     */
    protected $_message = null;

    /**
     * Text part of the message body
     * @var string
     */
    protected $_text = null;

    /**
     * HTML part of the message body
     * @var string
     */
    protected $_html = null;

    /**
     * Headers
     * @var string
     */
    protected $_headers = null;

    /**
     * Character set
     * @var string
     */
    protected $_charset = 'utf-8';

    /**
     * MIME boundary
     * @var string
     */
    protected $_mime_boundary = null;

    /**
     * File attachments
     * @var array
     */
    protected $_attachments = array();

    /**
     * Language object
     * @var Moc10_Language
     */
    protected $_lang = null;

    /**
     * Constructor
     *
     * Instantiate the mail object.
     *
     * @param  string|array $em
     * @param  string       $nm
     * @param  string       $subj
     * @throws Exception
     * @return void
     */
    public function __construct($em, $nm = null, $subj = null)
    {

        $this->_lang = new Moc10_Language();

        $this->_subject = $subj;

        // If the email parameter passed is an array, set accordingly.
        if (is_array($em)) {

            foreach ($em as $value) {
                if (is_array($value)) {
                    if (!array_key_exists('email', $value)) {
                        throw new Exception($this->_lang->__("Error: At least one of the array keys must be 'email'."));
                    } else {
                        $this->_queue[] = $value;
                    }
                } else {
                    $this->_queue[] = array('email' => $value);
                }

            }

        // Else, set the single email value.
        } else {
            if (!is_null($nm)) {
                $this->_queue[] = array('name' => $nm, 'email' => $em);
            } else {
                $this->_queue[] = array('email' => $em);
            }
        }

    }

    /**
     * Set the subject
     *
     * @param  string $subj
     * @return void
     */
    public function setSubject($subj)
    {

        $this->_subject = $subj;

    }

    /**
     * Get the subject
     *
     * @return string
     */
    public function getSubject()
    {

        return $this->_subject;

    }

    /**
     * Set MIME boundary
     *
     * @param  string $bnd
     * @return void
     */
    public function setBoundary($bnd = null)
    {

        $this->_mime_boundary = (!is_null($bnd)) ? $bnd : sha1(time());

    }

    /**
     * Get MIME boundary
     *
     * @return string
     */
    public function getBoundary()
    {

        return $this->_mime_boundary;

    }

    /**
     * Set character set
     *
     * @param  string $chr
     * @return void
     */
    public function setCharset($chr)
    {

        $this->_charset = $chr;

    }

    /**
     * Get character set
     *
     * @return string
     */
    public function getCharset()
    {

        return $this->_charset;

    }

    /**
     * Set text part of the message.
     *
     * @param  string $txt
     * @return void
     */
    public function setText($txt)
    {

        $this->_text = $txt;

    }

    /**
     * Set HTML part of the message.
     *
     * @param  string $html
     * @return void
     */
    public function setHtml($html)
    {

        $this->_html = $html;

    }

    /**
     * Attach a file to the mail object.
     *
     * @param  string|Moc10_File $file
     * @throws Exception
     * @return void
     */
    public function attachFile($file)
    {

        // Determine if the file is valid.
        if (((is_string($file)) && (!file_exists($file))) && (!($file instanceof Moc10_File))) {
            throw new Exception($this->_lang->__('Error: The parameter passed must either be a valid file or an instance of Moc10_File.'));
        } else if (is_string($file)) {
            $fle = new Moc10_File($file);
        } else {
            $fle = $file;
        }

        // Encode the file contents and set the file into the attachments array property.
        $contents = chunk_split(base64_encode($fle->read()));
        $this->_attachments[] = array('file' => $fle, 'contents' => $contents);

    }

    /**
     * Set headers
     *
     * @param  string|array $hdrs
     * @return void
     */
    public function setHeaders($hdrs = null)
    {

        if (is_null($hdrs)) {
            $this->_headers = null;
        } else if (is_array($hdrs)) {
            foreach ($hdrs as $key => $value) {
                $this->_headers .= (is_array($value)) ? $key . ": " . $value[0] . " <" . $value[1] . ">\n" : $key . ": " . $value . "\n";
            }
        } else {
            $this->_headers .= $hdrs;
        }

    }

    /**
     * Initialize the email message.
     *
     * @throws Exception
     * @return void
     */
    public function init()
    {

        $msgType = $this->_getMessageType();

        if (is_null($msgType)) {
            throw new Exception($this->_lang->__('Error: The message body elements are not set.'));
        } else {

            $this->setBoundary();

            switch ($msgType) {

                // If the message contains files, HTML and text.
                case 'FILE|HTML|TEXT':
                    $this->_headers .= "MIME-Version: 1.0\nContent-Type: multipart/mixed; boundary=\"" . $this->getBoundary() . "\"\nThis is a multi-part message in MIME format.\n\n";
                    foreach ($this->_attachments as $file) {
                        $this->_message .= "\n--" . $this->getBoundary() . "\nContent-Type: file; name=\"" . $file['file']->basename . "\"\nContent-Transfer-Encoding: base64\nContent-Description: " . $file['file']->basename . "\nContent-Disposition: attachment; filename=\"" . $file['file']->basename . "\"\n\n" . $file['contents'] . "\n\n";
                    }
                    $this->_message .= "--" . $this->getBoundary() . "\nContent-type: text/html; charset=" . $this->getCharset() . "\n\n" . $this->_html . "\n\n";
                    $this->_message .= "--" . $this->getBoundary() . "\nContent-type: text/plain; charset=" . $this->getCharset() . "\n\n" . $this->_text . "\n\n--" . $this->getBoundary() . "--\n\n";
                    break;

                    break;

                // If the message contains files and HTML.
                case 'FILE|HTML':
                    $this->_headers .= "MIME-Version: 1.0\nContent-Type: multipart/mixed; boundary=\"" . $this->getBoundary() . "\"\nThis is a multi-part message in MIME format.\n\n";
                    foreach ($this->_attachments as $file) {
                        $this->_message .= "\n--" . $this->getBoundary() . "\nContent-Type: file; name=\"" . $file['file']->basename . "\"\nContent-Transfer-Encoding: base64\nContent-Description: " . $file['file']->basename . "\nContent-Disposition: attachment; filename=\"" . $file['file']->basename . "\"\n\n" . $file['contents'] . "\n\n";
                    }
                    $this->_message .= "--" . $this->getBoundary() . "\nContent-type: text/html; charset=" . $this->getCharset() . "\n\n" . $this->_html . "\n\n--" . $this->getBoundary() . "--\n\n";
                    break;

                // If the message contains files and text.
                case 'FILE|TEXT':
                    $this->_headers .= "MIME-Version: 1.0\nContent-Type: multipart/mixed; boundary=\"" . $this->getBoundary() . "\"\nThis is a multi-part message in MIME format.\n\n";
                    foreach ($this->_attachments as $file) {
                        $this->_message .= "\n--" . $this->getBoundary() . "\nContent-Type: file; name=\"" . $file['file']->basename . "\"\nContent-Transfer-Encoding: base64\nContent-Description: " . $file['file']->basename . "\nContent-Disposition: attachment; filename=\"" . $file['file']->basename . "\"\n\n" . $file['contents'] . "\n\n";
                    }
                    $this->_message .= "--" . $this->getBoundary() . "\nContent-type: text/plain; charset=" . $this->getCharset() . "\n\n" . $this->_text . "\n\n--" . $this->getBoundary() . "--\n\n";
                    break;

                // If the message contains HTML and text.
                case 'HTML|TEXT':
                    $this->_headers .= "MIME-Version: 1.0\nContent-Type: multipart/alternative; boundary=\"" . $this->getBoundary() . "\"\nThis is a multi-part message in MIME format.\n\n";
                    $this->_message .= "--" . $this->getBoundary() . "\nContent-type: text/plain; charset=" . $this->getCharset() . "\n\n" . $this->_text . "\n\n";
                    $this->_message .= "--" . $this->getBoundary() . "\nContent-type: text/html; charset=" . $this->getCharset() . "\n\n" . $this->_html . "\n\n--" . $this->getBoundary() . "--\n\n";
                    break;

                // If the message contains HTML.
                case 'HTML':
                    $this->_headers .= "MIME-Version: 1.0\nContent-Type: multipart/alternative; boundary=\"" . $this->getBoundary() . "\"\nThis is a multi-part message in MIME format.\n\n";
                    $this->_message .= "--" . $this->getBoundary() . "\nContent-type: text/html; charset=" . $this->getCharset() . "\n\n" . $this->_html . "\n\n--" . $this->getBoundary() . "--\n\n";
                    break;

                // If the message contains text.
                case 'TEXT':
                    $this->_headers .= "Content-Type: text/plain; charset=" . $this->getCharset() . "\n";
                    $this->_message = $this->_text . "\n";
                    break;

            }

        }

    }

    /**
     * Send mail message or messages.
     *
     * This method depends on the server being set up correctly as an SMTP server
     * and sendmail being correctly defined in the php.ini file.
     *
     * @return void
     */
    public function send()
    {

        // Iterate through the queue and send the mail messages.
        foreach ($this->_queue as $rcpt) {

            $subject = $this->_subject;
            $message = $this->_message;

            // Set the recipient parameter.
            $to = (isset($rcpt['name'])) ? $rcpt['name'] . " <" . $rcpt['email'] . ">" : $rcpt['email'];

            // Replace any set placeholder content within the subject or message.
            foreach ($rcpt as $key => $value) {
                $subject =  str_replace('[{' . $key . '}]', $value, $subject);
                $message =  str_replace('[{' . $key . '}]', $value, $message);
            }

            // Send the email message.
            mail($to, $subject, $message, $this->_headers);

        }

    }

    /**
     * Get message type.
     *
     * @return string
     */
    protected function _getMessageType()
    {

        if ((count($this->_attachments) > 0) && is_null($this->_html) && is_null($this->_text)) {
            $type = null;
        } else if ((count($this->_attachments) > 0) && (!is_null($this->_html)) && (!is_null($this->_text))) {
            $type = 'FILE|HTML|TEXT';
        } else if ((count($this->_attachments) > 0) && (!is_null($this->_html))) {
            $type = 'FILE|HTML';
        } else if ((count($this->_attachments) > 0) && (!is_null($this->_text))) {
            $type = 'FILE|TEXT';
        } else if ((!is_null($this->_html)) && (!is_null($this->_text))) {
            $type = 'HTML|TEXT';
        } else if (!is_null($this->_html)) {
            $type = 'HTML';
        } else if (!is_null($this->_text)) {
            $type = 'TEXT';
        }

        return $type;

    }

}
Return current item: Moc10 PHP Library