title: PHP mail() BCC - display only the end receivers address in To: header
body: <p>I'm attempting to BCC to a list of subscribers from a database, using PHP mail(). Everything works, but I'm running into a problem that has troubled me all morning. I'm able to send the list with BCC, but are unable to append the receiving end email address to the deader "To:". </p>
  
  <p>For example, I send the list to the following email addresses <strong>(test1@example.com, test2@example.com, and test3@example.com)</strong>. Each email address receives an email and the other email addresses are hidden because of BCC.</p>
  
  <p>My problem is that in the header, "To:" is displayed blank on all of the receiving ends of the list. That I understand and know that header won't display because I have only the BCC header within the outgoing message. I've attemted to <code>for</code> loop the process but I receive all of the emails, plus one for that address in one loop.</p>
  
  <p>Here is my working code: The working code includes the loop that I attempted for solving the <strong>To:</strong> header. I'm able to send the email, though I receive all of the emails that were sent.</p>
  
  <pre><code>&lt;?php
  
  /*
     Gathers the number of rows within the database. Used for the loop that displays the entire list in the BCC. 
  */
  
     session_start();
     include_once '../dbconnect.php';
     $result = mysql_query("SELECT * FROM news");
     $num_rows = mysql_num_rows($result);
     $rows = $num_rows;
  
  /*
     Requests the list from the database, please the list in a loop, displays the list only once, and places list in an operator.
  */
  
     $result = mysql_query("SELECT * FROM `news`");
       while($row = mysql_fetch_array( $result )) {
         for ($x = 1; $x &lt;= 1; $x++) {
           $contacts.= "".$row['email'].",";
         }
       }
  
  /*
     ATTEMPT (It works... sort of): Sends mail to the list using BCC, displays the ONLY receivers email address in the To: header
  */
  
     $result = mysql_query("SELECT * FROM `news`");
       while($row = mysql_fetch_array( $result )) {
         for ($x = 1; $x &lt;= 1; $x++) {
           $receiver= "".$row['email']."";
  
           $to = "$receiver";
           $subject = 'Test Email';
           $headers = "From: example@example.com\r\n";
           $headers .= "BCC: $contacts";
  
           $headers .= "MIME-Version: 1.0\r\n";
           $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
           $message = '&lt;html&gt;&lt;body&gt;';
           $message .= '&lt;h1 style=""&gt;Test Message&lt;/h1&gt;';
           $message .= '&lt;/body&gt;&lt;/html&gt;';
           mail($to,$subject, $message, $headers);
        }
      }
  ?&gt;
  </code></pre>
  
  <p>My general thought was to loop, but I can't find a solution that actually solves this completely, by BBC to the list and displaying only the receiving ends email address in the <strong>To:</strong> header. Any thoughts or ideas? </p>
  
  <p><strong>Update with SMTP Server</strong></p>
  
  <p>I've been attempting to use the solution found in this thread and apply it to an SMTP server. Send the message using SendGrid was the ideal choice. I've come up with the below option, but the script only seems to send one message to one of the addresses in the database and not all the address. </p>
  
  <pre><code>&lt;?php
  require_once "Mail.php";
  
  $sub = $_POST['subject'];
  $ttl = $_POST['title'];
  $img = $_POST['image'];
  $bdy = $_POST['body'];
  $lk = $_POST['link'];
  
  mysql_connect("", "", "") or die(mysql_error()) ; 
     mysql_select_db("") or die(mysql_error()) ; 
  
    $result = mysql_query("SELECT `email` FROM news");
  
    $num_rows = mysql_num_rows($result);
  
    $receivers = array();
    while ($row = mysql_fetch_array($result)) {
      $receivers[] = $row['email'];
    }
  
    foreach ($receivers as $receiver) { }
  
  $from = "test@example.com";
  $to = $receiver;
  $subject = $sub;
  $mime = "1.0";
  $ctype = "text/html; charset=ISO-8859-1";
  $body = '
  &lt;html&gt;&lt;body&gt;
  &lt;p&gt;Test Message!&lt;/p&gt;
  &lt;/body&gt;&lt;/html&gt;
  ';
  
  $host = "";
  $port = "";
  $username = "";
  $password = "";
  
  $headers = array ('From' =&gt; $from,
    'To' =&gt; $to,
    'Subject' =&gt; $subject,
    'MIME-Version' =&gt; $mime ,
    'Content-Type:' =&gt; $ctype);
  
  $smtp = Mail::factory('smtp',
    array ('host' =&gt; $host,
      'port' =&gt; $port,
      'auth' =&gt; true,
      'username' =&gt; $username,
      'password' =&gt; $password));
  
  $mail = $smtp-&gt;send($to, $headers, $body);
  
  if (PEAR::isError($mail)) {
    echo("&lt;p&gt;" . $mail-&gt;getMessage() . "&lt;/p&gt;");
  } else {
    echo("&lt;p&gt;Message successfully sent!&lt;/p&gt;");
  }
  ?&gt;
  </code></pre>