Contact Form with multiple recipients

Discussion in 'PHP' started by visualkreations, Sep 2, 2009.

  1. #1
    I am trying to build a contact form that can either be sent to 1 or 2 recipients. I would like it to give the user the option of checking either Inquiries about employment check box or the General Inquiries check box or checking both.
    For example if they check Inquiries about employment check box the form would submit to and/or if they check the General Inquiries check box the form would submit to .

    Heres how the form is now:

    <h1>Contact Us</h1>
    <form action="contact_form.php" method="post">
      <label for="employment">Employment</label>
      <input type="checkbox" name-"employment" />
      <label for="GInquiries">General Inquiries</label>
      <input type="checkbox" name-"GInquiries" />
      <br />
      <label for="name">Name<span style="color:red">*</span>:</label>
      <br />
      <input type="text" name="name" />
      <br />
      <label for="email">E-mail<span style="color:red">*</span>:</label>
      <br />
      <input type="text" name="email" />
      <br />
      <label for="subj">Subject<span style="color:red">*</span>:</label>
      <br />
      <input type="text" name="subj" maxlength="<?php echo MAX_SUBJ_LEN;?>" />
      <br />
      <br />
      <label for="body">Message/Comment<span style="color:red">*</span>:</label>
      <br />
      <textarea name="body" style="width:260px; height:100px;" wrap="virtual"></textarea>
      <br />
      <em>All fields marked with <span style="color:red">*</span> are required.</em>
      <p>
        <?php
    /* A PHP contact form. */
    
    define('YOUR_EMAIL', 'test@test.com');
    
    // If the user does not fill in a subject, this will be used.
    define('DEFAULT_SUBJ', 'Company contact form');
    
    // This is the maximum length of a subject, in characters.
    define('MAX_SUBJ_LEN', 1000);
    
    if (isset($_POST['mail'])) {
        $errors = array();
        
        // Sanitize the subject;
        if (preg_match('/(%0A|%0D|\\n+|\\r+)/i', $_POST['subj'])) {
            $errors[] = "<font color=\"red\">Your subject contains illegal characters.";
        } else {
            if (!strlen($_POST['subj']) || is_null($_POST['subj'])) {
                $subj = DEFAULT_SUBJ;
            } else {
                $subj = substr($_POST['subj'], 0, MAX_SUBJ_LEN);
            }
        }
        
        // Validate their e-mail address.
    	if (!preg_match('/^[a-z0-9._-]+@[a-z0-9._-]+\.([a-z]{2,4})($|\.([a-z]{2,4})$)/i', $_POST['email'])){
            $errors[] = "<font color=\"red\">Invalid e-mail address.</font>";
        }
            
        
        // Validate the body.
        if (preg_match('/(%0A|%0D|\\n+|\\r+)(content-type:|to:|cc:|bcc:)/i', $_POST['body'])) {
            $errors[] = "<font color=\"red\">Your message body contains invalid characters.</font>";
        }
        if (!strlen($_POST['body'])) {
            $errors[] = "<font color=\"red\">The body of your message was blank.</font>";
        }
    
        if (count($errors)) {
            for ($i = 0; $i < count($errors); $i++) {
                printf('<div class="error">%s</div>', $errors[$i]);
            }
        } else {
            $headers = sprintf("From: %s\r\n", $_POST['email']);
    		
            if (mail(YOUR_EMAIL, $subj, $_POST['body'], $headers)) {
                //header( 'Location: http://www.yoursite.com/cnn.html' ) ;
            } else {
                print "<font color=\"red\"><em>
    			<p>An error occurred while we were attempting to send your message. Please try again later.
    			</p></em></font>";
            }
        }
    }
    ?>
        <br />
        <input type="submit" name="mail" value="Send Message" />
    </form>
    PHP:
     
    visualkreations, Sep 2, 2009 IP
  2. shaunole

    shaunole Peon

    Messages:
    24
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You don't seem to have the to address set. You should be able to use a switch/case or if statement to identify whether the checkboxes are checked. If they are, build your to address into a variable (separated by semi-colons).

    UPDATE:
    I see you have a constant for the YOUR_EMAIL. Just populate it based on the result of the checkboxes. I would avoid creating it as a constant unless thats needed for some reason. Of course after you create it, you can't change the constant.
     
    Last edited: Sep 3, 2009
    shaunole, Sep 3, 2009 IP