Mail Class

Discussion in 'PHP' started by Danltn, Feb 1, 2008.

  1. #1
    <?php
    
    /**
     * Danltn | http://danltn.com/
     * No warranty is given to code used
     */
    
    /* For testing purposes */
    error_reporting(E_ALL);
    
    class email
    {
        private $to;
        private $from;
        private $cc;
        private $bcc;
        private $subject;
        private $raw_headers = array();
        private $headers;
        private $message;
    
        public function __construct($short = false, $to = '', $from = '', $cc = '', $bcc = '', $subject = '', $message = '', $headers = '')
        {
            if ($short)
            {
                $this->to($to);
                $this->from($from);
                $this->cc($cc);
                $this->bcc($bcc);
                $this->subject($subject);
                $this->message($message);
                $this->header($header);
            }
        }
    
        public function reset($what = '')
        {
            $what = strtolower($what);
            if (!$what)
            {
                $what = 'all';
                /* We just do this to make PHP happier. */
            }
            switch ($what)
            {
                case 'to':
                    $this->to = null;
                    break;
    
                case 'cc':
                    $this->cc = null;
                    break;
                    
                case 'bcc':
                    $this->bcc = null;
                    break;
                    
            	case 'from':
            		$this->from = null;
            		break;
            		
            	case 'message':
            		$this->message = null;
            		break;
    
                case 'subject':
                    $this->subject = null;
                    break;
    
                case 'headers':
                    $this->headers = null;
                    break;
    
                case 'raw_headers':
                    $this->raw_headers = array();
                    break;
    
                case 'all headers':
                    $this->raw_headers = array();
                    $this->headers = null;
                    break;
    
                default:
                    $this->to = null;
                    $this->from = null;
                    $this->cc = null;
                    $this->bcc = null;
                    $this->subject = null;
                    $this->message = null;
                    $this->raw_headers = array();
                    $this->headers = null;
            }
            return $this;
        }
    
        public function to($to = '')
        {
            if (!$to)
            {
                return $this;
            }
            if (is_array($to))
            {
                $to = implode(', ', $to);
            }
            $to = str_replace("\n", '', $to);
            $to = trim($to);
            $this->to = $to;
            return $this;
        }
    
        public function from($from = '')
        {
            if (!$from)
            {
                return $this;
            }
            if (is_array($from))
            {
                $from = implode(', ', $from);
            }
            $from = trim($from);
            $this->header($from, 'from');
            $this->from = $from;
            return $this;
        }
    
        public function cc($cc = '')
        {
            if (!$cc)
            {
                return $this;
            }
            if (is_array($cc))
            {
                $cc = implode(', ', $cc);
            }
            $cc = trim($cc);
            $this->cc = $cc;
            return $this;
        }
    
        public function bcc($bcc = '')
        {
            if (!$bcc)
            {
                return $this;
            }
            if (is_array($bcc))
            {
                $bcc = implode(', ', $bcc);
            }
            $this->bcc = $bcc;
            return $this;
        }
    
        public function subject($subject = '', $force70 = 1)
        {
            /* Some things choke if the subject is more than 70 chars. */
            if (!$subject)
            {
                return $this;
            }
            if ($force70 == false)
            {
                $subject = substr($subject, 0, 70);
            }
            $subject = str_replace("\n", ' ', $subject);
            $this->subject = $subject;
            return $this;
        }
    
        public function message($message = '', $wordwrap = 0, $htmlspecialchars = 0)
        {
            if (!$message)
            {
                return $this;
            }
            if ($wordwrap)
            {
                $message = wordwrap($message, 70, "\n");
            }
            if ($htmlspecialchars)
            {
                $message = htmlspecialchars($message);
            }
            $this->message = $message;
            return $this;
        }
    
        private function ss($text)
        {
            if (get_magic_quotes_gpc())
            {
                return stripslashes($text);
            }
            else
            {
                return $text;
            }
        }
    
        public function html_mail($ar = true)
        {
            if ($ar)
            {
                $this->header('MIME-Version: 1.0', false);
                $this->header('Content-type: text/html; charset=iso-8859-1', false);
            }
            else
            {
    
                foreach ($this->raw_headers as & $header)
                {
                    if ($header == 'MIME-Version: 1.0' or $header == 'Content-type: text/html; charset=iso-8859-1')
                    {
                        unset($header);
                    }
                }
    
            }
            return $this;
        }
    
        public function header($header = '', $special = false)
        {
            if (!$header)
            {
                return $this;
            }
            if(is_array($header))
            {
    			foreach($header as $h)
    			{
    				$this->header($h, false, false);
    			}
    			return $this;
    		}
            if ($special == true)
            {
                $special = str_replace(' ', '', str_replace('-', ' ', strtolower($special)));
                switch ($special)
                {
                    case 'from':
                        $this->header("From: $header", false);
                        break;
    
                    case 'reply to':
                        $this->header("Reply-To: $header", false);
                        break;
    
                    case 'bcc':
                        $this->header("Bcc: $header", false);
                        break;
    
                    case 'cc':
                        $this->header("Cc: $header", false);
                        break;
    
                    case 'x mailer':
                        $this->header("X-Mailer: $header", false);
                        break;
                }
            }
            else
            {	
            	$header = str_replace("\n", '', $header);
                $this->raw_headers[] = $header;
                return $this;
            }
        }
    
        private function gen_header()
        {
            if (!$this->raw_headers)
            {
                return $this;
            }
            else
            {
                $this->headers = '';
                if (!defined('CRLF'))
                {
                    define('CRLF', " \r\n");
                }
                foreach ($this->raw_headers as $header)
                {
                    $this->headers .= $header . CRLF;
                }
            }
            return $this;
        }
    
        public function send()
        {
            $this->gen_header();
            $s = mail($this->to, $this->subject, $this->message, $this->headers);
            if ($s)
            {
            	/* This was just here for testing, you can remove it if need be. */
                echo "Mail successfully sent to {$this->to}.";
                return true;
            }
            else
            {
                return false;
            }
        }
    }
    
    ?>
    PHP:
    PHP 5 only.
    Suggested usage via Method Chaining:
    <?php
    
    $mail = new email;
    
    $mail->html_mail()->to('daniel.neville@gmail.com')->subject('Hello')->message('Hello there <b>Daniel Neville</b>. How are you?')->from('daniel@neville.tk')->header(array("Blah: Hello", "There: You are"))->send();
    
    $mail->reset();
    
    $mail->to('daniel.neville@gmail.com')->subject('Hello')->message('Hello there <b>Daniel Neville</b>. This should not be bold')->from('daniel@neville.tk')->send();
    
    ?>
    PHP:
    http://danltn.com/bin/70q.phps

    Constructive criticism and feedback are more than welcome. :hehe:

    I'd love to know your opinions, so please tell me them!

    Reply back if you need any help.

    Dan
     
    Danltn, Feb 1, 2008 IP
  2. jorgy

    jorgy Peon

    Messages:
    611
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Nice of you to share!
     
    jorgy, Feb 1, 2008 IP
  3. xxKillswitch

    xxKillswitch Peon

    Messages:
    331
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I love being able to private and protect things. Thanks for sharing, looks well constructed.
     
    xxKillswitch, Feb 1, 2008 IP
  4. Kaizoku

    Kaizoku Well-Known Member

    Messages:
    1,261
    Likes Received:
    20
    Best Answers:
    1
    Trophy Points:
    105
    #4
    I would suggest you code reset() function like this :)
    Basically, just set default value for $what as 'all', so that you don't need to do detections afterwards.

    
        public function reset($what = 'all')
        {
            $what = strtolower($what);
            switch ($what)
            {
                case 'to':
                    $this->to = null;
                    break;
    
                case 'cc':
                    $this->cc = null;
                    break;
                    
                case 'bcc':
                    $this->bcc = null;
                    break;
                    
                case 'from':
                    $this->from = null;
                    break;
                    
                case 'message':
                    $this->message = null;
                    break;
    
                case 'subject':
                    $this->subject = null;
                    break;
    
                case 'headers':
                    $this->headers = null;
                    break;
    
                case 'raw_headers':
                    $this->raw_headers = array();
                    break;
    
                case 'all headers':
                    $this->raw_headers = array();
                    $this->headers = null;
                    break;
    
                default:
                    $this->to = null;
                    $this->from = null;
                    $this->cc = null;
                    $this->bcc = null;
                    $this->subject = null;
                    $this->message = null;
                    $this->raw_headers = array();
                    $this->headers = null;
            }
            return $this;
        }
    
    PHP:
     
    Kaizoku, Feb 2, 2008 IP
  5. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #5
    Kaizoku:

    No idea why I didn't do this, I only added the if(!$what) part in my distribution part, so I didn't fully think it through, it isn't even there on my personal copy.

    Thanks for the feedback however, and I hope it helps you in the future!

    Dan
     
    Danltn, Feb 2, 2008 IP
  6. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #6
    You can also access the object properties dynamically like this:
    
    $this->$what = null;
    
    PHP:
    This way you could shrink the switch() a bit or remove it at all.
     
    nico_swd, Feb 2, 2008 IP