0

i am trying to send emails via Bcc and i was expecting that they were hidden but it seems not. Maybe my code is not correct?

// grab all emails from txt file
$myfile = fopen("database-email.txt", "r") or die("Unable to open file!");
$allEmails = fread($myfile,filesize("database-email.txt"));
fclose($myfile);

$afzender = "noreply@wisselslag.nl";

$to = 'pj.maessen41@live.nl';

$subject = 'Nieuwsbrief de Wisselslag';

$headers = "From: " . $afzender . "\r\n";   
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$headers .= "Bcc: " . $allEmails  . "\r\n";

if (mail($to, $subject, $message, $headers)) {
  echo 'Bericht is verstuurd!';
} else {
  echo 'There was a problem sending the email.';
}

So i have a database-email.txt file in which all the emails are stored, comma seperated from each other like this:

joey.tims@gmail.com,
j.maessen@online.nl,
john.doe@live.nl,
diana.johnson@hotmail.com,

When sending to my gmail account, i can see this:

enter image description here

How is this possible that i can see to where the email also is sent to?

Jack Maessen
  • 1,780
  • 4
  • 19
  • 51

2 Answers2

3

The email list should not have new line character.

Make it one line:

$allEmails = str_replace(array("\n","\r"), '', $allEmails);

// joey.tims@gmail.com, j.maessen@online.nl, john.doe@live.nl, diana.johnson@hotmail.com
Ben
  • 5,069
  • 4
  • 18
  • 26
2

As mentioned in my comment, any list of recipients should not contain newline characters.

I'd change the format of your file to a single line

joey.tims@gmail.com,j.maessen@online.nl,john.doe@live.nl,diana.johnson@hotmail.com

I'd also use file_get_contents() instead of fopen / fread.


Alternately, store your email addresses on each line, without commas, eg

joey.tims@gmail.com
j.maessen@online.nl
john.doe@live.nl
diana.johnson@hotmail.com

and use file() and implode()

$allEmails = implode(',' file(__DIR__ . '/database-email.txt',
        FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES));
Phil
  • 157,677
  • 23
  • 242
  • 245