943,717 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 1521
  • PHP RSS
Aug 18th, 2007
0

Mail to a list of people

Expand Post »
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 Syntax (Toggle Plain Text)
  1. <?php
  2. $file = fopen("upload/newletter.html", "r");
  3. $sub = fopen("to.txt", "r");
  4. $x = 0;
  5. $y = 0;
  6. while (!feof($sub))
  7. {
  8. $names[$x] = fgets($sub);
  9. $x++;
  10. $y++;
  11. }
  12. while (!feof($file))
  13. $content = $content . fgets($file);
  14.  
  15. echo $content;
  16. $x = 1;
  17. while ($x <= $y)
  18. {
  19. mail($names[$x], "Branches and Roots Weekly Newsletter", "Testing");
  20. $x++;
  21. }
  22. ?>

Heres the error I get:
Warning: mail(): Bad parameters to mail() function, mail not sent. in (Directory) on line 19
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
Firestone is offline Offline
37 posts
since Nov 2006
Aug 19th, 2007
0

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 .
Reputation Points: 129
Solved Threads: 11
Posting Whiz in Training
Anonymusius is offline Offline
223 posts
since Aug 2006
Aug 20th, 2007
0

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
Sponsor
Featured Poster
Reputation Points: 550
Solved Threads: 728
Bite my shiny metal ass!
pritaeas is offline Offline
4,166 posts
since Jul 2006
Aug 20th, 2007
0

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?
Reputation Points: 10
Solved Threads: 0
Light Poster
Firestone is offline Offline
37 posts
since Nov 2006
Aug 20th, 2007
0

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,
PHP Syntax (Toggle Plain Text)
  1. foreach ($names as $to) {
  2. mail($to, "Branches and Roots Weekly Newsletter", "Testing");
  3. }
would make your loop a bit easier.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 838
Posting Genius
Ezzaral is offline Offline
6,760 posts
since May 2007
Aug 22nd, 2007
0

Re: Mail to a list of people

Click to Expand / Collapse  Quote originally posted by Firestone ...
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 Syntax (Toggle Plain Text)
  1. <?php
  2. $file = fopen("upload/newletter.html", "r");
  3. $sub = fopen("to.txt", "r");
  4. $x = 0;
  5. $y = 0;
  6. while (!feof($sub))
  7. {
  8. $names[$x] = fgets($sub);
  9. $x++;
  10. $y++;
  11. }
  12. while (!feof($file))
  13. $content = $content . fgets($file);
  14.  
  15. echo $content;
  16. $x = 1;
  17. while ($x <= $y)
  18. {
  19. mail($names[$x], "Branches and Roots Weekly Newsletter", "Testing");
  20. $x++;
  21. }
  22. ?>

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 :
PHP Syntax (Toggle Plain Text)
  1. if(!file_exists("to.txt"))
  2. die("File could not be found");
2.
PHP Syntax (Toggle Plain Text)
  1. $names[$x] = fgets($sub);
  2. $x++;
I assume you are familiar with C , but in php it's easier to do this:
PHP Syntax (Toggle Plain Text)
  1. $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:
PHP Syntax (Toggle Plain Text)
  1. $content = $content . fgets($file);
you cand just as easily write :
PHP Syntax (Toggle Plain Text)
  1. $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)
Last edited by Eko; Aug 22nd, 2007 at 5:07 pm.
Eko
Reputation Points: 12
Solved Threads: 1
Junior Poster in Training
Eko is offline Offline
60 posts
since Nov 2006
Aug 25th, 2007
0

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
Reputation Points: 19
Solved Threads: 0
Newbie Poster
nyashaC is offline Offline
19 posts
since Aug 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: MySQL datatype problem....
Next Thread in PHP Forum Timeline: updating database problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC