我试图使用PHPMailer发送多封电子邮件(向每位收件人发送不同的电子邮件)

时间:2014-09-25 20:58:06

标签: php email phpmailer

第一次发帖,所以任何帮助都非常感谢...

  • 我很高兴SQL查询有效并返回两行
  • 我很高兴留言,#34;你好"现在
  • 我看了很多答案,没有解决我的问题
  • 此功能仅将邮件发送给第一个(第2个)收件人,而不是第二个收件人。

如果有人能告诉我哪里出错了,我将非常感激......

public function sendOrderEmail($customer_order_id)
{ 
$sql3=" SELECT DISTINCT     w.user_id AS supplier_id, 
                            s.trading_name AS supplier_name, 
                            s.contact_email AS supplier_email  
        FROM supplier_info AS s 
        JOIN wine AS w ON w.user_id = s.id
        JOIN order_detail AS o ON o.wine_id = w.id
        WHERE o.order_no_id = :customer_order_id ORDER BY supplier_id DESC;";

$query3 = $this->db->prepare($sql3);
$query3->execute(array(':customer_order_id' => intval($customer_order_id)));

//$result3 = $query3->fetchAll();
while($row3 = $query3->fetch(PDO::FETCH_ASSOC)){
     //  while ($row3 = $query3->fetch()) {

         $supplier_name = $row3['supplier_name'];
         $supplier_email = $row3['supplier_email'];

// foreach($result3 as $key => $output){
    //$supplier_id = $output->supplier_id;
  //  $supplier_email = $output->email; */

    $mail = new PHPMailer;

        if (EMAIL_USE_SMTP) {
                            $mail->IsSMTP();
                            $mail->SMTPDebug = PHPMAILER_DEBUG_MODE;
                            $mail->SMTPAuth = EMAIL_SMTP_AUTH;

                            if (defined('EMAIL_SMTP_ENCRYPTION')) {
                            $mail->SMTPSecure = EMAIL_SMTP_ENCRYPTION;
                            }

                            $mail->Host = EMAIL_SMTP_HOST;
                            $mail->Username = EMAIL_SMTP_USERNAME;
                            $mail->Password = EMAIL_SMTP_PASSWORD;
                            $mail->Port = EMAIL_SMTP_PORT;
        } else {
                            $mail->IsMail();
        }

// Build email body
        $message = "<p>hello</p>";

         // fill mail with data
$mail->isHTML(true); 
$mail->From = "billyjlennon@gmail.com";
$mail->FromName = "Me";
$mail->AddAddress($supplier_email);
$mail->Subject = "Your request ";
$mail->Body = $message;

   // final sending and check
    if($mail->Send()) {
        $_SESSION["feedback_positive"][] = FEEDBACK_CONTACT_MAIL_SENDING_SUCCESSFUL;
        return true;
    } else {
        $_SESSION["feedback_negative"][] = FEEDBACK_CONTACT_MAIL_SENDING_ERROR . $mail-        >ErrorInfo;
        return false;
    } 
} 

2 个答案:

答案 0 :(得分:0)

首先,您应该首先检查PHPMailer文档,因为an example that does exactly this。你不可能看起来太难。

在你的代码中,不要每次在循环周围创建一个新的PHPMailer实例 - 所有保持不变的东西(isSmtp,Host,Port等)应该在循环之前设置一次。在循环中你应该做这些事情:

  • 设置邮件正文
  • 设置收件人
  • 发送消息
  • 存储任何错误
  • 清除收件人列表(使用clearAllRecipients()

您的发送检查后您还收到了return个语句 - 这些语句将完全退出您的功能,因此它永远不会发送多条消息。您应该在列表中收集错误并在最后显示(或返回)它们。

答案 1 :(得分:0)

我在上面的@synchro建议的addAddress周围使用了一个while循环。 Foreach迭代器语法不会像我使用PHP 5.3一样工作(根据上面PHPmailer链接中的注释)。

然后我在while循环之外接受了返回,一切正常。

这是有效的代码: (感激地接受任何改进)

public function sendOrderEmail($customer_order_id)
{
    $sql3 = " SELECT DISTINCT     w.user_id AS supplier_id,
                                s.trading_name AS supplier_name,
                                s.contact_email AS supplier_email
            FROM supplier_info AS s
            JOIN wine AS w ON w.user_id = s.id
            JOIN order_detail AS o ON o.wine_id = w.id
            WHERE o.order_no_id = :customer_order_id ORDER BY supplier_id DESC;";

    $query3 = $this->db->prepare($sql3);
    $query3->execute(array(':customer_order_id' => intval($customer_order_id)));

    $mail = new PHPMailer;

    if (EMAIL_USE_SMTP) {
        $mail->IsSMTP();
        $mail->Host = EMAIL_SMTP_HOST;
        $mail->Username = EMAIL_SMTP_USERNAME;
        $mail->Password = EMAIL_SMTP_PASSWORD;
        $mail->Port = EMAIL_SMTP_PORT;
        $mail->SMTPDebug = PHPMAILER_DEBUG_MODE;
        $mail->SMTPAuth = EMAIL_SMTP_AUTH;
        $mail->SMTPKeepAlive = true;
        if (defined('EMAIL_SMTP_ENCRYPTION')) {
            $mail->SMTPSecure = EMAIL_SMTP_ENCRYPTION;
        }
    } else {
        $mail->IsMail();
    }

    $mail->isHTML(true);
    $mail->From = "billyjlennon@gmail.com";
    $mail->FromName = "Me";

    while ($row3 = $query3->fetch(PDO::FETCH_ASSOC)) {
        // Build email body
        $message = "<p>hello</p>";

        // fill mail with data
        $mail->AddAddress($row3['supplier_email']);
        $mail->Subject = "Your request to " . $row3['supplier_name'];
        $mail->Body = $message;

        // final sending and check
        if ($mail->Send()) {
            //  $_SESSION["feedback_positive"][] = FEEDBACK_CONTACT_MAIL_SENDING_SUCCESSFUL;
            //return true;
        } else {
            $_SESSION["feedback_negative"][] = FEEDBACK_CONTACT_MAIL_SENDING_ERROR . $mail->ErrorInfo;
           // return false;
            break;
        }
        $mail->ClearAddresses();
    }
    return true;
}