TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 02-02-2008, 10:46 AM   #1 (permalink)
The Wanderer
 
Join Date: Jan 2008
Location: Nottingham
Posts: 7
Thanks: 1
Daniel is on a distinguished road
Default Mail Class

PHP Code:
<?php

/**
 * Danltn | http://danltn.com/
 * No warranty is given to code used
 * Under creative commons license:
 * http://creativecommons.org/licenses/by-nc-sa/2.0/uk/
 * Attribution-Non-Commercial-Share Alike 2.0 UK: England & Wales
 */

/* 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 '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;
    }

    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($subject070);
        }
        
$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($message70"\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($hfalsefalse);
            }
            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 5 only.
Suggested usage via Method Chaining:
PHP Code:
<?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();

?>
http://danltn.com/bin/o0q.phps

Constructive critiscism 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
Send a message via MSN to Daniel
Daniel is offline  
Reply With Quote
The Following 2 Users Say Thank You to Daniel For This Useful Post:
Alan @ CIT (02-09-2008), ETbyrne (07-04-2008)
Old 02-02-2008, 03:40 PM   #2 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

Only took a quick look though, but it looks good. Unlike a lot of OOP scripts that have been posted here, it is using OOP for something it should be used for.
__________________

Village Idiot is offline  
Reply With Quote
Old 02-02-2008, 04:35 PM   #3 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default

Looks simple, yet good. :D
__________________
VillageIdiot can have my babbies ;d
Orc is offline  
Reply With Quote
Old 02-02-2008, 06:21 PM   #4 (permalink)
The Acquainted
Inquisitive 
 
WinSrev's Avatar
 
Join Date: Sep 2007
Posts: 133
Thanks: 6
WinSrev is on a distinguished road
Default

Whats the advantage of 100+ lines of code over just doing the mail(); function?
Send a message via ICQ to WinSrev
WinSrev is offline  
Reply With Quote
Old 02-02-2008, 07:18 PM   #5 (permalink)
The Wanderer
 
Join Date: Jan 2008
Location: Nottingham
Posts: 7
Thanks: 1
Daniel is on a distinguished road
Default

Quote:
Originally Posted by WinSrev View Post
Whats the advantage of 100+ lines of code over just doing the mail(); function?
The simple answer is, there isn't a definitive reason to use this, other than convenience, and not having to remember how to set all the individual options each time. If you're fully comfortable with using mail(); then you might a well use it. But if you're less confident, or need a simpler option, this is it.

This will soon be expanded for attachments too.

Dan.
Send a message via MSN to Daniel
Daniel is offline  
Reply With Quote
Old 02-02-2008, 11:40 PM   #6 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

Quote:
Originally Posted by WinSrev View Post
Whats the advantage of 100+ lines of code over just doing the mail(); function?
Security and accessibility, its easier and safer to use that function.
__________________

Village Idiot is offline  
Reply With Quote
Old 10-31-2008, 03:38 PM   #7 (permalink)
The Wanderer
 
Join Date: Oct 2008
Posts: 9
Thanks: 0
kjarli is on a distinguished road
Default

Quote:
Originally Posted by Village Idiot View Post
Security and accessibility, its easier and safer to use that function.
No... It's not by definition easier or safer... Check out swift mailer, uses smtp. Much better than either one.

For those who want to know, there is soooo much not good about mail(), that most smtp scripts are better than it ;)
kjarli is offline  
Reply With Quote
Old 02-03-2008, 01:28 AM   #8 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

A nice start, and good to see you keeping the class simple and to the point. The actual number of lines could be whittled down with some rewriting (look where you're repeating yourself).

I'm not a fan of the whole 'reset' idea. Best, in my opinion, keeping two different email messages separate from each other. I'd perhaps also throw in a static method rather than having to create at least one instance of the class with a global variable: email::instance()->to('blah...
Salathe is offline  
Reply With Quote
Old 03-24-2008, 09:16 AM   #9 (permalink)
The Contributor
 
marxx's Avatar
 
Join Date: Sep 2007
Location: Finland
Posts: 45
Thanks: 3
marxx is on a distinguished road
Default

Very nice class! Will use this.. =)
Send a message via MSN to marxx
marxx is offline  
Reply With Quote
Old 03-24-2008, 11:28 PM   #10 (permalink)
how quixotic are you?
 
ETbyrne's Avatar
 
Join Date: Dec 2007
Location: Lapeer, MI
Posts: 445
Thanks: 37
ETbyrne is on a distinguished road
Default

Normally I dislike it when people bump threads but I never saw this before, so thank you marxx! And thank you David for a great mail class.
__________________
Dingo Web Systems > http://www.dingocode.com
My Website > http://www.evanbot.com
ETbyrne is offline  
Reply With Quote
Old 05-14-2008, 10:35 AM   #11 (permalink)
Jim
The Addict
 
Jim's Avatar
 
Join Date: Nov 2007
Location: the Netherlands
Posts: 281
Thanks: 2
Jim is on a distinguished road
Default

Would be nice if you can specify your own SMTP server. Very simple to add :)
__________________
Nunchaku! Who doesn't like martial arts? =)
Send a message via MSN to Jim Send a message via Skype™ to Jim
Jim is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 07:42 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design