1

There are a lot of other posts about this. I have gone through several and tried arrays and inputting emails in the $to field and the like but my form keeps only sending if I specify one email. What am I doing wrong? I'm a PHP noob too.

<?php
// EVALUATION FORM
$your_email = 'email1@address.com, email2@address.com';

session_start();
$errors        = '';
$name          = '';
$visitor_email = '';
$phone         = '';

if (isset($_POST['submit'])) {

    $name          = $_POST['name'];
    $visitor_email = $_POST['email'];
    $phone         = $_POST['phone'];
    ///------------Do Validations-------------
    if (empty($name) || empty($visitor_email)) {
        $errors .= "\n Form was not submitted. Please fill out all the fields. ";
    }
    if (IsInjected($visitor_email)) {
        $errors .= "\n Bad email value!";
    }
    if (empty($_SESSION['6_letters_code']) || strcasecmp($_SESSION['6_letters_code'], $_POST['6_letters_code']) != 0) {
        //Note: the captcha code is compared case insensitively.
        //if you want case sensitive match, update the check above to
        // strcmp()
        $errors .= "\n The captcha code does not match!";
    }

    if (empty($errors)) {
        //send the email
        $to      = $your_email;
        $subject = "Free Injury Evaluation form submission from Northwest Injury Centers";
        $from    = $your_email;
        $ip      = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '';

        $body = "$name submitted the Free Injury Evaluation form:\n\n" . "Name: $name\n" . "Email: $visitor_email \n" . "Phone: $phone \n" . "IP: $ip\n";

        $headers = "From: $from \r\n";
        $headers .= "Reply-To: $visitor_email \r\n";

        mail($to, $subject, $body, $headers);

        header('Location: thankyou.php');
    }
}

// Function to validate against any email injection attempts
function IsInjected($str)
{
    $injections = array(
        '(\n+)',
        '(\r+)',
        '(\t+)',
        '(%0A+)',
        '(%0D+)',
        '(%08+)',
        '(%09+)'
    );
    $inject     = join('|', $injections);
    $inject     = "/$inject/i";
    if (preg_match($inject, $str)) {
        return true;
    } else {
        return false;
    }
}
?>
Nagendra Rao
  • 7,016
  • 5
  • 54
  • 92

3 Answers3

3

You can provide multiple recipients, however you can only provide one sender. Since you are using $your_email both for $to and $from, you're trying to send an email with multiple senders.

Nic Wortel
  • 11,155
  • 6
  • 60
  • 79
  • Thanks! I knew it was some silly noob thing. I made an $email_to and specified multiple emails. I then specified the $email_to variable within $to = $email_to; The form works! – user2109656 Dec 11 '13 at 22:07
0

You can add cc to your mail header,

$headers .= 'Cc: someemail@domain.com' . "\r\n";

This will send a copy to the above mentioned email along with to email address.

Nagendra Rao
  • 7,016
  • 5
  • 54
  • 92
0

This will work:

$email_to = "jhewitt@amleo.com,some@other.com,yet@another.net";

Fore readability sake in the code use an array and implode it to a comma separated string:

$recipients = array(
  "youremailaddress@yourdomain.com",
  // more emails
);
$email_to = implode(',', $recipients); // your email address
$email_subject = "Contact Form Message"; // email subject line
$thankyou = "thankyou.htm"; // thank you page
sergio
  • 5,210
  • 7
  • 24
  • 46