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!","


Just testing the email script



PRINT 'done';
Who is Participating?

Improve company productivity with a Business Account.Sign Up

jkna_gunnConnect With a Mentor Commented:
$contents = file('list.txt');
foreach($contents as $email) {
   mail($email,"update - Test Mail!","hi etc","");

print "done";

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

$email = rtrim($email);
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),

ChrisAndrewsAuthor Commented:

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

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.