title: PHP - email to email address based on database query
body: <p>i am trying to create a reply to an ad form where a user clicks an ad and can send an email, however i do not want the users to actually see which address they are sending it to but instead the email address can be taken from a database based on the username of the ad</p>
  
  <p>i have tried this code but cant seem to get it to work, any help appreciated!</p>
  
  <pre><code>$username = $_SESSION['username'];
  
  $sql = "SELECT * FROM user WHERE username=:username";
  $q = $conn-&gt;prepare($sql);
  $q-&gt;bindParam(':username', $username, PDO::PARAM_STR);
  $q-&gt;execute();
  $row = $q-&gt;fetch(PDO::FETCH_ASSOC);
  
  $email1 = $row['email']; 
  
   $to='$email1';
  $messageSubject='Message subject';
  $confirmationSubject='Confirmation message subject';
  $confirmationBody="Confirmation message body";
   $email='';
    $body='';
  $displayForm=true;
  if ($_POST){
  $email=stripslashes($_POST['email']);
  $body=stripslashes($_POST['body']);
  // validate e-mail address
  $valid=eregi('^([0-9a-z]+[-._+&amp;])*[0-9a-z]+@([-0-9a-z]+[.])+[a-z]{2,6}$',$email);
  $crack=eregi("(\r|\n)(to:|from:|cc:|bcc:)",$body);
  if ($email &amp;&amp; $body &amp;&amp; $valid &amp;&amp; !$crack){
    if (mail($to,$messageSubject,$body,'From: '.$email."\r\n")
        &amp;&amp; mail($email,$confirmationSubject,$confirmationBody.$body,'From: '.$to."\r\n")){
      $displayForm=false;
  </code></pre>