treehouse : what would you like to learn today?
Web Design Web Development iOS Development

One submit button, two recipients

  • Hi all -

    I took the simpleform from the site and have modified it a bit to allow the user to choose who receives the email.
    I also wanted to add a 'CC' if possible.

    Here is the code:


    <?php

    $EmailFrom = Trim(stripslashes($_POST['Email']));
    $EmailTo = Trim(stripslashes($_POST['Recipient']));
    $EmailCc = \"MYEMAIL@gmail.com\";
    $Subject = \"TEST Contact\";
    $Name = Trim(stripslashes($_POST['Name']));
    $Tel = Trim(stripslashes($_POST['Tel']));
    $Email = Trim(stripslashes($_POST['Email']));
    $Message = Trim(stripslashes($_POST['Message']));

    // validation
    $validationOK=true;
    if (!$validationOK) {
    print \"<meta http-equiv=\\"refresh\\" content=\\"0;URL=error.htm\\">\";
    exit;
    }

    // prepare email body text
    $Body = \"\";
    $Body .= \"Name: \";
    $Body .= $Name;
    $Body .= \"\n\";
    $Body .= \"Tel: \";
    $Body .= $Tel;
    $Body .= \"\n\";
    $Body .= \"Email: \";
    $Body .= $Email;
    $Body .= \"\n\";
    $Body .= \"Message: \";
    $Body .= $Message;
    $Body .= \"\n\";

    // send email
    $success = mail($EmailTo, $Subject, $Body, \"From: <$EmailFrom>\");

    // redirect to success page
    if ($success){
    print \"<meta http-equiv=\\"refresh\\" content=\\"0;URL=contactthanks.php\\">\";
    }
    else{
    print \"<meta http-equiv=\\"refresh\\" content=\\"0;URL=error.htm\\">\";
    }
    ?>


    I added this little snippet as i thought it may send the second email, but it didn't work.
    $EmailCc = \"MYEMAIL@gmail.com\";


    Maybe I should just modify the '$EmailTo' variable, but i'm not too proficient in php.

  • $EmailTo = \"UREMAIL@EMAIL.COM, \".Trim(stripslashes($_POST['Recipient']));
  • if you want to carbon copy just do this:


    $success = mail($EmailTo, $Subject, $Body, \"From: <$EmailFrom>\");

    // redirect to success page
    if ($success){
    //copy to someone else
    mail(newaddress@newaddress.com, $Subject, $Body, \"From: <$EmailFrom>\");
    print \"<meta http-equiv=\\"refresh\\" content=\\"0;URL=contactthanks.php\\">\";
    }
  • All you need to do is pass the CC in the last variable of mail();



    <?php

    $header = \"From: $EmailFrom\r\n\";
    $header .= \"CC: otheremail@*website*.com\r\n\";
    $header .= \"BCC: yetanotheremail@*website*.com\r\n\";

    // send email
    $success = mail($EmailTo, $Subject, $Body, $header);

    ?>


    Enjoy,
  • Sorry about the double post, but I forgot to mention.

    It's good practice to always send the email from the domain that your server is on and use a replyto instead.


    <?php

    $header = \"From: server@thisdomain.com\r\n\";
    $header .= \"Replyto: $EmailFrom\r\n\";

    $header .= \"CC: otheremail@*website*.com\r\n\";
    $header .= \"BCC: yetanotheremail@*website*.com\r\n\";

    // send email
    $success = mail($EmailTo, $Subject, $Body, $header);

    ?>