Jump to content

fgets not finding my line breaks?


vozzek

Recommended Posts

Hi all,

 

Quick problem: I'm trying to read the email addresses from my mailing list, which is a .txt file separated by line breaks.  I need to append this into the "Bcc: " section of the $headers variable so when I send the email everyone is blind to everyone else.

 

I'm using fgets, and it doesn't seem to be picking up the line breaks.  When I append new addresses to the file I used "/r" as the line terminator instead of "/n", but from what I've read that shouldn't really matter.  Here's my code:

 

$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: customerservice@thefrogandtheprincess.com' . "\r\n";

$myFile = "test_list_TFATP.txt";
$fh = fopen($myFile, 'r');

while(!feof($fh))
  {
  $email_address = fgets($fh, 4096);
  $headers .= 'Bcc: '.$email_address."\r\n";
  }

fclose($fh);
mail($to, $subject, $message, $headers);

 

Right now I'm using a three email test file.  The customerservice email gets through okay, but at the very top of the body of the email I see the following line:

Bcc: email1@whatever.com Bcc: email2@whatever.com Bcc: email3@whatever.com Bcc:

 

So the three magical questions are:

 

1) Why isn't the $headers variable being broken up line by line so each email can be sent?

2) Why is this showing up in the body of the email?

3) What's with the extra "Bcc: " at the end of the read?  Wouldn't the eof marker prevent that, or should I check for a null $email_address value before appending to $headers?

 

Thanks in advance for the help.

 

 

 

Link to comment
Share on other sites

Okay, I cheated (commence bashing me).  ;D

 

Instead of reading the file line-by-line, I did the following:

 

$headers .= 'Bcc: ';

$mailstr = file_get_contents("test_list_TFATP.txt");
$mailstr = eregi_replace("\n",",",$mailstr);   // Replaced page breaks with commas

$headers .= $mailstr.'customersupport@thefrogandtheprincess.com' . "\r\n";

 

Does anyone see a problem with this?  What's the character/size limitations on <b>file_get_contents</b> anyway?

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.