wright67uk Posted January 4, 2014 Share Posted January 4, 2014 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 .= "test@test.com"; $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('TodaysTips@thenovicetipster.com', 'TodaysTips'); //Set an alternative reply-to address $mail->addReplyTo('TodaysTips@thenovicetipster.com', '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); } } Quote Link to comment https://forums.phpfreaks.com/topic/285085-send-an-email-to-each-address-from-mysql-table/ Share on other sites More sharing options...
jazzman1 Posted January 4, 2014 Share Posted January 4, 2014 Can I see the output of $to variable? Quote Link to comment https://forums.phpfreaks.com/topic/285085-send-an-email-to-each-address-from-mysql-table/#findComment-1463822 Share on other sites More sharing options...
wright67uk Posted January 4, 2014 Author Share Posted January 4, 2014 Sure, where should I echo $to? If I echo it just before the php closing tag I get; {"status":"success","tc":"0: ","tb":"0: "}1emailaddress@gmail.com PS. thank you for such a quick reply :-) Quote Link to comment https://forums.phpfreaks.com/topic/285085-send-an-email-to-each-address-from-mysql-table/#findComment-1463823 Share on other sites More sharing options...
jazzman1 Posted January 4, 2014 Share Posted January 4, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/285085-send-an-email-to-each-address-from-mysql-table/#findComment-1463826 Share on other sites More sharing options...
wright67uk Posted January 4, 2014 Author Share Posted January 4, 2014 email@gmail.comemail@hotmail.co.ukemail@hotmail.comemail@hotmail.co.ukemail@hotmail.comemail@outlook.comemail@hotmail.co.uk // ....and on and on {"status":"success","tc":"0: ","tb":"0: "} no spaces or characters between email addresses. Quote Link to comment https://forums.phpfreaks.com/topic/285085-send-an-email-to-each-address-from-mysql-table/#findComment-1463829 Share on other sites More sharing options...
jazzman1 Posted January 4, 2014 Share Posted January 4, 2014 I'm going to play tennis and don't have the time right now to get into it. Go to phpMailer web site and check what the proper format is, when you're sending multiple emails, b/s I think this is wrong. Quote Link to comment https://forums.phpfreaks.com/topic/285085-send-an-email-to-each-address-from-mysql-table/#findComment-1463830 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.