I'm trying to mail a newsletter to a list of people, but I get an error every time it tries to mail. Anyone know what I'm doing wrong?

<?php
$file = fopen("upload/newletter.html", "r");
$sub = fopen("to.txt", "r");
$x = 0;
$y = 0;
while (!feof($sub))
{
    $names[$x] = fgets($sub);
    $x++;
    $y++;
}
while (!feof($file))
    $content = $content . fgets($file);

echo $content;
$x = 1;
while ($x <= $y)
{
    mail($names[$x], "Branches and Roots Weekly Newsletter", "Testing");
    $x++;
}
?>

Heres the error I get:
Warning: mail(): Bad parameters to mail() function, mail not sent. in (Directory) on line 19

There are no invalid e-mails, and I found that if I hard code it, it will work. However I can't hard code it for what I'm trying to do, so anyway I can fix it?

As Anon said above, drop in a print_r or var_dump of $names to see what you have there.
Also,

foreach ($names as $to) {
  mail($to, "Branches and Roots Weekly Newsletter", "Testing");
}

would make your loop a bit easier.

I'm trying to mail a newsletter to a list of people, but I get an error every time it tries to mail. Anyone know what I'm doing wrong?

<?php
$file = fopen("upload/newletter.html", "r");
$sub = fopen("to.txt", "r");
$x = 0;
$y = 0;
while (!feof($sub))
{
    $names[$x] = fgets($sub);
    $x++;
    $y++;
}
while (!feof($file))
    $content = $content . fgets($file);

echo $content;
$x = 1;
while ($x <= $y)
{
    mail($names[$x], "Branches and Roots Weekly Newsletter", "Testing");
    $x++;
}
?>

Heres the error I get:
Warning: mail(): Bad parameters to mail() function, mail not sent. in (Directory) on line 19

Some advice :
1.Always test if the file exists , first :

if(!file_exists("to.txt"))
         die("File could not be found");

2.

$names[$x] = fgets($sub);
    $x++;

I assume you are familiar with C , but in php it's easier to do this:

$names[]=fgets($sub);

It will start from 0 and get on the next element at every loop.
And later if you wanna know the number of elements in the array,you can use count($names);

3.With strings you can also use a very used shortcut, so instead of:

$content = $content . fgets($file);

you cand just as easily write :

$content.=fgets($file);

which does the same thing

Anyway,back to our sheeps , the $names[] array is the problem.
As the others said,try to print it out , see if you have the correct info.
Oh,and btw,I hope you are running the script from a server that supports the mail() function( not the localhost)

$x = 1;

Change this
to

$x = 0;

There is no records at $names[0] which is where the while loop is starting

This article has been dead for over six months. Start a new discussion instead.