Sending two distinct emails from one online form

hello,

im sure there is a simple answer to this, but its not something i’ve see posted anywhere.

i have a php from in a html page, i got it from a PHP book, its well scripted and secure.
in the form the user submits some data and nomimates friends to send a link.

i wish to collect the data and send it to my email address. and i want the friends to receive a pre-written email that contains only some of the information that was collected.

i have successfully completed this in flash by sending the variables to two separate php files. Can this is done in html? on clicking the send button the variables are sent to two separate processing php files? or can it be done within one php file? either way is good for me.

all suggestions are welcome.

cressa

You can do this in the same php file. I’d be glad to help you out, but I would need to see the method you are currently using in your php file.

Check out phpmailer . It will make your life a lot easier. It allows CC’s, BCC’s, attachments, you name it.

thanks for the help. i attached the code below. the code is all contained in the same page. i separated the code into that which sits above the HTML and the form itself

 
<?php
include('includes/title.inc.php');
include('includes/corefuncs.php');
if (function_exists('nukeMagicQuotes')) {
  nukeMagicQuotes();
  }
// process the email
if (array_key_exists('send', $_POST)) {
  $to = 'info@website.com'; // use your own email address
  $subject = 'Feedback from Contact Form';
 
  // list expected fields
  $expected = array('name', 'email', 'phone', 'comments', 'howhear');
  // set required fields
  $required = array('name', 'email', 'comments', 'howhear');
  // create empty array for any missing fields
  $missing = array();
 
  // assume that there is nothing suspect
  $suspect = false;
  // create a pattern to locate suspect phrases
  $pattern = '/Content-Type:|Bcc:|Cc:/i';
 
  // function to check for suspect phrases
  function isSuspect($val, $pattern, &$suspect) {
    // if the variable is an array, loop through each element
 // and pass it recursively back to the same function
 if (is_array($val)) {
      foreach ($val as $item) {
     isSuspect($item, $pattern, $suspect);
     }
   }
    else {
      // if one of the suspect phrases is found, set Boolean to true
   if (preg_match($pattern, $val)) {
        $suspect = true;
     }
   }
    }
 
  // check the $_POST array and any sub-arrays for suspect content
  isSuspect($_POST, $pattern, $suspect);
 
  if ($suspect) {
    $mailSent = false;
 unset($missing);
 }
  else {
    // process the $_POST variables
    foreach ($_POST as $key => $value) {
      // assign to temporary variable and strip whitespace if not an array
      $temp = is_array($value) ? $value : trim($value);
   // if empty and required, add to $missing array
   if (empty($temp) && in_array($key, $required)) {
     array_push($missing, $key);
     }
   // otherwise, assign to a variable of the same name as $key
   elseif (in_array($key, $expected)) {
     ${$key} = $temp;
     }
   }
 }
 
  // validate the email address
  if (!empty($email)) {
    // regex to ensure no illegal characters in email address 
 $checkEmail = '/^[^@]+@[^\s
\'";,@%]+$/';
 // reject the email address if it doesn't match
 if (!preg_match($checkEmail, $email)) {
   array_push($missing, 'email');
   }
 }
 
  // go ahead only if not suspect and all required fields OK
  if (!$suspect && empty($missing)) {
    // build the message
    $message = "Name: $name

";
    $message .= "Email: $email

";
 $message .= "Phone: $phone

";
 $message .= "Comments: $comments

";
    $message .= "How did you hear of us?: $howhear";
    // limit line length to 70 characters
    $message = wordwrap($message, 70);
 
 // create additional headers
 $additionalHeaders = 'From: Web Contact Form<info@website.com>';
 if (!empty($email)) {
   $additionalHeaders .= "
Reply-To: $email";
   }
 
    // send it  
    $mailSent = mail($to, $subject, $message, $additionalHeaders);
 if ($mailSent) {
   // $missing is no longer needed if the email is sent, so unset it
   unset($missing);
   }
    }
  }
