title: php mailer add confirmation email to inputted address and form submit
body: <p>I'm really trying to keep both functionality -- <strong>form fields filled out > form submit > goes to web masters email address</strong> with the information.</p>
  
  <p><strong>I'm failing to add a second functionality; I would like the user to be sent a custom confirmation message after form submit</strong> <em>(all users get same default message)</em> -- this message is emailed to the address inserted by user in 'email' field. I've gotten this; but by doing so, I killed my original functionality; I would like to maintain both; thus far I can get one to work, but then the other functionality dies -- currently it just sends confirmation to both emails below.</p>
  
  <pre><code>&lt;?php
  
  /* config start website@website.com*/
  
  $emailAddress = 'website@gmail.com'; // form goes here
  $to      = $_POST['email']; // user gets confirmation
  $message = 'Thank you for submitting the form on our website.!';
  
  /* config end */
  
  
  require "phpmailer/class.phpmailer.php";
  
  session_name("fancyform");
  session_start();    
  
  
  foreach($_POST as $k=&gt;$v)
  {
      if(ini_get('magic_quotes_gpc'))
      $_POST[$k]=stripslashes($_POST[$k]);
  
      $_POST[$k]=htmlspecialchars(strip_tags($_POST[$k]));
  }
  
  
  $err = array();
  
  if(!checkLen('name'))
      $err[]='The name field is too short or empty!';
  
  if(!checkLen('email'))
      $err[]='The email field is too short or empty!';
  else if(!checkEmail($_POST['email']))
      $err[]='Your email is not valid!';
  
  /* if(!checkLen('phone'))
      $err[]='The phone field is too short or empty!'; */
  
  /*if(!checkLen('subject'))
      $err[]='You have not selected a subject!';*/
  
  /* if(!checkLen('message'))
      $err[]='The message field is too short or empty!';*/
  
  if((int)$_POST['captcha'] != $_SESSION['expect'])
      $err[]='The captcha code is wrong!';
  
  
  if(count($err))
  {
      if($_POST['ajax'])
      {
          echo '-1';
      }
  
      else if($_SERVER['HTTP_REFERER'])
      {
          $_SESSION['errStr'] = implode('&lt;br /&gt;',$err);
          $_SESSION['post']=$_POST;
  
          header('Location: '.$_SERVER['HTTP_REFERER']);
      }
  
      exit;
  }
  
  
  $msg=
  'Name:  '.$_POST['name'].'&lt;br /&gt;
  Phone:  '.$_POST['phone'].'&lt;br /&gt;
  Email:  '.$_POST['email'].'&lt;br /&gt;
  IP: '.$_SERVER['REMOTE_ADDR'].'&lt;br /&gt;&lt;br /&gt;
  Referred By:    '.$_POST['referer'].'&lt;br /&gt;&lt;br /&gt;
  Message:&lt;br /&gt;&lt;br /&gt;
  
  '.nl2br($_POST['message']).'
  
  ';
  
  
  $mail = new PHPMailer();
  $mail-&gt;IsMail();
  
  $mail-&gt;AddReplyTo($_POST['email'], $_POST['name']);
  $mail-&gt;AddAddress($emailAddress);
  $mail-&gt;addAddress($to);  // why can't both coexist?
  $mail-&gt;SetFrom($_POST['email'], $_POST['name']);
  $mail-&gt;Subject = "A new Bridgetower.com Lead".mb_strtolower($_POST['subject'])." from ".$_POST['name']." | Via your contact form feedback";
  
  $mail-&gt;MsgHTML($msg);
  $mail-&gt;msgHtml($message); // why can't both coexist?
  
  $mail-&gt;Send();
  
  // $to      = $_POST['email'];
  // $message = 'Thank you for submitting the form on our website.!';
  
  
  unset($_SESSION['post']);
  
  if($_POST['ajax'])
  {
      echo '1';
  }
  else
  {
      $_SESSION['sent']=1;
  
      if($_SERVER['HTTP_REFERER'])
          header('Location: '.$_SERVER['HTTP_REFERER']);
  
      exit;
  }
  
  function checkLen($str,$len=2)
  {
      return isset($_POST[$str]) &amp;&amp; mb_strlen(strip_tags($_POST[$str]),"utf-8") &gt; $len;
  }
  
  function checkEmail($str)
  {
      return preg_match("/^[\.A-z0-9_\-\+]+[@][A-z0-9_\-]+([.][A-z0-9_\-]+)+[A-z]{1,4}$/", $str);
  }
  
  ?&gt;
  </code></pre>
  
  <p><strong>Code I added</strong> <em>(which works, but overrides base form submit)</em></p>
  
  <p>Grab email inputted in email field within online form > send confirmation there after submit.</p>
  
  <p>If I take the below lines out; my form works normally; user fills out form, sends to specified default email address - I just want to add in the confirmation message, and maintain the form working properly as well.</p>
  
  <hr>
  
  <pre><code>$to      = $_POST['email']; // user gets confirmation
  $message = 'Thank you for submitting the form on our website.!';
  
  
  $mail-&gt;addAddress($to);  // why can't both coexist?
  
  $mail-&gt;msgHtml($message); // why can't both coexist?
  </code></pre>
  
  <hr>
  
  <p>Maybe the question is how to allow multiple <code>$mail-&gt;MsgHTML</code> to send?</p>
  
  <p><strong>Original code; I'm trying to add second functionality on to:</strong></p>
  
  <pre><code>&lt;?php
  
  /* config start website@website.com*/
  
  $emailAddress = 'website@gmail.com';
  
  /* config end */
  
  
  require "phpmailer/class.phpmailer.php";
  
  session_name("fancyform");
  session_start();    
  
  
  foreach($_POST as $k=&gt;$v)
  {
      if(ini_get('magic_quotes_gpc'))
      $_POST[$k]=stripslashes($_POST[$k]);
  
      $_POST[$k]=htmlspecialchars(strip_tags($_POST[$k]));
  }
  
  
  $err = array();
  
  if(!checkLen('name'))
      $err[]='The name field is too short or empty!';
  
  if(!checkLen('email'))
      $err[]='The email field is too short or empty!';
  else if(!checkEmail($_POST['email']))
      $err[]='Your email is not valid!';
  
  /* if(!checkLen('phone'))
      $err[]='The phone field is too short or empty!'; */
  
  /*if(!checkLen('subject'))
      $err[]='You have not selected a subject!';*/
  
  /* if(!checkLen('message'))
      $err[]='The message field is too short or empty!';*/
  
  if((int)$_POST['captcha'] != $_SESSION['expect'])
      $err[]='The captcha code is wrong!';
  
  
  if(count($err))
  {
      if($_POST['ajax'])
      {
          echo '-1';
      }
  
      else if($_SERVER['HTTP_REFERER'])
      {
          $_SESSION['errStr'] = implode('&lt;br /&gt;',$err);
          $_SESSION['post']=$_POST;
  
          header('Location: '.$_SERVER['HTTP_REFERER']);
      }
  
      exit;
  }
  
  
  $msg=
  'Name:  '.$_POST['name'].'&lt;br /&gt;
  Phone:  '.$_POST['phone'].'&lt;br /&gt;
  Email:  '.$_POST['email'].'&lt;br /&gt;
  IP: '.$_SERVER['REMOTE_ADDR'].'&lt;br /&gt;&lt;br /&gt;
  Referred By:    '.$_POST['referer'].'&lt;br /&gt;&lt;br /&gt;
  Message:&lt;br /&gt;&lt;br /&gt;
  
  '.nl2br($_POST['message']).'
  
  ';
  
  
  $mail = new PHPMailer();
  $mail-&gt;IsMail();
  
  $mail-&gt;AddReplyTo($_POST['email'], $_POST['name']);
  $mail-&gt;AddAddress($emailAddress);
  $mail-&gt;SetFrom($_POST['email'], $_POST['name']);
  $mail-&gt;Subject = "A new Bridgetower.com Lead".mb_strtolower($_POST['subject'])." from ".$_POST['name']." | Via your contact form feedback";
  
  $mail-&gt;MsgHTML($msg);
  
  $mail-&gt;Send();
  
  
  unset($_SESSION['post']);
  
  if($_POST['ajax'])
  {
      echo '1';
  }
  else
  {
      $_SESSION['sent']=1;
  
      if($_SERVER['HTTP_REFERER'])
          header('Location: '.$_SERVER['HTTP_REFERER']);
  
      exit;
  }
  
  function checkLen($str,$len=2)
  {
      return isset($_POST[$str]) &amp;&amp; mb_strlen(strip_tags($_POST[$str]),"utf-8") &gt; $len;
  }
  
  function checkEmail($str)
  {
      return preg_match("/^[\.A-z0-9_\-\+]+[@][A-z0-9_\-]+([.][A-z0-9_\-]+)+[A-z]{1,4}$/", $str);
  }
  
  ?&gt;
  </code></pre>