Jump to content

send an email to each address from mySQL table


wright67uk

Recommended Posts

Hello, I've had someone write me a script that queries a MySQL table and sends the results to each of my members email addresses

(I have roughly 200 members).  The only thing is that for some reason it doesn't seem to actually send any emails.

 

The coder says it is an issue with my hosting account, can anyone spare a few minutes to tell me if the code makes sense?

<?php

ini_set('memory_limit', '-1');
//ini_set('meemory_limit', '96M');
//ini_set('post_max_size', '64M');
//ini_set('upload_max_filesize', '64M');
set_time_limit(0);
//get tips by id
include 'connection.php';
$curentDate = $_POST['date'];
if ($mysqli->connect_error) {
    die('Connect Error: ' . $mysqli->connect_error);
}

$query = "SELECT placed, win, id, date, horse, course, odds1, odds2, sp, place, time, tip, description, booky, date_added, time_added FROM toptips WHERE userid = 54 AND `date` ='" . $curentDate . "'  order by date desc";
$result = $mysqli->query($query);

while ($row = $result->fetch_array()) {
    $title = '(' . date("d/m/y", strtotime($row['date'])) . ") " . $row['time'] . " " . $row['course'] . " - " . $row['horse'] . " " . $row['odds1'] . "/" . $row['odds2'] . $row['sp'] . "-" . $row['place'];
    $des = $rows['description'];
    $title = nl2br($title);
    $des = nl2br($des);
    $rows[] = '<b>' . $title . '</b><br>' . $des;
}
$sendmailbody = '';
if (count($rows) > 0) {
    $sendmailbody = implode('<hr>', $rows);
}
// get all users from phpbb_user
$queryUser = "select username,user_email from phpbb_users where user_email !=''";
$resultUser = $mysqli->query($queryUser);
$rowsUser = array();
$toEmail = '';
while ($rowU = $resultUser->fetch_array()) {
    if (trim($rowU['user_email']) != '') {
        $rowsUser[] = array('user_email' => $rowU['user_email'], 'name' => $rowU['username']);
    }
}
$toEmail .= "[email protected]";
$to = '';
if (count($rowsUser) > 0) {
    /* config mail */
    require '/home/content/w/o/o/###/html/###/mailer/PHPMailerAutoload.php';


//end
    $tc = 0;
    $tb = 0;
    $mtc = '';
    $mtb = '';
    foreach ($rowsUser as $_user) {

        $to = $_user['user_email'];
        if ($to != '' && $sendmailbody != '') {
            //Create a new PHPMailer instance

            $mail = new PHPMailer();
            $mail->CharSet = "UTF-8";                                          
            //Set PHPMailer to use the sendmail transport

            $mail->isSendmail();    
            //Set who the message is to be sent from

            $mail->setFrom('[email protected]', 'TodaysTips');   
            //Set an alternative reply-to address

            $mail->addReplyTo('[email protected]', 'TodaysTips');

            //Set who the message is to be sent to
            $mail->addAddress($to, $_user['name']);
            $subject = 'Send all tips';
            $subject = nl2br($subject);
            $sendmailbody = nl2br($sendmailbody);
           
            //Set the subject line
            $mail->Subject = $subject;                     

            //Read an HTML message body from an external file, convert referenced images to embedded,
            //convert HTML into a basic plain-text alternative body

            $mail->msgHTML($sendmailbody);                 
            //Replace the plain text body with one created manually

            $mail->AltBody = 'This is a plain-text message body';
            $sendmail = $mail->send();
            if ($sendmail) {
                $mtc .= $to . ',';
                $tc = $tc + 1;
                sleep(5);
            } else {
                $tb = $tb + 1;
                $mtb .= $to . ',';
            }
        }
    }

    
    echo json_encode(array(
        'status' => 'success',
        'tc' => $tc . ": " . $mtc,
        'tb' => $tb . ": " . $mtb
    ));
}
?>


PhpMailerAutoload.php

function PHPMailerAutoload($classname)
{
    //Can't use __DIR__ as it's only in PHP 5.3+
    $filename = dirname(__FILE__).DIRECTORY_SEPARATOR.'class.'.strtolower($classname).'.php';
    if (is_readable($filename)) {
        require $filename;
    }
}

if (version_compare(PHP_VERSION, '5.1.2', '>=')) {
    //SPL autoloading was introduced in PHP 5.1.2
    if (version_compare(PHP_VERSION, '5.3.0', '>=')) {
        spl_autoload_register('PHPMailerAutoload', true, true);
    } else {
        spl_autoload_register('PHPMailerAutoload');
    }
} else {
    /**
     * Fall back to traditional autoload for old PHP versions
     * @param string $classname The name of the class to load
     */
    function __autoload($classname)
    {
        PHPMailerAutoload($classname);
    }
}

Sure, where should I echo $to? 

 

After $to = $_user['user_email'];

foreach ($rowsUser as $_user) {
 
$to = $_user['user_email'];

echo $to; //here


if ($to != '' && $sendmailbody != '') {
//Create a new PHPMailer instance
........................................

Don't show me the full list of emails only the pattern (format).

 

 Also, Use prepared statements and parameterized queries to prevent SQL injection(s) in PHP.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.