?>

 
<table border="0" align="center">
  <tr>
    <td width="446"><?php
  if ($_POST && isset($missing)) {
  ?>
      <span class="style43">Please complete the missing item(s) indicated.</span>
      </p>
      <p align="center">
        <?php
    }
  elseif ($_POST && !$mailSent) {
  ?>
        <span class="style43">Sorry, a problem occured during sending. Please try later.</span></p>
      <p align="center">
        <?php
    }
  elseif ($_POST && $mailSent) {
  ?>
        <span class="style43">Your message has been sent. Thank you for your feedback.</span></p>
      <p>
        <?php } ?>
      </p>
      <form id="feedback" method="post" action="">
        <table width="" border="0" align="center" bordercolor="0">
          <tr>
            <td height="227"><fieldset class="style20">
              <legend class="">Submission Form</legend>
              <table width="371" border="0" align="center">
                <tr>
                  <td width="205" height="48" align="left" class=""><span class="">Name:</span><span class="">*</span><span class="style16">
                    <?php
    if (isset($missing) && in_array('name', $missing)) { ?>
                    <span class="">Enter  name</span>
                    <?php } ?>
                  </span></td>
                  <td width="183" align="center"><input name="name" type="text" class="formbox " id="name" size="25" 
    <?php if (isset($missing)) {
      echo 'value="'.htmlentities($_POST['name']).'"';} ?>
    /></td>
                </tr>
                <tr>
                  <td height="55" align="left" class="">Email:<span class="">*</span><span class="">
                    <?php
    if (isset($missing) && in_array('email', $missing)) { ?>
                    Enter valid email
                    <?php } ?>
                  </span></td>
                  <td align="center"><input name="email" type="text" class="formbox " id="email" size="25" 
    <?php if (isset($missing)) {
      echo 'value="'.htmlentities($_POST['email']).'"';} ?>/></td>
                </tr>
                <tr>
                  <td height="43" align="left" class=""><span class=""><span class="">URL*:</span></span></td>
                  <td align="center"><input name="URL" type="text" class="formbox " id="URL" size="25" 
    <?php if (isset($missing)) {
      echo 'value="'.htmlentities($_POST['phone']).'"';} ?> /></td>
                </tr>
                <tr>
                  <td height="63" align="left" class="">Clip Name:<span class="style38">*
                      <?php
    if (isset($missing) && in_array('email', $missing)) { ?>
                    Enter Clip Name
                        <?php } ?>
                  </span></td>
                  <td align="center"><input name="clipName" type="text" class="formbox " id="clipName" size="25" 
    <?php if (isset($missing)) {
      echo 'value="'.htmlentities($_POST['phone']).'"';} ?> /></td>
                </tr>
                <tr>
                  <td height="61" align="left" class="">Description:<span class=""><span class="style38">*
                        <?php
    if (isset($missing) && in_array('comments', $missing)) { ?>
                    Enter  Description
                        <?php } ?>
                  </span></span></td>
                  <td align="center"><textarea name="description" id="description" cols="21" rows="5"><?php 
    if (isset($missing)) {
      echo htmlentities($_POST['comments']);
      } ?>
                  </textarea></td>
                </tr>
                <tr>
                  <td height="25" colspan="2" align="center" valign="top" class=""><table width="273" height="53" border="0">
                      <tr>
                        <td width="267"><input type="reset" name="Reset" value="Reset" style="float:left" />
                            <input name="send" id="send" type="submit" value="Submit" style="float:right"/></td>
                      </tr>
                  </table></td>
                </tr>
              </table>
            </fieldset></td>
          </tr>
        </table>
      </form></td>

Basically all you need to do is append a new message and a new call to mail()…

Set the new user mail body message (anywhere in the file)


$usermessage = 'Thanks for registering!';
$usersubject = 'Reg info';

And then call a new mail() (I’m appending this into your code;


 if ($mailSent) {
   // $missing is no longer needed if the email is sent, so unset it
   mail($email, $usersubject, $usermessage, $additionalHeaders);
   unset($missing);
}