Swiftmailer:发送两次,检查所有常见的罪魁祸首

时间:2016-01-07 23:14:32

标签: php swiftmailer

所以这是代码。它只发生在一些用户身上,但经常发生这种情况会让人感到恼火。我检查了所有常见的罪魁祸首(IE额外发送命令等)

当脚本完成时,我也会收到错误,显示Warning: Unknown: Error occurred while closing statement in Unknown on line这显然告诉我什么都没有,或者这两个问题是否相关。

<?php

$row = array();
$sql = "SELECT username, email, role FROM newsletter";
require_once '../includes/db_connect.php';

$stmt = $conn->prepare($sql);
$stmt->execute();
$stmt->store_result();
$meta = $stmt->result_metadata(); 

while ($field = $meta->fetch_field()) {
    $params[] = &$row[$field->name]; 
} 
call_user_func_array(array($stmt, 'bind_result'), $params);

while ($stmt->fetch()) { 
    foreach($row as $key => $val) { 
        if($val === 'Not Found' || $val == ''){
        continue;
        }else {
        $c[$key] = $val; 
        }   
    } 
    $row[] = $c;
}

$htmlEmail = 'blah blah blah';

$textEmail = "blah blah bah";

require_once '../lib/swift_required.php';
// Create the Mailer using any Transport
$mailer = Swift_Mailer::newInstance(
  Swift_SmtpTransport::newInstance('mail.****.com', 25)
  ->setUsername('sales@*****.com')
  ->setPassword('********')
);

// Rate limit to 100 emails per-minute
$mailer->registerPlugin(new Swift_Plugins_ThrottlerPlugin(
    50, Swift_Plugins_ThrottlerPlugin::MESSAGES_PER_MINUTE
));

// Create the message
$message = Swift_Message::newInstance();

// Give the message a subject
$message->setSubject('Sorry about that we had a glitch!');

// Set the From address with an associative array
$message->setFrom(array('sales@*****.com' => 'Auction Hunter Pro'));

// Give it a body
$message->setBody($textEmail);

// And optionally an alternative body
$message->addPart($htmlEmail, 'text/html');

 foreach($row as $user) {
     if(is_array($user) && !empty($user['username'])){
         $message->setTo($user['email']);
         $mailer->send($message);
     }else { continue; }
  }

0 个答案:

没有答案