DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   PHP (http://www.daniweb.com/forums/forum17.html)
-   -   Mail to a list of people (http://www.daniweb.com/forums/thread87045.html)

Firestone Aug 18th, 2007 8:01 pm
Mail to a list of people
 
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

Anonymusius Aug 19th, 2007 1:32 am
Re: Mail to a list of people
 
I don't see any problems in my first glance, try to put a print_r($names); somewhere and see whether the array contains email adresses valid for PHP to swallow ( http://nl3.php.net/manual/en/function.mail.php ).

BTW, $x = 1;, shouldn't that be $x = 0;? Or does the file your loading first have an empty line or something like it?

You might to want to check your php.ini is fully configured for sending mail: http://nl3.php.net/manual/en/ref.mail.php .

pritaeas Aug 20th, 2007 7:51 am
Re: Mail to a list of people
 
You can simplify your file handling by using file_get_contents() followed by an explode(), should the problem lie in invalid email address.

http://nl3.php.net/manual/en/functio...t-contents.php
http://nl3.php.net/explode

Firestone Aug 20th, 2007 12:21 pm
Re: Mail to a list of people
 
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?

Ezzaral Aug 20th, 2007 2:31 pm
Re: Mail to a list of people
 
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.

Eko Aug 22nd, 2007 5:05 pm
Re: Mail to a list of people
 
Quote:

Originally Posted by Firestone (Post 421312)
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)

nyashaC Aug 25th, 2007 6:37 am
Re: Mail to a list of people
 
$x = 1;

Change this
to

$x = 0;

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


All times are GMT -4. The time now is 11:40 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC