Re: Mass Mailing Using PHP
| From: | Jim Grill | Date: | Mon, 27 Sep 2004 12:42:56 +0000 |
| Subject: | Re: Mass Mailing Using PHP | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-197946@lists.php.net to get a copy of this message | ||
> Are your users' details in a database?
>
> If so simply read through the database mailing to each user, something
like:
>
> <?php
> $message = "Hello everyone";
> $subject = "Hello";
> $headers = "From: Someone <someone@somewhere.com>\r\n";
> $sql = "SELECT user_name, email FROM user";
> $result = mysql_query($sql) or die ("Query failed: " . mysql_error());
> while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
> {
> $email = $row['email'];
> mail($email, $subject, $message, $headers);
> }
> ?>
>
If the message is the same for each user then use the Bcc header and save
yourself some overhead. There is no need to send a new mail to each
recipient in a loop. If your on a shared system I'm sure your ISP will
appreciate not getting bombed by your script. ;-)
loose example:
while ($row = mysql_fetch_assoc($result))
{
$bccList .= "<{$row['email']}>,";
}
/*remove the last comma*/
$bccList = substr($bccList,0,-1);
$headers .= "Bcc: $bccList\n";
/*mail only one time*/
mail($email, $subject, $message, $headers);
Jim Grill