我想寫PHPMailer發送電子郵件。我使用mysqli_fetch_array的while循環從MySQL中提取電子郵件記錄,並將'email'數據庫字段分配給一個名爲'$ to'的變量,並提供給PHPMailer $ mailer-> AddAddress(「[email protected]」)呼叫。
該腳本可以工作,但僅向數據庫中發現的第一個收件人發送電子郵件。任何線索我在哪裏搞砸了?謝謝!
$from = '[email protected]';
$from_name = 'My Name';
$subject = $_POST['subject'];
$text = $_POST['elvismail'];
$dbc = mysqli_connect('localhost', 'username', 'the_password', 'database_name')
or die('Error connecting to mysql');
$query = "SELECT * FROM email_list";
$result = mysqli_query($dbc, $query)
or die('Error querying database.');
while ($row = mysqli_fetch_array($result)){
$to = $row['email'];
$first_name = $row['first_name'];
$last_name = $row['last_name'];
$msg = "Dear $first_name $last_name,\n$text";
}
require("PHPMailer_v5.1 2/class.phpmailer.php");
$mailer = new PHPMailer();
$mailer->IsSMTP();
$mailer->Host = 'ssl://smtp.gmail.com:465';
$mailer->SMTPAuth = TRUE;
$mailer->Username = '[email protected]'; // Sender's gmail address
$mailer->Password = 'the_password'; // Sender's gmail password
$mailer->From = "$from"; // Sender's email address
$mailer->FromName = "$from_name"; // senders name
$mailer->Body = "$msg";
$mailer->Subject = "$subject";
$mailer->AddAddress("$to"); // Recipient
if(!$mailer->Send())
{
echo 'Email sent to:' . $to . '<br/ >';
echo "Mailer Error: " . $mailer->ErrorInfo;
}
else
{
echo 'Email sent to:' . $to . '<br/ >';
}
mysqli_close($dbc);
這已在此處回答。 看看http://stackoverflow.com/questions/1770765/phpmailer-addaddress – 2011-03-08 23:56:42