1

I am pulling email addresses from my mailing list in a txt file. with the following:

clearstatcache(); 

$file = file("test.txt");

 for ($i = 0; $i < 20; $i++) {  
 $emails .= $file[$i];  
 }

As you can see I've stored them in $emails. and if I echo $emails, I get the emails listed: info@example.com, test@mydomain.com, admin@domain.com, etc.

now to sending the BCC:

// recipient
$to  = ''; 

// subject
$subject = 'The subject is here';

// message
$message = 'The body of the email is here';

// To send HTML mail, the Content-type header must be set
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

// Additional headers
$headers .= 'From: John Doe <info@example.com>' . "\r\n";
$headers .= 'Bcc: '.$emails . "\r\n";

// Mail it
mail($to, $subject, $message, $headers);

The mail doesn't get sent to the list, only to info@example.com - So this doesn't work and something unexpected happens - for some strange reason the 20 emails from the for loop are listed at the top of the email body when received by info@example.com

When I try manual input it works perfectly. so the code below works, but manual input is counter productive to what I am trying to achieve.

$test = "info@example.com, test@mydomain.com, admin@domain.com,";

// recipient
$to  = ''; 

// subject
$subject = 'The subject is here';

// message
$message = 'The body of the email is here';

// To send HTML mail, the Content-type header must be set
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

// Additional headers
$headers .= 'From: John Doe <info@example.com>' . "\r\n";
$headers .= 'Bcc: '.$test . "\r\n";

// Mail it
mail($to, $subject, $message, $headers);

So it appears that the issue is in the variable, but I can't figure out why it doesn't work because $emails echoes out all the email addresses fine.

Guage
  • 85
  • 10
  • 2
    What are the exact actual contents of your text file? I am guessing that your methodology for reading it results in some blank lines inside the string, which mistakenly terminate the header block and start the body. A simpler method would be to just do `implode(',' file('test.txt'))`. The function `file()` reads linewise into an array, and `implode()` sticks them together with commas. – Michael Berkowski Nov 05 '14 at 02:28
  • use `$test = "info@example.com,test@mydomain.com,admin@domain.com";` without space between comma – Tamil Selvan C Nov 05 '14 at 02:32
  • On top of what @MichaelBerkowski said, you may want to read this: http://stackoverflow.com/a/9525476/2518525 – Darren Nov 05 '14 at 02:53
  • 2
    Make your life easier and use a DB. – Funk Forty Niner Nov 05 '14 at 02:57
  • To further elaborate on my comment about using a DB; in using that, mail can be sent to individual email addresses, without the fear of someone seeing other people's emails to whom they were sent to. Even using Bcc, email addresses still show up in mail headers and can be viewed via the source. – Funk Forty Niner Nov 05 '14 at 03:37

2 Answers2

0

White space is automatically added to the end of line. This change in the for loop fixes the issue.

$emails .= trim($file[$i]);
Guage
  • 85
  • 10
0

You must close the loop after: mail($to, $subject, $message, $headers); } and $headers .= 'Bcc: '.$emails . "\r\n"; is $headers .= 'Bcc: '.$file[$i] . "\r\n";

So the loop will run the entire program 20 times. Do not place recipient in $ to because otherwise will send 20 times as well. Tested and works great.