This is an email blast script that is suppose to fire an email to all emails in my sql database but its not happening... What is wrong? This code is several years old.

<?php

include_once "connect_to_mysql.php";
$sql = mysql_query("SELECT * FROM blast WHERE received='0' LIMIT 20");
$numRows = mysql_num_rows($sql); // Added for "End Campaign Check" at the bottom of this file(not shown on the video)
$mail_body = '';
while($row = mysql_fetch_array($sql)){
	$id = $row["id"];
	$email = $row["email"];
	$name = $row["name"];

$mail_body = '<html>
<body style="background-color:#CCC; color:#000; font-family: Arial, Helvetica, sans-serif; line-height:1.8em;">
<h3><a href="http://www.developphp.com"><img src="http://www.yoursite.com/images/logo.png" alt="DevelopPHP" width="216" height="36" border="0"></a> Newsletter
</h3>
<p>Hello ' . $name . ',</p>
<p>You can make this out to be just like most any web page or design format you require using HTML and CSS.</p>
<p>~Adam @ DevelopPHP</p>
<hr>
<p>To opt out of receiving this newsletter,  <a href="http://triwebworks.com/capiovo.com/newsletter/optout.php?e=' . $email . '">click here</a> and we will remove you from the listing immediately.</p>
</body>
</html>';
    $subject = "Develop PHP Newsletter";
    $headers  = "From:newsletter@developphp.com\r\n";
    $headers .= "Content-type: text/html\r\n";
    $to = "$email";

$mail_result = mail($to, $subject, $mail_body, $headers);

if ($mail_result) {
	mysql_query("UPDATE blast SET received='1' WHERE email='$email' LIMIT 1");
} else {
   // this else statement can be used to write into an error log if the mail function fails
   // It can also be removed if you do not need error logging
}

}
?>
<?php
// This section is script I discussed adding to this file on video
// This section is for sending the site owner a message informing them that
// all people in the database have been sent the newsletter for the current campaign
if ($numRows == 0) { // $numRows is set on line 4 using the existing query

 $subj = "Newsletter Campaign Has Ended";
 $body = "The current newsletter campaign has ended. All have been sent the newsletter.";
 $hdr  = "From:joseph@fleckllc.com\r\n";
 $hdr .= "Content-type: text/html\r\n";
 mail("joseph@jfleckllc.com", $subj, $body, $hdr);

}
// End Check Section
?>
    jfleck25;11054527 wrote:

    This is an email blast script that is suppose to fire an email to all emails in my sql database but its not happening... What is wrong? This code is several years old.

    Answering your own questions? 😉

    Q: What is wrong?
    A: This code is several years old.

    But seriously, and don't take any of this personally ... the first thing you should do is verify that the DB connection works. And then verify that it actually returns something that would allow the script to work. I'd hesitate to call this script "trash", but it doesn't bother verify anything before going to the next step.

    Oh, and this:

    to all emails in my sql database

    is counteracted by this:

    LIMIT 20

    So it will only send it to your entire database if your entire database is 20 people or less.

    If you really don't know how to debug this, I'd recommend reading [thread=10240608]Debugging 101[/thread].

      I'm aware of the limit 20. I need a script that I can blast out to my mysql database full of emails. If this is trash do you know of any scripts I could get to take it's place? I'm not smart enough to design one from scratch

        Using the built-in PHP mail() function is not a good way to go about this if the volume of mail will be large (for an undefined value of "large"). From the mail() manual page:

        Note:

        It is worth noting that the mail() function is not suitable for larger volumes of email in a loop. This function opens and closes an SMTP socket for each email, which is not very efficient.
        
        For the sending of large amounts of email, see the » [URL="http://pear.php.net/package/Mail"]PEAR::Mail[/URL], and » [URL="http://pear.php.net/package/Mail_Queue"]PEAR::Mail_Queue[/URL] packages.

        And possibly the best solution in terms of handling the volume and avoiding being flagged as a spammer is to use a reputable mass email provider, likely supplying your list of emails via their API.

          Okay thanks, So I need a new script. can someone please provide me with one? I cant find anything on google.

            17 days later

            There's php mailer and Swift mailer.
            They both need care in setting up so as to avoid being marked as a spam sender.
            It could well be more involved than you're hoping for.

              jfleck25;11054573 wrote:

              Okay thanks, So I need a new script. can someone please provide me with one? I cant find anything on google.

              Sure! Let us all get right on that for you! :rolleyes:

                Write a Reply...