foreach

um, this is my first attempt at using 'foreach', I must have something wrong:

I have a txt file that is a list of email addresses, one per line.

The script below sends to the first name on the list, then says 'done'.  Doesn't send to the remaining addresses.  Do you see what's wrong?    Also... would it be reasonable to use this script for 4000 emails, or is it likely to overwhelm my server (p4, 512 ram, light load)?

Thanks,     Chris

<?
$emaillist_url = 'list.txt';

$content = file_get_contents($emaillist_url);

$emailarray = explode("\n",$content);

foreach ($emailarray as $email)  {

mail("$email","update - Test Mail!","

Hi,

Just testing the email script

","From:updates@mysite.com");

}

PRINT 'done';
?>
ChrisAndrewsAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
jkna_gunnConnect With a Mentor Commented:
<?php
$contents = file('list.txt');
foreach($contents as $email) {
   mail($email,"update - Test Mail!","hi etc","From:updates@mysite.com");
}

print "done";
?>

0
 
jkna_gunnCommented:
if there are problems you might have to use rtrim() on the $email variable before using it in the mail function

$email = rtrim($email);
0
 
ChrisAndrewsAuthor Commented:

Thank you :)

It seems to loop back and resend the very first email, but I can work around that (use my own address),

Chris
0
 
ChrisAndrewsAuthor Commented:

oh, I think the rtrim fixed that, will test again,

Chris
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.