PHP Diary | PHP Housekeeping | PHP Scripts | TD Scripts.com
Online Offline Manager - Show the online/offline status of a group of users on a webpage


[back]
WB01624_.gif (281 bytes) 01/02/00 "Using loops to send mail to multiple recipients" WB01626_.gif (272 bytes)[next]

I was stumped!

The admin code I created worked fine except for one bothersome thing. It would send out the email to multiple recipients and print the proper unsubscribe code, BUT the "From" mail header did not work the way it should have inside of the "all" loop. It should have been printing my from email address on every email address, however it was printing "nobody" instead. When I send the emails to any single person the header it printed correctly. Do you know what I was doing wrong? Read on and I'll explain.

The Admin PHP code for the mail list script

Yesterday we did the HTML form and I realized while re-reading my diary entry I neglected to explain the "test" email and its use. You could set up an email and then mail to the "test" email first (you, namely). It is pretty good idea to run a test mail to see how things look before actually sending the email to multiple recipients. Unfortunately we are in a spam-filled cyber world these days. Let's look at the entire admin code to send the mail to multiple recipients. It is quite similar to the loop yesterday which filled the dynamic dropdown select menu:

<HTML>
<BODY>
<?
require("setup.php3");
  if($contents == "")
{
   print("Error, no comments were submitted");
   exit;
}
$contents .= "\n ------------------------------------------------- ";
$contents .= "\n This email is not spam. You signed up for this ";
$contents .= "\n opt-in email list at http://www.php-scripts.com ";
$contents .= "\n If you would like to unsubscribe from this opt-in ";
$contents .= "\n list, just click the hyperlink below:\n";
if($to == "ALL")
{
  $toemails = file("$mail_list_path/YOUR_EMAIL_LIST.txt");
  sort($toemails);
  print("Sending email to...<br><strong>");
    for($index = 0; $index < count($toemails); $index++)
   {
     $y = $contents;
     $y .= "\n http://www.php-scripts.com/php_diary/unsubscribe.php3?ID=$toemails[$index]";
     $toemails[$index] = ereg_replace("\n", "", $toemails[$index]);
     mail($toemails[$index], $subject, $y, $from_header);
     print("$toemails[$index]</strong>...<strong>");
   }
  print("</strong>DONE");
}
  else
{
    // mail to only one recipient $to email address
    $contents .= "http://www.php-scripts.com/php_diary/unsubscribe.php3?id=$to";
    mail($to, $subject, $contents, $from_header);
    print("Sending email to...<strong>$to</strong>...DONE");
}
$text = "<p>Email message sent was<br>$contents";
print(nl2br($text));
?>
</BODY>
</HTML>

Notes: I use the $y variable to show the unique unsubscribe url for each email. I could not simply append the $contents variable like I do when mailing to only one email because the second and subsequent emails in the loop would have a bunch of urls from other people tacked at the end. The $y variable will be reassigned with each loop and thus be unique for each email. Using the $contents .= I append the "this is not spam" default message to the end of the $contents message to each email with the appropriate link to unsubscribe from the list (generated from inside the loop). I didn't have to sort the emails before I sent them, but I chose to do it to make it easier to follow who got sent what emails. After doing a mailout you could print this page and file it away so you knew what you mailed to who. Quite handy reference, I think. Another PHP script could be written also to track what mails were sent to what recipients. Lastly, you will note a new and quite useful function called nl2br which replaces any new line (\n) with a <BR> HTML tag.

This completes construction of the mail list script. In review: we have the form that subscribes and unsubscribes people to the list, and the secure browser-based admin form to send mail to one recipient or to all. Admittedly, this is a very basic set of scripts, but it functions very well and shows the process of building a mail list program using PHP from the ground up. It also illustrates the process of building almost any php script. First you plan the HTML, then you insert the PHP code where necessary. Unlike Perl, you can weave in and out of HTML quite conveniently, so it makes a lot of sense to build your HTML design first and do the PHP coding last. It would be pretty easy, for instance, to add multiple mail lists to the admin area. That way you could first choose which email list you wanted to mail to, and second send the list out.

Now what was I stumped over?

The addition of the ereg_replace function was needed to remove the new lines from each email address as it was cycled through the "ALL" loop. Those new lines were fouling up the mail function, because new lines are how the headers are separated. ereg_replace is a case-sensitive match and replace whereas eregi_replace is case INsensitive match and replace. We will certainly be using these functions more in the future.

Please vote on what you think of this diary lesson :)

How useful was this diary entry? Avg Surfer Rating: 3.66 (244)

[back]WB01624_.gif (281 bytes) 01/02/00 "Using loops to send mail to multiple recipients" WB01626_.gif (272 bytes)[next]

PHP Diary | PHP Housekeeping | PHP Scripts | TD Scripts.com

Copyright 2000 php-scripts.com Last Modified 01/2/00 04:36