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:
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 :
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)