I am tring to create my own mailing list. I want to be able to store the email addresses in a MySQL table, retrieve them, and enter a message in a web form, and have the email sent to the multiple users. I have included the codes I have been working with. Thanks for any help you can give!

Kevin Muller

////code for mail_list.php/////
<?
$db_name = "thejuggalos";
$table_name = "mail_list";
$connection = @mysql_connect("localhost", "account", "password") or die("Error 1");
$db = @mysql_select_db($db_name, $connection) or die("Error 2");
$sql = "SELECT email FROM $table_name";
$result = @($sql, $connection) or die("Error 3");
while ($row = mysql_fetch_array($result)) {

$email = $row['email'];
;

$result1 .= "
$email";
}

?>

<head>
<style type="text/css">
a:hover {color:white}
a {text-decoration:none}
</style>
<title>JuggaloDirectory.com</title>
</head>
<body>
<form method="post" action="send_list.php">
<b>Email To:</b><br>
<textarea cols=30 rows=5 name="juggalos" size=30><? echo "$result1"; ?></textarea>
<br><br>
<b>Message:</b>
<br><textarea name="message" cols=30 rows=5 wrap=virtual></textarea>
<br><br>
<input type="submit" name="submit" value="Send This Form">
</form>
</body>

////code for send_list.php/////

<?
/ recipients /
$recipient .= "$juggalos" . ", " ;

$subject .= "TheJuggalos.com Mailing";

/ additional header pieces for errors, From cc's, bcc's, etc /

$headers .= "From: WeBmAsTa <juggalo_wicked@hotmail.com>\n";
$headers .= "X-Sender: <birthday@php.net>\n";
$headers .= "X-Mailer: PHP\n"; // mailer
$headers .= "X-Priority: 1\n"; // Urgent message!
$headers .= "Return-Path: <birthday@php.net>\n"; // Return path for errors

/ If you want to send html mail, uncomment the following line /
// $headers .= "Content-Type: text/html; charset=iso-8859-1\n"; // Mime type

$headers .= "cc:admin@juggalodirectory.com\n"; // CC to
$headers .= "bcc:juggalo_wicked@yahoo.com, birthdaygifts@php.net\n"; // BCCs to

/ and now mail it /
mail($recipient, $subject, $message, $headers);

?>

<BODY>
The emails have been sent!
</body>

    Keep in mind that this kind of a mailinglist will only function for up to (about) 1500 receipients...
    If youll want to mail to any more than that, youll have to use the flush() function to continously update your web page, or else the browser will give the user a "page-does-not-exist" error...!

    -Trond

      I forgot to mention that this code isn't working. It only sends it to the first email listed. Any ideas why?

      Thanks,

      Kevin

        I would think that the reason why it oonly sends to the first email listed, is because you have to send the mail within the "while" statement where you fetch out those email adresses.

        $sql = "SELECT email FROM newsletter" ;
        $sql_query = mysql_query($sql, $link) ;
        while($row = $mysql_fetch_array($sql_query)) {

        $email = mail($row[], "Subject", "email", "headers") ;
        if !($email) {
        echo "Could not send to $row[]" ;
        }

        } // End while

        Then for each email address that is fetched out of the database, the script sends an email.

        How many subscribers do you have to this list??

          a month later

          Hello,

          I have a similar problem, and you seem to know about this issue.
          I wrote some code that looks roughly like this one, and it works perfectly when I'm testing it with a dozen e-mail addresses. But when I wanted to use it with my 1500 subscriber mailing-list, it became a true nightmare. Lots of them didn't get the newsletter, while other lots of them got it up to 5 times! I really don't know why that happened, apparently you say it has something to do with the flush() function?
          Or is there another way that is usually used when you have many subscribers?

          Thanks a lot for your answer.

          Thanassis.

            4 months later

            I'm having the same problem. It only emails to the original recipient. I thought adding a BCC header with all the email addresses (3 at this point) would work, but it doesn't.

            Short of running a while statement, what else would work? Shouldn't the BCC header with multiple addresses work?

            Tanx!

              3 months later

              Usually many MTA does not support "many" recipients in mail header. Its OK sometimes for 10 or 100, but it's depend on your configuration of MTA used.

                2 months later

                Hey Trond,

                Good info on the flush function in this context. I'm dealing with the issue of a huge mailing list that chokes and this may be just the ticket. My question, however, is do you know of a better way to handle a mail list than the mail function? Is there a more effecient way to send mail to 3000+ recipients?

                Thanks,
                -Ryan

                  Hey Ryan
                  Glad my work can be of help 🙂
                  When I first started finding info on how to send mail using mail(), everybody disencoureged me from doing so.

                  Apparently, the best way is to have a mailinglist like majordomo..
                  It is more efficient too...

                  But I have had no problem with my mailinglist so far...
                  I've also heard of ppl that are sending up to 7000 mails every day with a php-mailinglist prog..

                  What you should do, is to sort all the mail adresses so that the prog will send mail to addresses with the same domainname in one bunch...
                  That can speed up your php-mail function..

                  But, apparantly, the best way to send lots of mails is by using a mailinglist prog.

                  Haven't done it myself, though.

                  • Trond -

                    Yeah, I'll have to check into using majordomo with PHP. Unfortunately, these people that I am doing the list for want to send their messages to each individual email address and include the persons name in the message, so ordering by domain name wouldn't help me much. How does your code work, in a nutshell? Do you just loop through the addresses and mail each address as part of the loop, or do you build a recipient list and then mail once to the whole list? Where does flush work into yours?

                    Thanks.
                    -Ryan

                      Our code is pretty straight.
                      We have several email db's the user can select from, biggest has about 2200 recipients.
                      Then selecty html/text format.
                      Are there any files to be included?
                      Preview of the mail.

                      Then it is sent.
                      In the while loop where I loop through all the email adresses in the db, I send the email and use the flush, and print a * to the screen.

                      If a mail is successfully sent, it is stored in a log.
                      When all the mails are sent, the script then shows users the finish screen.

                        What I meant by ordering by domain, is to separate the username from the domainname in the email address (user@domain.com).

                        You can prob do that in the sql.
                        ... ORDER by regex(email, "@") or something... 😉

                        Sending all the mails that has hotmail.com in one bunch improves speed of script.
                        obviously. I haven't had the time to do this myself, but if you are serious about a php-mailinglist, I think it is a good idea.

                          Write a Reply...