Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3

Thread: php form

  1. #1
    New Coder
    Join Date
    Apr 2006
    Posts
    37
    Thanks
    2
    Thanked 0 Times in 0 Posts

    php form

    i have this php form, and once the form is submitted, i want it to send to 2 email addresses instead of just one.

    Ive tried separating with commas, semi-colons and adding the next email address.


    I even tried adding the 'cc', but i obviously did it wrong cos it didnt work.

    Can someone please help me with this.

    Here's the code

    PHP Code:
    <?php
    class SimpleContact {

        var 
    $state// 0 = fail, 1 = ready, 2 = attempted injection attack, 3 = mail sent
        
    var $errorMsg// human readable version of $this->state with specific detail of error's cause

        
    var $_to;
        var 
    $_from;
        var 
    $_subject;
        var 
    $_msg;

        function 
    _isEmail($email) {
            if (
    preg_match("#^(([A-Za-z0-9]+_+)|([A-Za-z0-9]+\-+)|([A-Za-z0-9]+\.+)|([A-Za-z0-9]+\++))*[A-Za-z0-9]+@((\w+\-+)|(\w+\.))*\w{1,63}\.[a-zA-Z]{2,6}$#"$email))
            { return 
    true; }
            else
            { return 
    false; }
        }

        function 
    _isInjection($text) {
            
    $text strtolower($text);
            if (
    preg_match('#(content\s*-\s*disposition)|(bcc\:)|(cc\:)|(content\s*-\s*transfer\s*-\s*encoding)|(mime\s*-\s*version)|(multipart\s*/\s*mixed)|(multipart\s*/\s*alternative)|(multipart\s*/\s*related)|(reply\s*-\s*to)|(x\s*-\s*mailer)|(x\s*-\s*sender)|(x\s*-\s*uidl)#is',$text))
            { return 
    true; }
            else
            { return 
    false;}
        }

        function 
    init($to$from$name$subject$msg) {
            
    // start with least expensive process, end with regex madness.
            
    if( empty($name) ) {
                
    $this->state 0;
                
    $this->errorMsg 'Please fill in your name';
                return 
    false;
            }
            if( empty(
    $subject) ) {
                
    $this->state 0;
                
    $this->errorMsg 'All mail must contain a subject';
                return 
    false;
            }
            if( empty(
    $msg) ) {
                
    $this->state 0;
                
    $this->errorMsg 'All mail must contain a message body';
                return 
    false;
            }
            if( empty(
    $from) || !$this->_isEmail($from) ) {
                
    $this->state 0;
                
    $this->errorMsg 'Invalid sender email address supplied';
                return 
    false;
            }
            if( empty(
    $to) || !$this->_isEmail($to) ) {
                
    $this->state 0;
                
    $this->errorMsg 'Invalid recipient email address supplied';
                return 
    false;
            }
            if( 
    $this->_isInjection($to) || $this->_isInjection($name) || $this->_isInjection($from) ||  $this->_isInjection($subject) || $this->_isInjection($msg)) {
                
    $this->state 2;
                
    $this->errorMsg 'This message has been identified as an email injection attempt';
                return 
    false;
            }
            
    $this->_to $to;
            
    $this->_from $from;
        
    $this->_name $name;
            
    $this->_subject $subject;
            
    $this->_msg $msg;

            
    $this->state 1;
            return 
    true;
        }

        function 
    sendMail() {
            if(
    $this->state !== 1) {
                
    trigger_error('SimpleMail Object is not initialized or contains invalid data - Mail sending cancelled'E_USER_WARNING);
            } else {
                
    $header "Return-Path: {$this->_from}\n";
                
    $header .= "X-Sender: {$this->_from}\n";
                
    $header .= "From: '{$this->_name}' <{$this->_from}>\n";
                
    $header .= "MIME-Version: 1.0\n";
                
    $header .= 'X-Mailer: PHP/'phpversion();

                
    ini_set(sendmail_from$this->_from); // stops gmail from putting (unknown sender) in From field
                
    $sent mail($this->_to$this->_subject$this->_msg$header);
                
    ini_restoresendmail_from );
                if (!
    $sent) {
                    
    trigger_error('SimpleContact failed due to php mail()'E_USER_WARNING);
                } else {
                    
    $this->state 3;
                }
            }
        }
    }


    // bot attacks may miss hidden input elements
    if (isset($_POST['confirm'])) {

        
    $to 'email@email.com'// administrator or comma deliminated group of administrators
        
    $name $_POST['name'];
        
    $from $_POST['email'];
        
    $subject $_POST['subject'];
        
    $msg $_POST['message'];

        
    $fin FALSE// is the entire user process complete?
        
    $userOutput ''// what the user will see at the end

        
        
    $mailer = new SimpleContact;
        
    $mailReady $mailer->init($to$from$name$subject$msg);

        
    //ensure input is good
        
    if(!$mailReady) {
            
    $userOutput $mailer->errorMsg;
        } else {
            
    $mailer->sendMail();
            if(
    $mailer->state !== 3) { //something went wrong, mail() probably failed :|
                
    $userOutput $mailer->errorMsg;
            } else {
                
    $userOutput "Hi {$_POST['name']}, thanks for your message";
                
    $fin TRUE;
            }
        }


    }
    ?>

  • #2
    Regular Coder
    Join Date
    Feb 2005
    Location
    Texas
    Posts
    472
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I don't know how to professionals would do it, but I would do this.

    PHP Code:
    $to 'email@email.com'// administrator or comma deliminated group of administrators
    $to=explode(","$to);
        
    $name $_POST['name'];
        
    $from $_POST['email'];
        
    $subject $_POST['subject'];
        
    $msg $_POST['message'];

        
    $fin FALSE// is the entire user process complete?
        
    $userOutput ''// what the user will see at the end

        
        
    $mailer = new SimpleContact;
    foreach(
    $to as $key => $value) {
        
    $mailReady $mailer->init($value$from$name$subject$msg);

    If you're reading this, it may already be too late!

  • #3
    New Coder
    Join Date
    Apr 2006
    Posts
    37
    Thanks
    2
    Thanked 0 Times in 0 Posts
    i tried what u gave, but it just said 'invalid recipient email address'


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •