PHPMailer MySQLi多个电子邮件地址

时间:2015-08-14 00:17:27

标签: php mysqli phpmailer

我在绳子的尽头,非常感谢您可以提供的任何帮助......

我有以下代码,虽然它可以单独使用,但我可以在生活中使其无法正常工作,以便所有电子邮件地址都可以使用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);

2 个答案:

答案 0 :(得分:0)

您需要将$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);
}

答案 1 :(得分:0)

你说你到处都是......除了你已有的代码! a mailing list example provided with PHPMailer正确而有效地完成了您的要求。

在您的代码中,您尝试向50个人发送一条消息,而不是50到50,并且它还会向所有收件人公开所有地址,因此这通常是一个坏主意。

您的代码基于一个过时的示例,并且可能使用旧版本的PHPMailer,因此get the latest