Solved

foreach

Posted on 2004-04-12
4
443 Views
Last Modified: 2008-03-06
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';
?>
0
Comment
Question by:ChrisAndrews
  • 2
  • 2
4 Comments
 
LVL 6

Accepted Solution

by:
jkna_gunn earned 50 total points
ID: 10811355
<?php
$contents = file('list.txt');
foreach($contents as $email) {
   mail($email,"update - Test Mail!","hi etc","From:updates@mysite.com");
}

print "done";
?>

0
 
LVL 6

Expert Comment

by:jkna_gunn
ID: 10811364
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
 

Author Comment

by:ChrisAndrews
ID: 10813429

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
 

Author Comment

by:ChrisAndrews
ID: 10813731

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

Chris
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question