PHPMailer MySQLi 多个电子邮件地址
PHPMailer MySQLi multiple email addresses
我已经筋疲力尽了,非常感谢您能提供的任何帮助...
我有以下代码,虽然它可以在个人基础上运行,但我不能让它运行,以便使用 PHPMailer 同时发送所有电子邮件地址。
在过去的几个月里,我搜索了 Whosebug,尝试了大量的组合,但都没有成功。
这个论坛上有很多讨论,虽然我已经尝试了这里所有的解决方案,但我仍然无法解决问题。如果我可能重复问题冒犯了任何人,请提前接受我的道歉。
<?php
// Script Error Reporting
//error_reporting(0);
// Require the form_functions to process the form
require('databaseConnect.php');
// Require the Email Class Functions
require("mailApp/class.phpmailer.php");
require("mailApp/class.smtp.php");
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPKeepAlive=true; // SMTP connection will not close after each email sent, reduces SMTP overhead
$mail->setFrom("No_Reply@girrawaagames.com", "Girrawaa Games"); // Valid email address from sender and Company name. Only Company name will be displayed
$mail->Subject='CASH Trader | The Spirit Stone'; // This adds the subject title in the subject line field
// Create a connection to MySQL and the database:
$con = mysqli_connect($hostname, $username, $password, $database);
// Check if the connection is active:
if(!$con) {
header('Location: alternate.php');
}
$result = mysqli_query($con, "SELECT fname, email FROM emails WHERE condition = 'condition'");
/* fetch associative array */
while ($row = $result->fetch_array()) {
$user['fname'][] = $row["fname"];
$user['email'][] = $row["email"];
}
$mail->AddAddress($user['email']);
$mail->Body="<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">
<html xmlns=\"http://www.w3.org/1999/xhtml\">
<head>
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />
<title>CASH Trader</title>
<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\" />
</head>
<body yahoo bgcolor=\"#ccffff\" style=\"margin:0; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px;\">
<table align=\"center\" cellpadding=\"0\" cellspacing=\"0\" width=\"600\" style=\"border-collapse:collapse\">
<tr>
<td style=\"padding-top:20px; padding-right:0px; padding-bottom:30px; padding-left:0px; color:#153643; font-size:28px; font-weight:bold; font-family:Arial, Helvetica, sans-serif;\">
<table align=\"center\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"600\">
<tr>
<td class=\"header\" align=\"center\" bgcolor=\"#333333\" style=\"padding-top:10px; padding-right:10px; padding-bottom:10px; padding-left:10px; color:#153643; font-size:28px; font-weight:bold; font-family:Arial, Helvetica, sans-serif;\">
<a href=\"http://www.girrawaagames.com\"><img src=\"http://www.girrawaagames.com/img/logo.jpg\" style=\"color:#FFFFFF\" alt=\"Cash Trader Logo\" style=\"display:block; max-width:100%; height:auto;\" /></a>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td bgcolor=\"#ffffff\" style=\"padding-top:40px; padding-right:30px; padding-bottom:20px; padding-left:30px;\">
<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\">
<tr>
<td align=\"center\" style=\"color:#153643; font-family:Arial, Helvetica, sans-serif; font-size:24px;\">
<b>Hi " . $user['fname'] . "</b>
</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>";
print_r($user['fname']);
print_r($user['email']);
if ($mail->send()) {
$updateCampaign = "UPDATE emails SET column = 'value' WHERE email = '" . mysqli_real_escape_string($row['email']) . "'";
$results = mysqli_query($con, $updateCampaign);
}
// Clear all addresses and attachments for next loop
$mail->clearAddresses();
mysqli_free_result($result);
mysqli_close($con);
您需要连接 $useremail
才能包含多个电子邮件。
$userEmail = '';
foreach($rows as $row) {
$userFname = $row["fname"];
$userEmail .= $row["email"] . ',';
}
在数组中将它们配对可能会更容易..
foreach($rows as $row) {
$user['fname'][] = $row["fname"];
$user['email'][] = $row["email"];
}
那以后就不需要explode
函数了
.....
或者更好的是在 while
循环中设置它,根本不需要 foreach
。
while($row = $result->fetch_array()){
$user['fname'][] = $row["fname"];
$user['email'][] = $row["email"];
}
更新:
<?php
// Script Error Reporting
//error_reporting(0);
// Require the form_functions to process the form
require('databaseConnect.php');
// Require the Email Class Functions
require("mailApp/class.phpmailer.php");
require("mailApp/class.smtp.php");
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPKeepAlive=true; // SMTP connection will not close after each email sent, reduces SMTP overhead
$mail->setFrom("No_Reply@girrawaagames.com", "Girrawaa Games"); // Valid email address from sender and Company name. Only Company name will be displayed
$mail->Subject='CASH Trader | The Spirit Stone'; // This adds the subject title in the subject line field
// Create a connection to MySQL and the database:
$con = mysqli_connect($hostname, $username, $password, $database);
// Check if the connection is active:
if(!$con) {
header('Location: alternate.php');
exit();
}
$result = mysqli_query($con, "SELECT fname, email FROM emails WHERE condition = 'condition'");
/* fetch associative array */
while ($row = $result->fetch_array()) {
$user['fname'][] = $row["fname"];
$user['email'][] = $row["email"];
}
foreach($user['email'] as $key => $email) {
$mail->AddAddress($email);
$mail->Body="<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">
<html xmlns=\"http://www.w3.org/1999/xhtml\">
<head>
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />
<title>CASH Trader</title>
<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\" />
</head>
<body yahoo bgcolor=\"#ccffff\" style=\"margin:0; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px;\">
<table align=\"center\" cellpadding=\"0\" cellspacing=\"0\" width=\"600\" style=\"border-collapse:collapse\">
<tr>
<td style=\"padding-top:20px; padding-right:0px; padding-bottom:30px; padding-left:0px; color:#153643; font-size:28px; font-weight:bold; font-family:Arial, Helvetica, sans-serif;\">
<table align=\"center\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"600\">
<tr>
<td class=\"header\" align=\"center\" bgcolor=\"#333333\" style=\"padding-top:10px; padding-right:10px; padding-bottom:10px; padding-left:10px; color:#153643; font-size:28px; font-weight:bold; font-family:Arial, Helvetica, sans-serif;\">
<a href=\"http://www.girrawaagames.com\"><img src=\"http://www.girrawaagames.com/img/logo.jpg\" style=\"color:#FFFFFF\" alt=\"Cash Trader Logo\" style=\"display:block; max-width:100%; height:auto;\" /></a>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td bgcolor=\"#ffffff\" style=\"padding-top:40px; padding-right:30px; padding-bottom:20px; padding-left:30px;\">
<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\">
<tr>
<td align=\"center\" style=\"color:#153643; font-family:Arial, Helvetica, sans-serif; font-size:24px;\">
<b>Hi " . $user['fname'][$key] . "</b>
</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>";
if ($mail->send()) {
$updateCampaign = "UPDATE emails SET column = 'value' WHERE email = '" . mysqli_real_escape_string($row['email']) . "'";
$results = mysqli_query($con, $updateCampaign);
}
// Clear all addresses and attachments for next loop
$mail->clearAddresses();
mysqli_free_result($result);
mysqli_close($con);
}
你说你到处都看过...除了你已有的代码! a mailing list example provided with PHPMailer 可以正确有效地完成您的要求。
在您的代码中,您尝试将一封邮件发送给 50 个人,而不是 50 到 50 人,并且它还会向所有收件人公开所有地址,因此这通常不是一个好主意。
您的代码基于一个过时的示例,并且可能使用的是旧版本的 PHPMailer,因此 get the latest。
我已经筋疲力尽了,非常感谢您能提供的任何帮助...
我有以下代码,虽然它可以在个人基础上运行,但我不能让它运行,以便使用 PHPMailer 同时发送所有电子邮件地址。
在过去的几个月里,我搜索了 Whosebug,尝试了大量的组合,但都没有成功。 这个论坛上有很多讨论,虽然我已经尝试了这里所有的解决方案,但我仍然无法解决问题。如果我可能重复问题冒犯了任何人,请提前接受我的道歉。
<?php
// Script Error Reporting
//error_reporting(0);
// Require the form_functions to process the form
require('databaseConnect.php');
// Require the Email Class Functions
require("mailApp/class.phpmailer.php");
require("mailApp/class.smtp.php");
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPKeepAlive=true; // SMTP connection will not close after each email sent, reduces SMTP overhead
$mail->setFrom("No_Reply@girrawaagames.com", "Girrawaa Games"); // Valid email address from sender and Company name. Only Company name will be displayed
$mail->Subject='CASH Trader | The Spirit Stone'; // This adds the subject title in the subject line field
// Create a connection to MySQL and the database:
$con = mysqli_connect($hostname, $username, $password, $database);
// Check if the connection is active:
if(!$con) {
header('Location: alternate.php');
}
$result = mysqli_query($con, "SELECT fname, email FROM emails WHERE condition = 'condition'");
/* fetch associative array */
while ($row = $result->fetch_array()) {
$user['fname'][] = $row["fname"];
$user['email'][] = $row["email"];
}
$mail->AddAddress($user['email']);
$mail->Body="<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">
<html xmlns=\"http://www.w3.org/1999/xhtml\">
<head>
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />
<title>CASH Trader</title>
<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\" />
</head>
<body yahoo bgcolor=\"#ccffff\" style=\"margin:0; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px;\">
<table align=\"center\" cellpadding=\"0\" cellspacing=\"0\" width=\"600\" style=\"border-collapse:collapse\">
<tr>
<td style=\"padding-top:20px; padding-right:0px; padding-bottom:30px; padding-left:0px; color:#153643; font-size:28px; font-weight:bold; font-family:Arial, Helvetica, sans-serif;\">
<table align=\"center\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"600\">
<tr>
<td class=\"header\" align=\"center\" bgcolor=\"#333333\" style=\"padding-top:10px; padding-right:10px; padding-bottom:10px; padding-left:10px; color:#153643; font-size:28px; font-weight:bold; font-family:Arial, Helvetica, sans-serif;\">
<a href=\"http://www.girrawaagames.com\"><img src=\"http://www.girrawaagames.com/img/logo.jpg\" style=\"color:#FFFFFF\" alt=\"Cash Trader Logo\" style=\"display:block; max-width:100%; height:auto;\" /></a>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td bgcolor=\"#ffffff\" style=\"padding-top:40px; padding-right:30px; padding-bottom:20px; padding-left:30px;\">
<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\">
<tr>
<td align=\"center\" style=\"color:#153643; font-family:Arial, Helvetica, sans-serif; font-size:24px;\">
<b>Hi " . $user['fname'] . "</b>
</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>";
print_r($user['fname']);
print_r($user['email']);
if ($mail->send()) {
$updateCampaign = "UPDATE emails SET column = 'value' WHERE email = '" . mysqli_real_escape_string($row['email']) . "'";
$results = mysqli_query($con, $updateCampaign);
}
// Clear all addresses and attachments for next loop
$mail->clearAddresses();
mysqli_free_result($result);
mysqli_close($con);
您需要连接 $useremail
才能包含多个电子邮件。
$userEmail = '';
foreach($rows as $row) {
$userFname = $row["fname"];
$userEmail .= $row["email"] . ',';
}
在数组中将它们配对可能会更容易..
foreach($rows as $row) {
$user['fname'][] = $row["fname"];
$user['email'][] = $row["email"];
}
那以后就不需要explode
函数了
.....
或者更好的是在 while
循环中设置它,根本不需要 foreach
。
while($row = $result->fetch_array()){
$user['fname'][] = $row["fname"];
$user['email'][] = $row["email"];
}
更新:
<?php
// Script Error Reporting
//error_reporting(0);
// Require the form_functions to process the form
require('databaseConnect.php');
// Require the Email Class Functions
require("mailApp/class.phpmailer.php");
require("mailApp/class.smtp.php");
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPKeepAlive=true; // SMTP connection will not close after each email sent, reduces SMTP overhead
$mail->setFrom("No_Reply@girrawaagames.com", "Girrawaa Games"); // Valid email address from sender and Company name. Only Company name will be displayed
$mail->Subject='CASH Trader | The Spirit Stone'; // This adds the subject title in the subject line field
// Create a connection to MySQL and the database:
$con = mysqli_connect($hostname, $username, $password, $database);
// Check if the connection is active:
if(!$con) {
header('Location: alternate.php');
exit();
}
$result = mysqli_query($con, "SELECT fname, email FROM emails WHERE condition = 'condition'");
/* fetch associative array */
while ($row = $result->fetch_array()) {
$user['fname'][] = $row["fname"];
$user['email'][] = $row["email"];
}
foreach($user['email'] as $key => $email) {
$mail->AddAddress($email);
$mail->Body="<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">
<html xmlns=\"http://www.w3.org/1999/xhtml\">
<head>
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />
<title>CASH Trader</title>
<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\" />
</head>
<body yahoo bgcolor=\"#ccffff\" style=\"margin:0; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px;\">
<table align=\"center\" cellpadding=\"0\" cellspacing=\"0\" width=\"600\" style=\"border-collapse:collapse\">
<tr>
<td style=\"padding-top:20px; padding-right:0px; padding-bottom:30px; padding-left:0px; color:#153643; font-size:28px; font-weight:bold; font-family:Arial, Helvetica, sans-serif;\">
<table align=\"center\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"600\">
<tr>
<td class=\"header\" align=\"center\" bgcolor=\"#333333\" style=\"padding-top:10px; padding-right:10px; padding-bottom:10px; padding-left:10px; color:#153643; font-size:28px; font-weight:bold; font-family:Arial, Helvetica, sans-serif;\">
<a href=\"http://www.girrawaagames.com\"><img src=\"http://www.girrawaagames.com/img/logo.jpg\" style=\"color:#FFFFFF\" alt=\"Cash Trader Logo\" style=\"display:block; max-width:100%; height:auto;\" /></a>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td bgcolor=\"#ffffff\" style=\"padding-top:40px; padding-right:30px; padding-bottom:20px; padding-left:30px;\">
<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\">
<tr>
<td align=\"center\" style=\"color:#153643; font-family:Arial, Helvetica, sans-serif; font-size:24px;\">
<b>Hi " . $user['fname'][$key] . "</b>
</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>";
if ($mail->send()) {
$updateCampaign = "UPDATE emails SET column = 'value' WHERE email = '" . mysqli_real_escape_string($row['email']) . "'";
$results = mysqli_query($con, $updateCampaign);
}
// Clear all addresses and attachments for next loop
$mail->clearAddresses();
mysqli_free_result($result);
mysqli_close($con);
}
你说你到处都看过...除了你已有的代码! a mailing list example provided with PHPMailer 可以正确有效地完成您的要求。
在您的代码中,您尝试将一封邮件发送给 50 个人,而不是 50 到 50 人,并且它还会向所有收件人公开所有地址,因此这通常不是一个好主意。
您的代码基于一个过时的示例,并且可能使用的是旧版本的 PHPMailer,因此 get the latest。