PHPMailer MySQLi多个电子邮件地址


PHPMailer MySQLi multiple email addresses

我已经忍无可忍了,非常感谢您的帮助。

我有下面的代码,虽然它在个人的基础上工作,我可以为我的生活不使它工作,以便所有的电子邮件地址在同一时间使用PHPMailer发送。

我在过去的几个月里搜索了StackOverflow,尝试了大量的组合都没有成功。有一些讨论在这个论坛上,虽然我已经尝试了所有的解决方案在这里,我仍然不能使它工作。如果我重复的问题冒犯了任何人,请提前接受我的道歉。

<?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);
}

你说你到处都找遍了…除了你已经拥有的代码!PHPMailer提供了一个邮件列表示例,它可以准确、有效地完成您的要求。

在你的代码中,你试图将一条消息发送给50个人,而不是50对50,并且它还会将所有地址暴露给所有收件人,所以这通常是一个坏主意。

您的代码基于一个过时的示例,并且可能正在使用旧版本的PHPMailer,因此获取最新版本。

相关文章: